CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: C++ assistance for my beginner class

#### Hybrid View

1. Junior Member
Join Date
May 2013
Posts
3

## C++ assistance for my beginner class

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

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

srand(unsigned(time(0)));

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

cout<<"Correct!\n";
count++;
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

2. ## Re: C++ assistance for my beginner class

What kind of assistance do you need with this?

3. Junior Member
Join Date
May 2013
Posts
3

## Re: C++ assistance for my beginner class

Originally Posted by Igor Vartanov
What kind of assistance do you need with this?
Igor I need to ensure that I have the program running exactly as instructed. I need the full credit for this assignment. Please feel free to add or change some of the lines on my code.

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: C++ assistance for my beginner class

Originally Posted by jesse saiz21
Igor I need to ensure that I have the program running exactly as instructed. I need the full credit for this assignment. Please feel free to add or change some of the lines on my code.
How can you get the credit if you're asking an expert or professional programmer to complete the assignment?

Regards,

Paul McKenzie

5. Junior Member
Join Date
May 2013
Posts
3

## Re: C++ assistance for my beginner class

Im not asking for completion Paul, I just need some guidance through what I have already. I am a beginner with all the intentions of learning this language and becoming a master programmer, however I am stuck here with what I have. It doesnt compile for me and I have no idea where else to get help from. I could easily ask another student to just let me have thier code. I thought this was a site to ask for, and get help.

6. ## Re: C++ assistance for my beginner class

Originally Posted by jesse saiz21
Im not asking for completion Paul, I just need some guidance through what I have already. I am a beginner with all the intentions of learning this language and becoming a master programmer, however I am stuck here with what I have. It doesnt compile for me and I have no idea where else to get help from. I could easily ask another student to just let me have thier code. I thought this was a site to ask for, and get help.
See my post #3 for initial help. If you have changed your code as per my suggestions and it still doesn't compile, repost your code and we'll give further guidance.

http://forums.codeguru.com/showthrea...ork-assignment

While we will provide help and guidance for you with code you have already written, we will not write it for you.

7. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: C++ assistance for my beginner class

Originally Posted by jesse saiz21
however I am stuck here with what I have. It doesnt compile for me and I have no idea where else to get help from.
Yet you didn't state one compiler error.

If you get a compiler error, state what they are by copying and pasting the error message in the edit window. Don't just say "it doesn't compile". Why? Here are the reasons:

1) We may not be using the same compiler as you're using, and the messages may be different (or may not show up at all).

2) You may have only copied and pasted a snippet, and snippets do not compile due to lack of headers, variable declarations, missing functions, etc.

3) If we see the compilation error, we will let you know why the error exists, and not just fix the error for you. Learning how to fix your own syntax/compilation errors is part of learning C++.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; May 6th, 2013 at 01:37 PM.

8. ## Re: C++ assistance for my beginner class

Originally Posted by jesse saiz21
Igor I need to ensure that I have the program running exactly as instructed.
To be sure the program is running as instructed you have to analyze your requirements and make a plan how you make sure not only about normal cases but error cases/input as well. Please don't get me wrong, but I cannot see a way how to assist you in this. Sorry, I'm not a teacher, I'm just an engineer. Try to turn your questions into engineer problems, and we'll talk.
I need the full credit for this assignment. Please feel free to add or change some of the lines on my code.
Please be sure I never hesitate to provide code snippets or full samples, but I always do that when I have at least a minimal interest to the problem. I like to solve problems, but I hate being involved in tedious teaching.
Last edited by Igor Vartanov; May 6th, 2013 at 02:19 PM.

9. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: C++ assistance for my beginner class

What is this?

num = (rand()&#37;8)+ 2;

10. ## Re: C++ assistance for my beginner class

Originally Posted by Igor Vartanov
Sorry, I'm not a teacher, I'm just an engineer. Try to turn your questions into engineer problems, and we'll talk.
Please be sure I never hesitate to provide code snippets or full samples, but I always do that when I have at least a minimal interest to the problem. I like to solve problems, but I hate being involved in tedious teaching.
I used to teach beginner/intermediate c++ which I quite enjoyed so I try to teach a bit when answering homework assignment posts if I have time.

11. ## Re: C++ assistance for my beginner class

Before you post, please format your code with proper indentations etc. Also please use code tags (Go Advanced, select code and click '#').

Looking at the code you have posted, there are a couple of problems you need to look at before you enhance your solution

1) rand generates a random number between 0 and RAND_MAX (which is 0x7fff on my system). Therefore to get a range between 1 and 10 as asked, you first need a range between 0 and 9 then add 1. So you need

Code:
`num = (rand() % 10) + 1;`
Code:
```if (answer1 == user)
cout<<"Correct!\n";

count++;

cout<<"Wrong! -> "<<num<<" x "<<num2<<" = "<<answer1;```
I've re-formatted these if statements so you can see how they work. If the answer is correct then only the cout statement is performed. The count++ statement is always performed, irrespective of whether the answer is right or wrong. This is because the if statement only works on the statement following it. If more than one statement is needed, then a compound statement should be used. Also, if answer1 is not equal to user, then rather than another if statement, an else clause could be used with the first if statement. Thus the above could be coded as

Code:
```if (answer1 == user) {
cout << "Correct!\n";
count++;
} else
cout << "Wrong! -> " << num << " x " << num2 << " = " << answer1;```
You next need at the start of the program to ask the user to enter the number of questions to ask. This is easy by using cout >> ...then a cin >> ... The variable used for the cin will then be used in the for loop.

If you don't want the same pair of numbers generated as previously, then you will need to save these numbers in a couple of variables and have a loop (do?) that loops until the new numbers generated are different to the previous.

For the 'play again' question, this is just a cout <<... followed by a cin >>... to a char variable. Put a loop (do?) round the code you have so far so that the loop continues if the user enters 'y' or terminates if 'n' etc.

Before you start with displaying tables, I would get the code so far to work as expected and then add code for displaying the tables.

If you have further questions, post your revised code and we'll provide further guidance.

PS You have designed your program first before you started to code, haven't you?
Last edited by 2kaud; May 6th, 2013 at 06:22 AM.

12. ## Re: C++ assistance for my beginner class

&#37; is html encoded %.
Last edited by Igor Vartanov; May 6th, 2013 at 03:26 PM.

13. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: C++ assistance for my beginner class

Originally Posted by Igor Vartanov
% is html encoded %.
Not sure C++ compilers will see it that way.

14. ## Re: C++ assistance for my beginner class

Originally Posted by GCDEF
Not sure C++ compilers will see it that way.
That statement's wrong anyhow as per my post #3.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•