Adding a user's choice.

I am giving the user a choice. He inputs the difficulty level he wants to attempt and the code acts appropriately. I don't know how to go about this. I can prompt for the choice and store it. I don't know how to implement it.

Currently, I have my code set up to randomly generate 2 numbers in between 1 and 9. That is the easyQuestions. The hardQuestions will be randomly generated numbers from 1-99. The user will have to multiply those.

I only have the first part, where there is no choice and I don't know how to implement one.

functions, how? if...else, how?

I am going to include my code.

-Josh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
using namespace std;











int main(int argc, const char * argv[])
{  
    
    
        
    
    //upon recieving a desired level program shall take the student to funciton 1 or function 2
    
    
    
    double correctAnswer = 0;
    double incorrectAnswer = 0;
    double percentCorrect;
    int level;
    
    
    cout << "What difficulty level would you like to try?\n" ;
    cout << "Enter 1 for easy problems or 2 for harder problems.\n";
    cin >> level;

    
    
        for(int counter = 1; counter<=5; counter++)
        {
            
            
            srand( (unsigned)time(0) );//randomize the randomizer with time as the seed
            double x = rand()  % 9 +1;//randomly generated number between 1 and 10
            double y = rand() % 9 +1;//randomly generated number between 1 and 10
            
            double product = x * y;//the correct answer
            
            double answer;//student's answer input
            
            cout << "What is " << x << " times " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
            
            cin >> answer;//users answer to be checked and compared with computer's product
            
            
            if (answer == product)//checking to see correctness of user's answer
            {
                ++correctAnswer;
                int goodStatement = rand()  % 3 +1;
                switch (goodStatement)
                {
                    case 1:
                        puts("Very good!");
                        break;
                    case 2:
                        puts("Excellent");
                        break;
                    case 3:
                        puts("Nice work!");
                        break;
                    case 4:
                        puts("Keep up the good work");
                        break;
                        
                    default:
                        puts("Default good");
                        break;
                }

            }
            
            else
            while (answer!= product)
            {//while loop shall run with the same x and y values being presented. The program will not move forward until a correct answer is entered by user.
         
                ++incorrectAnswer;
                int badStatement = rand()  % 3 +1;
                switch (badStatement)
                {
                    case 1:
                        puts("No. Please try again");
                        break;
                    case 2:
                        puts("Wrong. Try once more");
                        break;
                    case 3:
                        puts("Don't give up");
                        break;
                    case 4:
                        puts("No. Keep trying");
                        break;
                    default:
                        puts("This is the default no");
                        break;
                }

                cout << "What is " << x << " times " << y << "?" << endl;
                cin >> answer;
            
            if (answer == product)//checking to see correctness of user's answer
            {
                ++correctAnswer;
                int goodStatement = rand()  % 3 +1;
                switch (goodStatement)
                {
                    case 1:
                        puts("Very good!");
                        break;
                    case 2:
                        puts("Excellent");
                        break;
                    case 3:
                        puts("Nice work!");
                        break;
                    case 4:
                        puts("Keep up the good work");
                        break;
                        
                    default:
                        puts("Default good");
                        break;
                }       
            }
        }            
    }
    
    cout << "You got " << correctAnswer <<" of them correct. Great work!" << endl;
    cout << "You missed " << incorrectAnswer << "." << endl;

    percentCorrect = (correctAnswer / correctAnswer + incorrectAnswer)*100;
    cout << percentCorrect << (setprecision(2)) << "%" << endl;
        if (percentCorrect < .75) {
            cout << "Please ask you teacher for extra help." << endl;       
        } else {
            cout << "Congratulations, you are ready to go the next level!" << endl;
        }     
}
Hint: What are you really asking?

If easy questions are random numbers from 1-9 and harder questions are 1-99... what is the real difference in your program?

Xen
Hi Xen,

I am asking, if the user choses difficulty 1 how do I only run that code? If the user chooses difficulty 2 how do I only run that code?

Lines 46-48 define variables x and y with random numbers of 1-9. How can I make that a choice (difficulty 1)?

-Josh
1
2
3
4
5
6
7
8
9
10
11
if (choice == 1) // if user wants easy questions
{
   a = rand()  % 9 + 1;
   b = rand()  % 9 + 1;
}

else if (choice == 2) // if user wants harder questions
{
   a = rand() % 99 + 1;
   b = radn() % 99 + 1;
}


Then implement your multiplication and ask the user to answer the question.
@TheJJJunk

That worked, thank you.

Your answer was so much simpler than what I was envisioning. I had an inkling towards an if...else statement, but I didn't even know how to implement that.

Thanks for the push.

-Josh
Instead of putting TheJJJunks code so that it must read the if's each time through the counter loop, a better way would be to create 2 new variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	int level, rand_x, rand_y; // Added 2 variables to hold the limits of random numbers x and y

	cout << "What difficulty level would you like to try?\n" ;
	do
	{
		cout << "Enter 1 for easy problems or 2 for harder problems.\n";
		cin >> level;
		if (level <1||level>2)
			cout << "Sorry. At the moment, there are only 2 levels to choose from.." << endl << endl;
                       // Above informs user that there are only the 2 levels at present
	} while (level <1 || level >2); // Only allows a 1 or 2, to be entered
	if (level == 1)
	{
		rand_x = 9;
		rand_y = 9;
	}
	else
	{
		rand_x = 99;
		rand_y = 99;
	}

 for(int counter = 1; counter<=5; counter++)
        // The rest of your is here... 
Topic archived. No new replies allowed.