Infinity loop, dunno why

hello!

idk why i am getting an infinity loop when i try to get a char to 'y'

idk just look

int yesorno;
cout<<"Go to see???\n(Y/N)" ;
cin>> yesorno;
if(yesorno != 'y')
{
do {
cout<<"\nYou should really go see it!\n";
cout<<"Go to see the gnome?(Y/N)";
cin>> yesorno;
}
while (yesorno != 'y');


and when it comes to this part it keeps scrolling:

You should really go see it!
Go to see the gnome?(Y/N)
You should really go see it!
Go to see the gnome?(Y/N)
You should really go see it!
Go to see the gnome?(Y/N)

and so on for infinity, and it doesnt let me type in letter y.

heres some of the code im using and where i am encountering the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


system("echo The ship gets on fire and is falling down"); 
system("echo And so, the ship crashes.");
system("echo As you climb out of the rubble,you hear voice of an old gnome..."); 
int yesorno;
cout<<"Go to see???\n(Y/N)" ;
cin>> yesorno;
if(yesorno != 'y')

{
do
{
cout<<"\nYou should really go see it!\n";
cout<<"Go to see the gnome?(Y/N)";
cin>> yesorno;
system("pause");
}
while (yesorno != 'y');  



}


system("cls");      


 
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


it's becouse yesorno is an integer.
when you write cin>>yesorno; you ask to find an integer in cin, but since you,ve pressed a char, no data is read and no data is removed from the stream.
change int to char and it should work.

btw: don't use system. http://www.cplusplus.com/forum/articles/11153/
Omg thanks, i feel so stupid! :D
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <locale>

using namespace std;

bool question(std::string msg)
{
    locale loc;
    string input = "";
    char responce  = {0};

    while(true)
    {
        cout << msg << " (Y/N): ";
        getline(cin, input);
        
        if (input.length() == 1) 
        {
            responce = toupper(input[0],loc);
            
            if(responce == 'Y')
                return true;
            else if(responce == 'N')
                return false;
     
        }
    }
}

int main()
{ 
    cout << "The ship gets on fire and is falling down" << endl; 
    cout << "And so, the ship crashes." << endl;
    cout << "As you climb out of the rubble,you hear voice of an old gnome..." << endl;
    if(question("Go to see?") == false)
    {
        do
        {
            cout<<"\nYou should really go see it!\n";

        } while (question("Go to see the gnome?") == false);
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.