0

I'm writing a program in C++ that takes integers from the user until they press "x" to stop.

Then the program will print the number of positives, negatives and zeros.

But whenever the user inputs "x", the program goes into an infinite loop.

I tried removing the "ZEROS" part and just made counters for positives and negatives and it worked good. But I want to count the zeros.

I need to let the user enter numbers including 0 until they enter character x.

Here is my code:

#include <iostream> using namespace std; int main() { int input, neg = 0, pos = 0, zer = 0; char z; do { cout << "Input another positive/negative number or 'x' to stop\n"; cin >> input; cin.ignore(); if (input > 0){ pos++; } else if (input == 0){ zer++; } else if(input < 0){ neg++; } } while (z!='x'); cout << "You entered " << pos << " positive numbers.\n"; cout << "You entered " << neg << " negative numbers.\n"; cout << "You entered " << zer << "Zeros."; return 0; } 
5
  • 1
    What's z supposed to be? Commented Dec 19, 2013 at 21:12
  • variable under the datatype character ?!! because all the other variables are integers ! ok .. i used (char)input==126 just was trying but didn't work .. :D lol .. i wanted the user to enter a character .. :| i know it's wrong that's why i'm here Commented Dec 19, 2013 at 21:16
  • What happens if someone enters another key, is that specified? (If it is, then this task is VERY difficult, and should not be attempted until your third or fourth year of C++) Commented Dec 19, 2013 at 21:18
  • check out below..that should work but dont worry about it you'll learn eventually Commented Dec 19, 2013 at 22:09
  • This is just a tip. Change the last if else - statement to just an else. In the two if-s above you have already checked wether it is greater or equal to zero. last option is by default less :) Commented Dec 19, 2013 at 22:36

5 Answers 5

4

By far the simplest way of getting numbers until a user enters something else is this:

int input = 0; cout << "Input a positive/negative number or 'x' to stop\n"; while(cin >> input) { //they entered a number, do stuff if (input > 0) pos++; else if (input == 0) zer++; else if (input < 0) neg++; cout << "Input another positive/negative number or 'x' to stop\n"; } //cin failed to read a number, probably because they entered a letter //if they failed to enter a number, we need to clear the fail flag before we can use cin again cin.setstate(cin.rdstate()&~std::ios_base::failbit); cout << "You entered " << pos << " positive numbers.\n"; cout << "You entered " << neg << " negative numbers.\n"; cout << "You entered " << zer << "Zeros."; 

I wouldn't recommend anything more complicated until you get very advanced with C++. Parsing input is immensely difficult to get correctly, and many experienced people get it wrong.

Sign up to request clarification or add additional context in comments.

11 Comments

You should note that he will need to clear the input buffer and error flags for std::cin after the loop.
and if the user enters k,a,b,c,e,f,r,t,y it will break the loop as well..as a matter of fact any non integer...-_-
thanks .. i should consider such conditions while writing my code :/
@MooingDuck You just need to call std::cin.clear() - no need to worry about the rest of the read states.
@Jay: Really? It compiles? I'm getting better at this!
|
1

In order to correctly handle input errors and limit it so that only lower case x will break your loop, you need to do a lot of error checking:

#include <iostream> #include <sstream> #include <string> int main() { int neg = 0; int pos = 0; int zer = 0; std::string line; while (std::cin >> line) { if (line == "x") { break; } std::istringstream iss(line); // convert to a stringstream int val = 0; if (!(iss >> val)) // if we can load an int, do it, otherwise show and error message { std::cout << "Please enter a valid number!" << std::endl; continue; } if (val > 0) { pos++; } else if (val < 0) { neg++; } else { zer++; } } std::cout << "You entered " << pos << " positive numbers.\n" << "You entered " << neg << " negative numbers.\n" << "You entered " << zer << " zeros." << std::endl; return 0; } 

Comments

0

The problem is that an object of type int may not read symbols as for exmaple 'x' from a stream. It expects digits in an input stream. So when a symbol that can not be in a number is encountered in an input stream an error is arised. The stream will have erroneous state. If you will try again and again to read a number the stream will give nothing due to its state and the fact that the next symbol is for example non-digit.

So there is no sense to compare variable input with 'x'.

I would rewrite your loop the following way

while ( true ) { int input; cout << "Input another positive/negative number or 'x' to stop: "; if ( !( cin >> input ) ) break; if (input > 0) { pos++; } else if (input == 0) { zer++; } else { neg++; } } 

1 Comment

oh i got the idea .. actually i tried using break; but i used the wrong way! :) thanks
0

check this out

#include <iostream> #include<string> using std::string; using std::getline; using namespace std; int main() { string input; int neg = 0, pos = 0, zer = 0; char z; input = ""; int iVal = 0; do { cout << "Input another positive/negative number or 'x' to stop\n"; getline(cin, input); iVal = atoi(input.c_str()); if (input != "x" && input !="X") { if (iVal > 0) { pos++; } else if (iVal == 0) { zer++; } else if(iVal < 0) { neg++; } } } while (input != "x" && input != "X"); cout << "You entered " << pos << " positive numbers.\n"; cout << "You entered " << neg << " negative numbers.\n"; cout << "You entered " << zer << " zeros.\n"; return 0; system("pause"); 

}

3 Comments

Variables that you want to initialized should be when you define them: string input(""), although in this case "" is the default anyway.
This adds one to the zer veriable when I enter 50123456789
Use std::stol instead of ::atoi, but you'll find that using std::getline in this particular case introduces a new set of issues to deal with (e.g. if your input is "123 x", the 123 is valid input and will be treated as such, but the x will simply be discarded).
-1

IT goes into the loop for a few reasons

1)You declared input as an integer, for this purpose ypu would have to declare it as char data type in order to do your validation on it
2)You dont have a if condition for x eg else if (input =='x')

1 Comment

how can i assign input with a character when it's already declared as an integer ?!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.