Reputation: 1
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;
}
Upvotes: 0
Views: 26228
Reputation: 152
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");
}
Upvotes: 0
Reputation: 66932
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.
Upvotes: 4
Reputation: 15872
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;
}
Upvotes: 1
Reputation: 311038
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++;
}
}
Upvotes: 0
Reputation: 152
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')
Upvotes: -1