C :: How To Stop User From Inputting Negative Number Into Unsigned Long
Feb 3, 2013
here's one more thing id like to do to make the input even better able to handle user error, but im not sure if its possible or at least easy. I need the function to return a large positive number. As of right now, it can handle users entering characters, but what if the user enters a negative number? is there a way to check to see if what is coming in is negative before the sign gets lost in conversion to unsigned"ness"?
Code:
unsigned long getNum(char prompt[80])
{
unsigned long darts;
printf("%s", prompt);
while((scanf("%lu", &darts)) != 1)
{
[code]....
I am trying to find the max number entered by the user, and it should terminate when a negative number is entered. For my code, it will just end when the user inputs a lower number than the previous. i.e.- 10 20 15 "The highest number is 20" when it should be "10 20 5 40 15 -1" "The highest number is 40". No arrays or do/while loops either.
#include <iostream> using namespace std; int Max(int x); int main() { int x;
Consider a new data type, the mikesint, which can hold 9 bits.
(a) What is the largest integer that an unsigned mikesint can hold? (b) What is the largest positive integer that a signed mikesint can hold? (c) What is the largest negative integer that a signed mikesint can hold?
I am a beginner trying to write a program that would state a user's chinese zodiac and horoscope sign after inputing his or her birthday. I have been, however, having trouble getting the program to run correctly.
You are to write a program that will allow a user to enter infinite numbers (greater than zero)(One number at a time). You must have a way for the user to stop entering values.Once the user stops, you will immediately display the following:
The lowest number entered is: The highest number entered is: The number of values entered is: The average of the numbers entered is: */
main() { int userNumber=0, sum=0, count=0, highNum=0, lowNum=0, lastNum = 0; double average; printf("Enter a number greater than 0: (Enter -1 to stop) "); scanf_s("%i", &userNumber);
[Code]....
My program outputs everything I need except the lowest number entered. I have been spending a considerable amount of time on this problem to no avail.
It's a tic-tac-toe program. I haven't finished doing everything I need to for the project, I'm just trying to get certain parts working as I go.
Anyway, my problem: The program will print the board and ask the first player which square they want to mark. Then it will print the updated board, print the question for the second player to input their square choice but not actually stop to let the user enter anything. Instead it prints the board again and then prints the question for player one again -- this time stopping to let them type in their choice.
Here's my code:
#include <stdio.h> // Function prototypes void printBoard(char board[3][3]); int main() { int quit = 0; // Loop so game continues until player quits
[Code] .....
And it looks like this when I run it, as an example:
I am studying c and I thought what would be better than using my pi to play with relays and c. I am used to PHP as a scripting lang and don' t do much programming. So I wrote this to use wirepi and ask the user "on or off" they type on or off and it does it. it does work but I know something is wrong when I use strcmp in the if statment I can -10 for a value. Here is the code.....
Code: #include <stdio.h> #include <string.h> main(int argc, const char * argv[]) { char oo[100]; system("gpio mode 0 out"); printf("Do you want it on or off?
[Code] ....
This is what it outputs at the prompt
Do you want it on or off? on on
It is now on!
How can I just get Code: if (oo = on) {} and like so with off.
So I have been given and as part of the solution I need to count the number of digits in a long long variable. To do this I use a while loop, but it is behaving strangely. Here is the code.
#include <stdio.h> #include <cs50.h> #include <math.h> int main (void) { printf("What is the card number?"); long long card = GetLongLong(); if(card <= 0)
[Code] .....
When I execute the program it asked for the number, but then nothing happens. Of course, my first instinct was that the program was caught in an infinite loop somehow, but could not figure out how. I commented out the while loop and the program completed (albeit returning the incorrect value), so I was further convinced that there was an infinite loop that I was not seeing. I ran the program throug gdb. Strangely, the program did not loop infinitely, instead it got to line 16 [while(card1 > 0] and then just stopped, it was not executing the next line of code at all.
how to stop outputting data based on a user input. The text file is as follows:
1. a 2. b 3. c
and the code I'm using is as follows:
int main (){ string line; int search; cout << "Enter a number from 1-3" << endl; cin >> search; search++; ifstream myfile ("example.txt");
[Code]...
What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"
How can I read a file that contains numbers only, but read it by three digits at a time? I have a long string of numbers and every three digits corresponds to a particular number in itself. i.e. a string of 064045154 would need to be read as '064' '045' and '154'. I need to then subtract one from each of these numbers and the new values I need to convert into their ASCII characters and place these in a new file. This is what I have (focusing on the 'Decrypt' function) but all it does is in the new file place a string of the same character repeated over and over a total number of times equal to the number of integers in the numbers file.
So, I'm attempting to create a grid on the screen, and to do so, I've implemented a multidimensional array of Rectangles. When the program starts, I use a for loop to increase the x and y coordinates to form the grid.
public Form1() { InitializeComponent(); for (int x = 0; x < 12; x++) { for (int y = 0; y < 12; y++) {
[Code] ....
My issue is trying to figure out when the user has clicked on a rectangle, and furthermore, which rectangle in the array that he/she has clicked on. As I will change the border to red when given the correct rectangle.
I'm using Visual Studio 2008, and here is my code so far.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
[Code] .....
I'm making this into a real game, with classes and all.
I have a thread with a while(1) loop in it. When the user push the stop button I would like that thread to end.
I thought about creating a bool and checking its value periodically in the thread and when I push the stop button I change the value of the bool for that the thread breaks out of the loop and finishes.
The code on lines 44-53 is suppose to display a message when the user enter a negative number, however, when a correct positive number is entered the message is display again.
#include<iostream> #include<cctype> using namespace std; int main() { char carType; int A, B, C;
I am getting strings from an HTTP request that will have hex values and I must convert those strings to a signed decimal.
//typical string inside response: //0E1D052BFBB711C1002C0042007A014DFE44022B270F7FFF8000000000000000 //every 4 characters above are a signed decimal value for (a = 0; a <= 63; a+=4){ sprintf(vval,"0X%c%c%c%c",response[a],response[a+1],response[a+2],response[a+3]); ds = strtol(vval, NULL, 16); sprintf(vval,"%d",ds); }
The problem is I never see a negative number. Decoding 0x8000 gives me 32768 but not -32768.
In this exercise:The C Programming Language Exercise 3-4..It states the following: "In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) ."
A char is one byte (255 bits). The range of an 8 bit variable using a two's complement representation is -128 to 127. Therefore -128 is the largest negative value. The statement in book suggests that the itoa function will not output -128 if we pass -128 as a parameter, because in itoa when we try to convert -128 to positive -128, the inverse of -128 is -128. However, I just ran this code in my computer and it successfully outputted -128.
Code:
#include <stdio.h> #include <string.h> #define SIZE 10 void reverse(char s[]) { int c, i, j; }
the real char got 1000 digits this is just example how do i convert chars from numbers[4] to numbers[15] and save them as one number ? in this case i will get int x = 5444546546545643 as u can see char numbers as a example above
So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.
My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. The code tag instructions were dubious.
Code:
#include <stdio.h> #include <math.h> main() { /*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/ printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root. "); float x,y,z; int num; num = 0;
I have a error with one of my programs. I'm supposed to get rid of negative numbers when there are numbers that are randomly generated. Here is the middle part of the code.
{ int vectorLength = 10; vector<int> bothSigns(vectorLength); cout << " Input vector: "; for (int i = 0; i < vectorLength; i = i + 1) { bothSigns[i] = rand()%201 - 100;
[code] .....
The part where i'm supposed to start is after the /////'s. However, whenever I input a number for the random numbers(not put in part of code), i keep getting a segmentation error.