doesn't help. This is what I get when I take the value of x out of the loop
Enter the value for x :
12345
12341
1232
123
14
05
Press any key to continue . . .
Oh my bad you have to take the cout function out also and in it insert a space between x and count because if you look closely and guessing your input was 12345 it happens that your are printing the loop so 1234 is x and 1 count... until you reach x = 0 and count = 5
the x value isn't correct because as the loop goes it goes down until it reaches 0 to fix that create another variable and assign x value to it and call it at the end
since negative numbers are less than zero you will need to make a function that checks if the number is negative and multiply it by -1 before sending it to the loop
we didn’t learned about stringing in class yet. My teacher is very particular about how we need to write our code. We just learned about looping and this is our 1st assignment on looping.
I have another problem with this homework assignment ---
I can't figure out how to loop it so that the user can test as many numbers as desired without leaving the program.
Also, I can't for the life of me figure out how make the program reject any input outside the long int range. My teacher didn't really explain any of this real well. Please can someone show me how to write this?
Well, let's address validating the input first. The way formatted extraction works (cin >> x) there is always going to be something in the input stream after it exits. Normally, this will be a '\n', but if a user entered a number that was too large to fit into the variable you're reading into, there will still be digits in the input stream.
You can check to see what the next character in the input stream is without removing it by invoking the peek method on cin. The other thing that's going in in the code below is that we're checking to see if the input operation succeeded (ie. nobody borked up our input with letters.)
1 2 3 4 5 6 7 8 9 10
bool input_is_valid = false ;
while ( !input_is_valid )
{
cout << "Enter the value for x: \n" ;
if ( cin >> x && cin.peek() == '\n' )
input_is_valid = true ;
elsewhile ( cin.get() != '\n' ) // remove unwanted stuff from the input stream.
;
}
As for the other thing, you just need to wrap everything in another loop and ask the user if he wants to keep entering numbers to evaluate.
I see. Thanks for the explanation . I didn’t know that there was always something in the input stream after it exits. My teacher didn't go through any of that. Thanks for the help.