Help with program

I'm trying to get this program to only ask if the user would like another question after the 5th correct answer. I think I'm a ways off. Can anyone walk me through the steps to doing this?

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
54
55
56
57
58
59
60
61
 #include <iostream>
#include <cstdlib> //contains function prototype for rand
using namespace std;
int generateQuestionAndAnswer();
void getComment(short commentType);
   
int main(){   
    int correctAns, answer, number = 0;
    char choice;
   
    do{
       correctAns = generateQuestionAndAnswer();
       do{
          cout << "\nYour answer: ";
          cin >> answer;
         
          if(answer == correctAns){
          	 number +=1;
             getComment(1);
          }else{
             getComment(0);
          }
         
       }while(answer != correctAns);
      	if(number >=5);
       		cout << "\n\nTry another question? [y/n] ";
       		cin >> choice;
    	}while(choice == 'y');
   
    //terminate progam successfully.. :)
    return 0;
}

int generateQuestionAndAnswer(){
    int num1, num2;
   
    num1 = 1 + rand() % 10;
    num2 = 1 + rand() % 10;
   
    cout << "\nHow much is " << num1 << " times " << num2 << "?\n";
   
    return num1 * num2;       
}
void getComment(short commentType){
    if(commentType == 1){
       switch(rand() % 4){
          case 0: cout << "\nVery good!"; break;   
          case 1: cout << "\nExcellent!"; break;   
          case 2: cout << "\nNice work!"; break;   
          case 3: cout << "\nKeep up the good work!";            
       }   
    }else{
       switch(rand() % 4){
          case 0: cout << "No. Please try again."; break;   
          case 1: cout << "Wrong. Try once more."; break;   
          case 2: cout << "Don't give up!"; break;   
          case 3: cout << "No. Keep trying.";         
       }   
    }
          
}
the 5 questions section is a mess.
if(condition); does nothing conditionally. the ; is wrong. ; is a do-nothing statement when it does not terminate a proper statement. for(things); while(condition); if(conditon); etc are all usually** wrong, no ; on that type of statement.

(line 25)
and your if there has no {} so the cin isnt tied to the prompt for it.
and choice has no initial value.
I would init choice to y, lose the ; and put {} around the prompt and cin for choice, see if that gets you close.

** you can have a reason to do it, like a self contained for loop.
Last edited on
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstdlib> //contains function prototype for rand

using namespace std;

int generateQuestionAndAnswer();
void getComment(short commentType);

int main()
{
    int correctAns{0}, trial_Ans{0}, number_tries{0};
    char choice;
    
    do
    {
        correctAns = generateQuestionAndAnswer();
        number_tries = 0;
        do
        {
            cout << "Your answer: ";
            cin >> trial_Ans;
            number_tries ++;
            
            if(trial_Ans == correctAns)
            {
               getComment(1);
            }
            else
            {
                getComment(0);
            }
            
        }while(trial_Ans != correctAns && number_tries < 5);
        
        cout << "Try another question? [y/n] ";
        cin >> choice;
        
    }while(choice == 'y');
    
    cout << "That's the end\n";
    
    //terminate progam successfully.. :)
    return 0;
}

int generateQuestionAndAnswer()
{
    int num1, num2;
    
    num1 = 1 + rand() % 10;
    num2 = 1 + rand() % 10;
    
    cout << "How much is " << num1 << " times " << num2 << "?\n";
    
    return num1 * num2;
}

void getComment(short commentType)
{
    if(commentType == 1)
    {
        switch(rand() % 4){
            case 0: cout << "Very good!"; break;
            case 1: cout << "Excellent!"; break;
            case 2: cout << "Nice work!"; break;
            case 3: cout << "Keep up the good work!";
        }
    }
    else
    {
        switch(rand() % 4)
        {
            case 0: cout << "No. Please try again."; break;
            case 1: cout << "Wrong. Try once more."; break;
            case 2: cout << "Don't give up!"; break;
            case 3: cout << "No. Keep trying.";
        }
    }
    cout << "\n\n";
}


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
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <ctime>

int generateQuestionAndAnswer();
void getComment(int commentType);

int main()
{
	constexpr unsigned NumCorr {5};
	unsigned number_correct {};

	srand(static_cast<unsigned>(time(nullptr)));

	for (char choice {'y'}; std::cin && choice == 'y'; (std::cout << "Try another question? [y/n] ") && (std::cin >> choice)) {
		do {
			const auto correctAns {generateQuestionAndAnswer()};
			bool correct {};

			do {
				int trial_Ans {};

				std::cout << "Your answer: ";
				std::cin >> trial_Ans;
				number_correct += correct = (trial_Ans == correctAns);
				getComment(correct);
			} while (!correct);
		} while (number_correct < NumCorr);
	}

	std::cout << "That's the end\n";
}

int generateQuestionAndAnswer()
{
	const auto num1 {1 + rand() % 10};
	const auto num2 {1 + rand() % 10};

	std::cout << "How much is " << num1 << " times " << num2 << "? ";

	return num1 * num2;
}

void getComment(int commentType)
{
	if (commentType == 1)
		switch (rand() % 4) {
			case 0: std::cout << "Very good!"; break;
			case 1: std::cout << "Excellent!"; break;
			case 2: std::cout << "Nice work!"; break;
			case 3: std::cout << "Keep up the good work!";
		}
	 else
		switch (rand() % 4) {
			case 0: std::cout << "No. Please try again."; break;
			case 1: std::cout << "Wrong. Try once more."; break;
			case 2: std::cout << "Don't give up!"; break;
			case 3: std::cout << "No. Keep trying.";
		}

	std::cout << "\n\n";
}

Topic archived. No new replies allowed.