Unintended loop

I want the program to return the user to the begining of the question and have them enter a new answer. Only problem is it repeatedly loops the lines:

cout << "The answer you've selected does not exsist! Please try again." << endl;
QuestionA();

which as you can imagine just leaves a wall of ""The answer you've selected does not exsist! Please try again." in the window. Any ideas on how to fix it?


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
#include <iostream>

using namespace std;

int a;

void QuestionA()
{

	cout << "Is it freaking you out?" << endl;
	cout << "1. Yes it is!\n2. Not at all." << endl;
	cin >> a;
	if (a == 1) {
		cout << "valid answer 1" << endl;
	}
	else if (a == 2) {
		cout << "valid answer 2" << endl;
	}
	else {
		cout << "The answer you've selected does not exsist! Please try again." << endl;
		QuestionA();
	}


}

int main()
{
	a = 0;
	
	cout << "Are you about to squish that bug?\nBefore you do, ask yourself the following:\n(For each question please choose the answer that best fits)\n" << endl;

	QuestionA();

	return 0;

}
Last edited on
1. You should avoid global variables.

2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while ( true ) {
	cin >> a;
	if (a == 1) {
		cout << "valid answer 1" << endl;
		break;
	}
	else if (a == 2) {
		cout << "valid answer 2" << endl;
		break;
	}
	else {
		cout << "The answer you've selected does not exsist! Please try again." << endl;
	}
}
That's because you put "int a" so if you write any letters goes in a loop.
Change "int a" with "string a" in this way:
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
#include <iostream>

using namespace std;

void QuestionA(string a)
{

	cout << "Is it freaking you out?" << endl;
	cout << "1. Yes it is!\n2. Not at all." << endl;
	cin >> a;
	if (a == "1") { //Now "a" is a string, so I have to put the numbers as part of a string, not a number
		cout << "valid answer 1" << endl;
	}
	else if (a == "2") {
		cout << "valid answer 2" << endl;
	}
	else {
		cout << "The answer you've selected does not exsist! Please try again." << endl;
		QuestionA(a);
	}


}

int main()
{
	string a =""; //Initializing the string.
	
	cout << "Are you about to squish that bug?\nBefore you do, ask yourself the following:\n(For each question please choose the answer that best fits)\n"<<endl;

	QuestionA(a);

	return 0;

}
Doesn't do that when I run it. Seems to work as intended.
The code you posted works for me if I enter an invalid number, but not if I enter a letter. The reason is that line 12 fails and leaves the letter in the input buffer.

Another problem is that you shouldn't use recursion when you really just want a loop. Here's a fixed version:

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
#include <iostream>
#include <limits>

using namespace std;

int a;

void QuestionA()
{

    while (true) {
        cout << "Is it freaking you out?" << endl;
        cout << "1. Yes it is!\n2. Not at all." << endl;
        cin >> a;
        if (a == 1) {
                cout << "valid answer 1" << endl;
                break;
        }
        else if (a == 2) {
                cout << "valid answer 2" << endl;
                break;
        }
        else {
                cout << "The answer you've selected does not exsist! Please try again." << endl;
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
}

int main()
{
        a = 0;

        cout << "Are you about to squish that bug?\nBefore you do, ask yourself the following:\n(For each question please choose the answer that best fits)\n" << endl;

        QuestionA();

        return 0;

}
Topic archived. No new replies allowed.