Do While loop trouble

closed account (Ebf21hU5)
I've written a program that acts as a Math Tutor and I want to make it so that the user has 5 attempts to get the question right. I want to use a do while loop but I'm having trouble making it 5 attempts so far it only gives the user 1 attempt. Here's what I have so far. Any suggestions to make it better will be greatly appreciated.

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
52
53
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int rnum1 = (rand()%100)+1;
	int rnum2 = (rand()%100)+1;
	int stdntAns;
	int NumOfAttempts = 0;

	cout << "Welcome to Math Tutor" << endl;
	cout << "This program will present you with two random numbers that you will add together." << endl;
	cout << "When you're done typing your answer press Enter to see if it's correct." << endl;
	cout << "Good Luck!" << endl;
	cout << " " << endl;
	cout << "What is " << rnum1 << "+" << rnum2 << "= ?" << endl;
	cin >> stdntAns;
	{
		char enter;
		
		do
			{
				cin.get(enter);
			}
		while (enter != '\n');
	}
	do
		{
			NumOfAttempts = NumOfAttempts + 1;
		}
	while (NumOfAttempts <= 5);
		{

		if (stdntAns == (rnum1+rnum2))
			{ 
				cout << "Congrats! That is the correct answer." << endl;
			}
		else if (stdntAns != (rnum1+rnum2))
			{
				cout << "That answer is incorrect. Please try again." << endl;
				cin >> stdntAns;
			}
		else 
			{
				cout << "You have reached your limit of guesses." << endl;
			}
		}

	system ("pause>nul");
	return 0;
}
This
1
2
3
4
5
	do
		{
			NumOfAttempts = NumOfAttempts + 1;
		}
	while (NumOfAttempts <= 5);
does nothing more than increasing the variable NumOfAttempts

your can use a for loop like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	int NumOfAttempts = 0; 
	for(; NumOfAttempts < 5; NumOfAttempts = NumOfAttempts + 1)
	{

		if (stdntAns == (rnum1+rnum2))
			{ 
				cout << "Congrats! That is the correct answer." << endl;
				break; // Note: this will end the loop
			}
		else if (stdntAns != (rnum1+rnum2))
			{
				cout << "That answer is incorrect. Please try again." << endl;
				cin >> stdntAns;
			}
	}
	if(NumOfAttempts >= 5)
		{
			cout << "You have reached your limit of guesses." << endl;
		}
closed account (Ebf21hU5)
Ok that makes perfect since. Thanks for your help!
Topic archived. No new replies allowed.