I am doing a homework assignment for c++. I am able to get my program to run, but I don't know how to modify it for my next assignment. Here is the question.

Modify the program so that various comments are printed for each correct answer and each incorrect answer as follows:
Response to a correct answer:
Very good!
Excellent!
Nice Work!
Keep up the good Work!

Response to an incorrect answer:
No, Please try again
Wrong.. Try once more
Don't give up!
No Keep trying.
User the random number generator to choose from 1-4 to select an appropriate response to each answer. User a switch to issue the response.

I don't know how to do this part: Any help would be great. Here is my code:

// practice.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
using std::rand;
using std::srand;
int main()
{
//generates a new random set each time:

int num1;
int num2;
int answer=1;
int compute;
int x=1;
srand ((unsigned)time(0));


while (x==1)

{

num1=rand()%5+1;
num2=rand()%5+1;
compute=num1*num2;



while (answer!=compute)

{

cout<<"How much is"<<num1<<"*"<<num2<<"?"<<endl;
cin>>answer;

if (answer==num1*num2)

cout<<"very good"<<endl;
 
else 

cout<<"No, please try again."<<endl;
}
}

}

You could have 2 switch statements, one for correct answer and one for incorrect. Then store each of the responses in the appropriate switch statement. You'd also have a variable that is randomly generated to be 1-4, then use that variable for selecting the response in the switch statement..

int random_variable;
switch (random_variable)
case: 1
     cout << "Very Good!" << endl;
case: 2
     cout << "Excellent!" << endl;
// etc.

Put that in a loop so random_variable keeps randomly generating another value.

Edited 7 Years Ago by Kontained: Typo

Maybe this will work:

#include <iostream>
//used to seed rand
#include <time.h>

using namespace std;
int main()
{
	int num1 = 4, num2 = 5, answer, random;

	//random generator
        srand ((unsigned)time(0));
	random = rand() % 4;

	//do-while to allow the user to try again.
	do{

		cout << "How much is " << num1 << " multiplied by " << num2 << "? : ";
		cin >> answer;
		cout << endl;
		if(answer == 20)
		{
			//switch depending on random generator
			switch(random)
			{
			case 1:
				cout << "Very Good!";
				break;
			case 2:
				cout << "Excellent!";
				break;
			case 3:
				cout << "Nice Work!";
				break;
			default:
				cout << "Keep up the Good work!";
				break;
			}
			cout << endl;
			cout << endl;
		}
		else
		{
			//switch depending on random generator
			switch(random)
			{
			case 1:
				cout << "No, Please try again.";
				break;
			case 2:
				cout << "Wrong...try once more.";
				break;
			case 3:
				cout << "Dont give up!";
				break;
			default:
				cout << "No, Keep Trying.";
				break;
			}
			cout << endl;
			cout << endl;
		}
	}while(answer != 20);
}

Edited 7 Years Ago by restrictment: n/a

*bump* and *edit*
I accidentally placed the random integer outside of the do-while loop. Do you know what this means? It means that at first the remark will be random, but the following remarks will all be the same as the first. (Not Random.)

I figured out my mistake, and my mistake was that I did not place the 'random' within the do-while perimeter. No that I added it within the loop the random number will seed each time it goes through, and produce a new number.

As well, the %4 simply makes the random number anywhere between 1 and 4. (I am not sure if you knew this or not, because of the way your code was currently structured.)

Anyway the new code looks like this:

#include <iostream>
//used to seed rand
#include <time.h>

using namespace std;
int main()
{
	int num1 = 4, num2 = 5, answer, random;

	//random generator
	srand ((unsigned)time(0));

	//do-while to allow the user to try again.
	do{
		random = rand() % 4;
		cout << "How much is " << num1 << " multiplied by " << num2 << "? : ";
		cin >> answer;
		cout << endl;
		if(answer == 20)
		{
			//switch depending on random generator
			switch(random)
			{
			case 1:
				cout << "Very Good!";
				break;
			case 2:
				cout << "Excellent!";
				break;
			case 3:
				cout << "Nice Work!";
				break;
			default:
				cout << "Keep up the Good work!";
				break;
			}
			cout << endl;
			cout << endl;
		}
		else
		{
			//switch depending on random generator
			switch(random)
			{
			case 1:
				cout << "No, Please try again.";
				break;
			case 2:
				cout << "Wrong...try once more.";
				break;
			case 3:
				cout << "Dont give up!";
				break;
			default:
				cout << "No, Keep Trying.";
				break;
			}
			cout << endl;
			cout << endl;
		}
	}while(answer != 20);
}

*Sorry for the double post, I just thought this post would be worth the bump..besides I would have really liked a response for the effort in making the code, as I am a newbie myself. :)*

Edited 7 Years Ago by restrictment: n/a

Thanks..but my question is if you are answer==20..then if the user types in 20 then the code will break out of the loop terminate right?

This article has been dead for over six months. Start a new discussion instead.