DOING LOOP UNTIL A KEY IS PRESSED

hi, i feel dumb for asking .. but i'm a very beginner !
i'm trying to make a program that keep taking integer numbers from user and calculate the positive and negative integers, until the user press a key , say that key is a character for example ... actually the programs runs but with error in calculating

so here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int input, pos=0, neg=0;
    char c;

    cout<<"please enter your numbers(press X to exit)";

    do
    {
        cin>>input;
        if (input>0) {pos++;}
        else {neg++;}
    } while (cin>>c);

    cout<<pos<<endl;
    cout<<neg;
    
    return 0;
}




and another thing .. i want to specify a key to press .. say i want to specify the character X .. so if user entered any character the program ignore it and keep taking input until X is pressed .. :]

You didn't ask anything
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..
Last edited on
samuel,
i was asking how i can i make the program take input from user until he press a specific key,

Lansana,
thank you, never thought about cin.ignore() ..
but i wonder if you know how to specify a key to exit/stop the program

like "press ESC to exit"
i tried

1
2
3
4
 if (GetAsyncKeyState(VK_ESCAPE))
        {
            exit = true;
        }


but no effect :D
i also included| bool exit; |
Last edited on
Well if you do the code that you just mentioned, your while loop could be while (exit == false) then execute the code. If they click ESC then exit would be true, and if the while loop checked for exit and found it to be true, which contradicts the conditions in the original while loop, then the program would stop.

I dont know for sure if thatll work as Im a beginner just like you, but it sounds logical, and SHOULD work.

Hope that makes sense
Topic archived. No new replies allowed.