Char problems

I have a slight problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char cPlay = 'y';
while(cPlay == 'y')
...
...
    cout << "\nWould you like to play again?\nI will give you an option to pick
         << another level of difficulty(y/n)" <<endl;
    cin >> cPlay;
    cPlay = tolower(cPlay);
    while(cPlay != 'y' && cPlay != 'n')
        {
            cout << "Unexpected input!\ny/Y = Keep playing\nn/N = Exit";
            cin >> cPlay;
            cPlay = tolower(cPlay);
        }

    }
    cout << "I hope you enjoyed the first game i ever created =]" << endl;
    system("PAUSE");
    return 0;


So the problems are: If a user inputs "yes" the game turns into a wild, continuous loop.
If the user inputs "hello", The error pops up 5 times in a row.

I tried using cin >> setw(1) >> cPlay, but that didnt work.
Tried using cin.clear() / cin.ignore() for the hello problem, and couldnt get it to come out right.

So im wondering how i can get by these problems. i only want the error message to pop up once, no matter what the person answers. And if the user types in "yes" i want it to goto the error message. If i just cut off the "es" in "yes", then that would mean im continuing the program even if the user wrote "You Stink"

Any ideas/answers?
Last edited on
input it as a string then account for all the situations that you want It to work.

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
46
47
48
49
50
51
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string cPlay = "Y";
	string temp;

	while(cPlay == "Y")
	{

		cout << "\nWould you like to play again?\nI will give you an option to pick";
		cout << " another level of difficulty(y/n)" <<endl;
		getline(cin, temp);

		cPlay = "";

		for(auto i = 0; i < temp.length(); i++)
		{
			cPlay += toupper(temp.at(i));
		}

		if (cPlay == "YES")
			cPlay = "Y";
		else if (cPlay == "NO")
			cPlay = "N";

		while(cPlay != "Y" && cPlay != "N")
		{
			cout << "Unexpected input!\ny/Y = Keep playing\nn/N = Exit";
			getline(cin, temp);

			cPlay = "";
			for(auto i = 0; i < temp.length(); i++)
			{
				cPlay += toupper(temp.at(i));
			}

			if (cPlay == "YES")
				cPlay = "Y";
			else if (cPlay == "NO")
				cPlay = "N";
		}

	}

	cout << "I hope you enjoyed the first game i ever created =]" << endl;
	cin.ignore();
	return 0;
}
Last edited on
Thank you for answering my question. I did something similar.

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
int main()
{
while(cPlay == 'y')
...
...
cout << "\nWould you like to play again?\nI will give you an option to
                << pick another level of difficulty(y/n)" <<endl;
        char szTest[2];
        cin.ignore(1,'\n');

        cin.getline(szTest, 512);
        while(szTest[1] != '\0')
        {
            ugh:
            cout << "Unexpected input!\ny/Y = Keep playing\nn/N = Exit"<<endl;
            cin.getline(szTest, 512);
        }
        cPlay = szTest[0];
        cPlay = tolower(cPlay);
        if(cPlay != 'y' && cPlay != 'n')
        {
            goto ugh;
        }
    }
    cout << "I hope you enjoyed the first game i ever created =]" << endl;
    cout << "Press ENTER to exit...."
    cin.ignore();
    return 0;
}



Thank you for the cin.ignore() idea instead of system("PAUSE"), i like it much better ^_^

OH, and i tried out your
getline(cin,szString)
Didnt work at all, wondering if it only works with a string variable?
Topic archived. No new replies allowed.