help with loop and return value

Hi, I have a question about my code and the return value. It's rather long but mostly because it's convoluted and/or poorly written.

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
bool getResponseToAnotherSimulation()
{
	char answer;  // holds response
	bool boolForAnswer;		// holds bool for response
    cout << endl << "**********************************************************************" << endl << endl;
	cout << "Do you want to do another simulation? Enter Y or N: ";
	cin >> answer;
    if((answer == 'Y') || (answer == 'y')) {  // correct responses
        boolForAnswer == true;
    }
    else if((answer == 'N') || (answer == 'n')) {  // correct responses
        printClosingBanner();
        boolForAnswer == false;
    }
    else {
		while((answer != 'Y') && (answer != 'y') && (answer != 'N') && (answer != 'n')) {	// repeat prompt until correct answer given
            cout << "Please re-enter your answer as Y or N: ";		
            cin >> answer;
			cout << endl;
		}
		if((answer == 'Y') || (answer == 'y')) {
			boolForAnswer == true;
		}
		else {
			printClosingBanner();
			boolForAnswer == false;
		}
	}
	return boolForAnswer;
}



basically, i use this function as my return value in int main to see whether i'm going to run a simulation again. the player can either enter Y y or n N -- if they don't an error message prompts them repeatedly to enter their answer in correct format. my output, however, looks like this

Do you want to do another simulation? Enter Y or N: a
Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: a

Please re-enter your answer as Y or N: n

Goodbye and Good Luck at Endor!

**********************************************************************

**********************************************************************

Do you want to do another simulation? Enter Y or N:


in other words, after saying 'n' it asks again if i want to run another simulation.

my int main is a do while loop that ends with
1
2
3
4
5
6
getResponseToAnotherSimulation();
	
}
while(getResponseToAnotherSimulation() == true);
return 0;
}


why does it re-run the prompt???!
Last edited on
You are calling your function twice, but discarding the results of the first call. While loops evaluate what is in the parentheses before executing. So the first time getResponseToAnotherSimulation() is called the answer is ignored and then it gets called again at the start of your while loop.
you mean the while loop in int main? should i just delete the first function call to getResponse in my int main? or is there a way to specify in my while condition something like, "while the above answer is true..."
i just tried deleting the first function call, but now when enter N (false) it exits properly, but when i enter Y (true) it just exists and doesn't start over..

also, i thought that while loop was just a condition?
Last edited on
Topic archived. No new replies allowed.