C++ assistance please, before midnight.

#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{

int num = 0;
int num2 = 0;
int user = 0;
int answer1 = 0;
int count = 0;

srand(unsigned(time(0)));

for(int i=1 ; i<=5 ; i++)
{
num = (rand()&#37;8)+ 2;
num2= (rand()%8)+ 2;
answer1=num*num2;
cout<<"\nWhat is "<<num<<" x "<<num2<<endl;
cin>>user;

if(answer1==user)
cout<<"Correct!\n";
count++;
if(answer1!=user)
cout<<"Wrong! -> "<<num<<" x "<<num2<<" = "<<answer1;

}

cout<<"\nYou got "<<count<<" out "<<"5 right!\n";

system("pause");
return 0;
}

//I have this started for the below assignment. Can anyone please provide insight as to why I cant build on this solution.




Write a program that keeps generating two random numbers between 1 and 10 and asks the user for the product of the two numbers, e.g.: "What is 4 x 6?". If the user answers correctly, the program responds with "Right!"; otherwise, it displays: Wrong! 4 x 6 = 24.

Begin by asking the user how many questions to ask. Generate as many pairs of numbers as specified and get the answers from the user for each. If at any time, both numbers are the same as last time, generate two new numbers before asking for the answer. Continue generating 2 new numbers until at least one is different from last time.

After presenting the number of pairs of numbers specified and getting the answers, display how many the user got right; e.g.: You got 4 of 5 right. Then, ask if he or she wants to play again, like so: "Do you want to play again? [y/n]". If the user answers with 'y' or 'Y', it again reads the number of questions to ask and generates that many pairs of numbers and reads the answers like before. If the answer is n or N, it quits generating numbers. If the answer is anything but y, Y, n or N, it tells the user to enter one of those letters until it is.

When the user decides to quit and has got less than 75% of all the questions right, the program displays the multiplication table (1x1 through 10x10) before terminating.

After displaying the table, randomly generate two numbers between 1 and 10, display their product and first number and ask the user to guess the second as more practice. For example, the program will generate 7 and 9 and will display 63 and 7 and the user must guess the second number (i.e.: 9). Do this 3 times. Do not repeat code. Use a loop to do this 3 times.

Use a nested for loop to display the table; a bunch of cout statements will not be acceptable. You must also use a loop for any part that calls for repetition such as generating 5 pairs of numbers.

The following is a sample interaction between the user and the program:

Enter the number of questions to ask: 5

1. What is 3 x 9? 27
Right!

2. What is 2 x 7? 14
Right!

3. What is 8 x 9? 63
Wrong! 8 x 9 = 72

4. What is 6 x 3? 21
Wrong! 6 x 3 = 18

5. What is 2 x 9? 18
Right!

You got 3 out of 5 right which is 60%.

Play agian? [y/n] n
Last edited on
Use the following code snip as a base for your program

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
#include <iostream>
#include <utility>
#include <cstdlib>
#include <ctime>
#include <cctype>

int main()
{
	std::srand( ( unsigned int )std::time( 0 ) );

	char c;

	do
	{
		std::cout << "Enter the number of questions to ask: ";

		size_t n = 0;
		std::cin >> n;

		std::pair<int, int> numbers;
		size_t count = 0;

		for ( size_t i = 0; i < n; i++ )
		{
			// insert here your code
		}

		std::cout << "\nYou got " << count 
			  << " out of " << n 
			  << " right which is "
			  << ( n == 0 ? 0 : 100 * count / n ) << "%\n";
 
		std::cout << "\nPlay again? [y/n] ";
		std::cin >> c;
		c = std::tolower( c );
	} while ( c == 'y' );
}
Last edited on
try this brooowww...

#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{

int num=0;
int num2=0;
int user = 0;
int answer1 = 0;
int wrong = 0;
int right=0;

srand(unsigned(time(0)));

for(int i=1 ; i<=5 ; i++)
{
int nom[10]={1,2,3,4,5,6,7,8,9,10};
num =rand()%11;
num2=rand()%11;

answer1=nom[num]*nom[num2];

cout<<"\nWhat is "<<nom[num]<<" x "<<nom[num2]<<" ? ";
cin>>user;

if(answer1==user)
{
cout<<"Correct!\n";
right++;
}
if(answer1!=user)
{
cout<<"Wrong! -> "<<num<<" x "<<num2<<" = "<<answer1<<endl;
wrong++;
}


}

cout<<"\nYou got "<<wrong<<" wrong and "<<right<<" right!\n";

system("pause");
return 0;
}
@junkz


The proposed by you code 1) has a bad style of programming and 2) does not satisfy the assignment.:)

Good luck!
Last edited on
huge Codding list....
@vlad

thankz 4 ur comment,,but i just fix the program,,n its work,,
maybe u can show me the correct program.

thenkz...
The correct program shall follow the scenario

Enter the number of questions to ask: 5
 
1. What is 3 x 9? 27
 Right!
 
2. What is 2 x 7? 14
 Right!
 
3. What is 8 x 9? 63
 Wrong! 8 x 9 = 72
 
4. What is 6 x 3? 21
 Wrong! 6 x 3 = 18
 
5. What is 2 x 9? 18
 Right!
 
You got 3 out of 5 right which is 60%

Play agian? [y/n] n.


and provide that "If at any time, both numbers are the same as last time, generate two new numbers before asking for the answer."

As for the programming style you should declare names in that declarative regions where they are used, For example in your code variable answer1 (why is it named answer1 and not simply answer?) is used only within declarative region of the for statement. So it should be declared in this declarative region.

Some names should be introduced for magic numbers 10 and 11 ( is 11 connected with 10?).

Further in fact you are doing a stupidity selecting the same number from the array as its index. That is you are gettting for example an index equal to 5 and then you are accessing the array that to get number 6.:) It is too complicated. It is enough to get a random number in one step.

Moreover the code has a bug because random numbers that you are generating are in the range 0 - 10 but acceptable indexes are in the range 0 - 9.

Then it is better to substitute two if statements for one if-else statement.
Also it is better to have one variable right because variable wrong is calculated simply as 5 - right.

Last edited on
@vlad


you're right,,I was wrong in 11,,its should be 10,,
thenkz for the lesson..
and,,
what you mean "selecting the same number from the array as its index"?
You are getting a random number and then accessing the array that to get the original random number + 1. I do not think that to add 1 to a number you should define an arrray. It would be simpler to write without using the array

std::rand() % 10 + 1

For example

std::pair<int, int> numbers( std::rand() % 10 + 1, std::rand() % 10 + 1 );
@vlad

ya ya ya,,I understand your point...
thenkz...
Topic archived. No new replies allowed.