Homework help

Need help with the user friendly functions and how to ask the user if they want to play again.

The assignment:
An elementary school teacher is looking for a way to help students practice multiplication. You are asked to write a program that randomly chooses two positive one-digit integers (rand() function will be necessary). You then need to ask the student a multiplication question using these two integers:

How much is 6 times 7?

The student then types the answer. Your program checks the student's answer.

* If it is correct, call a user-written function [name it correct_response()] that will randomly choose and print one of the following responses:

Very Good!

Excellent!

Keep up the good work!

Nice Work!

Then ask the student another question.

* If it is incorrect call a user-written function [name it incorrect_response()] that will randomly choose and print one of the following responses:

No, please try again.

Wrong. Try once more.

Don't give up!

No. Keep trying.

Then let the student try the same question, again, repeatedly until the student gets the correct answer.

The student should be able to play the game until he/she decides to quit.

Do you want to play again? (enter 1 for yes or 2 for no)

Here is my code:

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
 int main ()
	{
	    int numbers[3];
		srand ((unsigned)time(0));

		int rn1 = rand() % 9 + 1; /* Random number 1 */
        int rn2 = rand() % 9 + 1; /* Random number 2*/
        int answer = rn1 * rn2; 

	/*required of user. */ 
    cout << "\nThis program asks for a simple multiplication";
    cout << "\nanswer from the user.";
    cout << "\nPlease enter answer and press enter.";

    /*  while (answer == rn1 * rn2) */
	while (1)   /* loop until we break out */
    {
       cout << "\n\nHow much is " << rn1 << " times " << rn2 << " ? " << endl;
       cin >> answer;
       if (answer == rn1 * rn2) 
      {
		  case 0:
			  cout << "Very Good!"<< endl;
              break; 
		  case 1:
			  cout << "Excellent!" << end1;
			  break;
		  case 2:
			  cout << "Keep up the good work!" end1;
			  break;
		  default:
			  cout << "Nice Work!" <<end1;
			  break;
       }
       else 
           case 0:
              cout<< "No. Please try again."<<endl;
		   case 1:
              cout<< "Wrong. Try once more."<<endl;
           case 2:
              cout<< "Don't give up!"<<endl;
           default:
              cout<< "No. Keep trying."<<endl;
        }  
	system ("pause";
	return 0;
 
    }


I also get the following errors:

(16) : error C2065: 'cout' : undeclared identifier
(17) : error C2065: 'cout' : undeclared identifier
(18) : error C2065: 'cout' : undeclared identifier
(23) : error C2065: 'cout' : undeclared identifier
(23) : error C2065: 'endl' : undeclared identifier
(24) : error C2065: 'cin' : undeclared identifier
(27) : error C2046: illegal case
(28) : error C2065: 'cout' : undeclared identifier
(28) : error C2065: 'endl' : undeclared identifier
(30) : error C2046: illegal case
(31) : error C2065: 'cout' : undeclared identifier
(31) : error C2065: 'end1' : undeclared identifier
(33) : error C2046: illegal case
(34) : error C2065: 'cout' : undeclared identifier
(34) : error C2146: syntax error : missing ';' before identifier 'end1'
(34) : error C2065: 'end1' : undeclared identifier
(36) : error C2047: illegal default
(37) : error C2065: 'cout' : undeclared identifier
(37) : error C2065: 'end1' : undeclared identifier
(41) : error C2046: illegal case
(42) : error C2065: 'cout' : undeclared identifier
(42) : error C2065: 'endl' : undeclared identifier
(43) : error C2046: illegal case
(44) : error C2065: 'cout' : undeclared identifier
(44) : error C2065: 'endl' : undeclared identifier
(45) : error C2046: illegal case
(46) : error C2065: 'cout' : undeclared identifier
(46) : error C2065: 'endl' : undeclared identifier
(47) : error C2047: illegal default
(48) : error C2065: 'cout' : undeclared identifier
(48) : error C2065: 'endl' : undeclared identifier
(50) : error C2143: syntax error : missing ')' before ';'
i'm sure you have not forget to #include <iostream>
-------------
undeclared identifier errors ->
make sure you have using namespace std;


(27) : error C2046: illegal case and illegal defaults ->
Actually you have no switch statement so how could you possibly use case :

(34) : error C2146: syntax error : missing ';' before identifier 'end1' ->
that is endl; not end1;

(50) : error C2143: syntax error : missing ')' before ';'
as the error says you are missing ')' in system() command
Last edited on
I added those and it works with the following code I rewrote. All I need help with is incorporating the user friendly functions of correct_response() and incorrect_response () and the print responses to say the four different repsonses.

My working code without the user friendly functions:

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
int main ()
	{
	    int numbers[3];
	    int answer = 0; /* USER INPUT */
	    srand (time(0)); 
	    
	 
	   /* Initiate checking for correct answer */ 
	   while (answer != 1) /* WHILE USER HASN'T ENTERED 1 */
	   {
	      numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
	      numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
	      numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
	      cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */ 
	      cout << "\n\n""What is " << numbers[0] << "*" << numbers[1] << "?" << endl;
	      cin >> answer;
	      if (answer == numbers[0] * numbers[1]) /* check against the numbers array */
	      {
	           cout << "\n""Very Good!"<< endl; /* Outcome for correct answer */
	           
	      }
	 
	      else  /* statement for while loop */
	          cout << "\nNo, please try again! The correct answer was " << numbers[2] << endl; //Outcome for incorrect answer
	   }
	     return 0;
	}
I get no errors with the above code
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
using namespace std;
	 
	int main ()
	{
	    int numbers[3];
	    int answer = 0; /* USER INPUT */
	    srand (time(0)); 
	    
	 
	   /* Initiate checking for correct answer */ 
	   while (answer != 1) /* WHILE USER HASN'T ENTERED 1 */
	   {
	      numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
	      numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
	      numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
	      cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */ 
	      cout << "\n\n""What is " << numbers[0] << "*" << numbers[1] << "?" << endl;
	      cin >> answer;
	      if (answer == numbers[0] * numbers[1]) /* check against the numbers array */
	      {
	           cout << "\n""Very Good!"<< endl; /* Outcome for correct answer */
	           
	      }
	 
	      else  /* statement for while loop */
	          cout << "\nNo, please try again! The correct answer was " << numbers[2] << endl; //Outcome for incorrect answer
	   }
	     return 0;
	}

I added a switch and I still get the answer Very good

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (answer == numbers[0] * numbers[1]) /* check against the numbers array */
	      {
			  switch ()
			  {
			  case 0:
	           cout << "\n""Very Good!"<< endl; /* Outcome for correct answer */
			   break;
			  case 1:
			  cout << "\n""Excellent!" << end1;
			  break;
			  case 2:
			  cout << "\n""Keep up the good work!" << end1;
			  break;
			  default:
			  cout << "\n""Nice Work!" << end1;
			  break:
			  }
	      }
	 
	      else  /* statement for while loop */
	          cout << "\nNo, please try again! The correct answer was " << numbers[2] << endl; //Outcome for incorrect answer
	   }
Why isn't numbers[2] used when checking the answer?

The switch () statement needs to test some specific variable or expression. There's something missing here.
Topic archived. No new replies allowed.