I think this is what you were trying to do.. I am a beginner myself so I know the code might be bad, but it works.
By the way, you dont need all of those headers. I just find myself putting them in a lot of my C++ programs because I dont want to write code and forget the header then think that Im doing something wrong when in reality I had just forgotten the headers.
#include <iostream>
#include <string>
#include <windows.h>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int input, neg = 0, pos = 0;
cout << "Input a positive or negative number, or type 'x' to exit\n";
cin >> input;
cin.ignore();
if (input > 0)
{
pos++;
} else if (input < 0)
{
neg++;
}
while (input < 0 || input > 0)
{
cout << "Input another positive/negative number or 'x' to stop\n";
cin >> input;
cin.ignore();
if (input > 0)
{
pos++;
} else if (input < 0)
{
neg++;
}
}
cout << "You had " << pos << " positive numbers.\n";
cout << "You had " << neg << " negative numbers.\n";
return 0;
}
|
And if you want your program to specifically stop running when "x" is pressed, as opposed to it stopping running when anything other than a negative or positive interger is pressed, as in my program, what you have to do is change your conditions in the do while/while loop.
In my program I put
while (input < 0 || input > 0) |
What that does is check to see if any number over 0, or any number under 0 has been entered. If so, the loop runs. If not, e.g. the user types in a random string like "dksjdksjd", that is not recognized in the while loop as a positive or negative interger, therefore the loop is stopped and it puts the last two cout outputs, and the program is stopped.
If you want it to specifically stop on "x" just change the while loop condition to something like while (input != 'x'). Thats just an example.. that wont work though because input is a int and x is a char value, so it will confuse the program. But you get the idea..