# switch and random num generator

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-06-2008
got1sleeve
switch and random num generator
Hey everyone. So i'm relatively new to all of this and have no experience. If it matters i'm using visual studio 2005.

I'm having trouble getting my switch statments to be recognized. Also I'm not very familiar with this random number generator. I get an error message on it so i assume i'm not coding it correctly. Any pointers would be great.

Also the other question i had was how do you keep track of the number correct and incorrect? Would you have to use another for loop?

Here's my code

Code:

``` #include "stdafx.h" #include <iostream> using namespace std; int main() {         int num1, num2, total;         int numpro, ansr;         int protype;         int A,a,B,b,C,c,x,D,d,E,e,F,f;         const int MAXNUM = 100;         int srand(time(NULL));         rand() = num1;         srand() = num2;                 cout << " A - Addition" ;         cout << " B - Subtraction" ;         cout << " C - Multiplication" ;         cout << " D - Division" ;         cout << " E - Random problems" ;         cout << " F - Quit";         cout << "Please select and option from the above menu." ;         cin >> protype;         switch (protype)         {                 case A,a,:                         cout << " How many problems would you like to do?" ;                         cin >> numpro;                                 for (numpro = 0; numpro < MAXNUM; numpro++)                                 {                                         cout << num1 << "+" << num2 << "=" ;                                         cin >> ansr;                                         total = num1 + num2;                                         {                                                 if total == ansr                                                         cout << "Correct!"                                                 else total :! ansr                                                         cout << " Incorrect. The correct answer is: " << total << ;                                                 return 0;                                         }                                 }                 break;                                 case B,b,:                         cout << " Sorry this function has not yet been implemented please try again." ;                         return 0;                         break;                                 case C,c,:                         cout << " Sorry this function ahs not yet been implemented please try again." ;                         return 0;                         break;                                 case D,d:                         cout << " Sorry this function has not yet been implemented, please try again." ;                         return 0;                         break;                         case E,e:                                         cout << " Sorry this function has not yet been implemented, please try again." ;                         return 0;                         break;                 case F:                         return 0;                         break;         }         cout << endl;         return 0; }```
• 01-06-2008
laserlight
For starters,
Code:

`int srand(time(NULL));`
should be:
Code:

`srand(time(NULL));`
The next problem I see is that:
Code:

`case A,a,:`
should be:
Code:

```case 'A': case 'a':```
Similiar changes should be made for the other cases.

Quote:

I get an error message on it so i assume i'm not coding it correctly.
What is the exact error message?

Quote:

Also the other question i had was how do you keep track of the number correct and incorrect?
Use a counter.

Quote:

Would you have to use another for loop?
No. You already have a counter for the number of problems, now you just have another counter for the number of problems solved correctly.
• 01-06-2008
twomers
>> rand() = num1;
>> srand() = num2;
Assignment 'goes' the other way.

num1 = rand();

srand doesn't assign anything. That's just seeding rand to a key.

>> case A,a,:
Doesn't work like this either. You're going to have to do something like:
Code:

```case 'a': case 'A':   // code break;```
Same for the rest of the switch items.
• 01-06-2008
anon
And int protoType should be char protoType, if you want to compare it against characters.
• 01-06-2008
Elysia
Code:

```        int A,a,B,b,C,c,x,D,d,E,e,F,f;         // ...         switch (protype)         {                 case A,a,:                 // ...                 case B,b,:                 // ...                 case C,c,:                 // ...                 case D,d:                 // ...                 case F:                 // ...         }```
So what's the connection here? Are you trying to check for the values stored in those variables or if the user entered A or A, B or b, etc?
• 01-06-2008
got1sleeve
I appreciate the help...i was hoping someone could explain things a bit to me though as well sorry if i was vague about that.

Why does
case a:
case A:

work but
case a,A:

doesn't?

Also this is the second time i've had to try and use a random number generator and well as everyone can see i still don't really understand it. The text book doesn't do much in the way of explaining it for dummies haha so if anyone would like to clarify for me i would greatly appreciate it.

I also fixed the code but like i said before i'm having a problem with the number generator. Also it says "syntax error" identifier total

From past projects it would appear to me that i have the equation of that set up correctly no?
Womers said you don't assign srand() to anything so if that's the case then how does it come up with two numbers for the problem? My first instincts say you would just set num1 and num2 equal to rand() but then that would make them the same number wouldn't it?

Thanks again for the noob patience and help.
• 01-06-2008
tabstop
Quote:

Originally Posted by got1sleeve
I appreciate the help...i was hoping someone could explain things a bit to me though as well sorry if i was vague about that.

Why does
case a:
case A:

work but
case a,A:

doesn't?

Neither works. The label after case must be a (single) integer constant. We can't have a,A because that's not a single value; we can't have a by itself because that's a variable, not a constant. We can have case 'a' (here the integer constant is the encoded value of the character 'a').

Quote:

Also this is the second time i've had to try and use a random number generator and well as everyone can see i still don't really understand it. The text book doesn't do much in the way of explaining it for dummies haha so if anyone would like to clarify for me i would greatly appreciate it.

I also fixed the code but like i said before i'm having a problem with the number generator. Also it says "syntax error" identifier total
I put on the Hat of Guessing and say that you're talking about the line with the if statement? The conditional check inside the if must be enclosed within parentheses:
Code:

`if (whatever == other_thing)`
Quote:

Womers said you don't assign srand() to anything so if that's the case then how does it come up with two numbers for the problem? My first instincts say you would just set num1 and num2 equal to rand() but then that would make them the same number wouldn't it?

Thanks again for the noob patience and help.
Functions are not things. Functions are processes that return a value. (In the case of srand, it is a function that returns no value.) You can't set variables equal to rand(), but you can set them equal to the value that rand returns. And the whole point of rand is that it returns a not-really-random-but-close-enough-for-us number each time you call it.
• 01-07-2008
got1sleeve
[QUOTE]
I put on the Hat of Guessing and say that you're talking about the line with the if statement? The conditional check inside the if must be enclosed within parentheses:
Code:

`if (whatever == other_thing)`
QUOTE]

I apologize for not being clearer. I did mean the total in the beginning of the if statement. However I've used if statements with this program before and haven't needed the parentheses with this compiler. I changed the code but now its saying i have an illegal else without a matching if. As well as the original syntax error identifier total

Code:

```cout << num1 << "+" << num2 << "=" ;                                         cin >> ansr;                                         total = num1 + num2;                                         {                                                 if total == ansr;                                                         cout << "Correct!";                                                 else total != ansr;                                                         cout << " Incorrect. The correct answer is: " << total << ;                                                 return 0;                                         }                                 }```
• 01-07-2008
Elysia
Code:

```        cout << num1 << "+" << num2 << "=" ;         cin >> ansr;         total = num1 + num2;         {                 if (total == ansr)                         cout << "Correct!";                 else if (total != ansr)                         cout << " Incorrect. The correct answer is: " << total << ;                 return 0;         } }```
There's one } too many in the code.
Aside from that, ifs must be enclosed with parenthesises, see corrections in red and they must NOT use an ending ;
The else after an if mean "everything else" and cannot be used with any additional condition. If so, you must use else if.
Have you enabled compiler warnings? Cause ifs with a ; at the end will generate a warning.

Code:

```if (condition) // OK         do_something;```
Code:

```if (condition);         do_something;```
...is the same as...
Code:

`do_something;`
Since you put a ; at the end, the compiler will simply ignore it and the line after the if will be executed regardless if the if is true or not.
You have a lot of syntax error - do you have a book or read any tutorials?
• 01-07-2008
got1sleeve
Quote:

Originally Posted by Elysia
You have a lot of syntax error - do you have a book or read any tutorials?

I have a text book for school but it describes things very briefly and gives one to two examples of things. Not to mention a professor who is head of the computer engineering department and has never taught an entry level programming class.

Thank you again for all the help I think i'm finally getting the hang of some of this. I finally got the program up and runnig the only problem i'm having now is that the num gen is spitting out the same two numbers.
Code:

```#include "stdafx.h" #include <iostream> #include <ctime> // allows rand num gen to choose a "random" number from the clock using namespace std; int main() {         int num1, num2, total;         int numpro, ansr, pro;         char protype; // must match the choice from menus         char A,a,B,b,C,c,x,D,d,E,e,F,f; // char since they are letters not numbers         srand(time(NULL)); // allows for random number generation         num1 = rand();         num1 = rand() % 100;         num2 = rand(); // picks a second random number that will not be the first         num2 = rand() % 100;         cout << "************************" << endl; //menu for options         cout << "* A - Addition        *" << endl;         cout << "* B - Subtraction      *" << endl;         cout << "* C - Multiplication  *" << endl;         cout << "* D - Division        *" << endl;         cout << "* E - Random problems  *" << endl;         cout << "* F - Quit            *" << endl;         cout << "************************" << endl;         cout << "Please select an option from the above menu.  " ;         cin >> protype;         switch (protype)         {         case 'A': // determines what type of problem program will perform         case 'a':                         cout << "\n How many problems would you like to do?  " ;                         cin >> numpro;                                 for (pro = 0; pro < numpro; pro++) //keeps a count until number of problems desired is reached                                 {                                         cout << "\n\n" << num1 << "+" << num2 << "=" ;                                         cin >> ansr;                                         total = num1 + num2;                                         {                                                 if (total == ansr) // verifies input answer is correct                                                         cout << "\n Correct!" << endl;                                                 else if (total != ansr)                                                         cout << "\n Incorrect. The correct answer is: " << total << endl;                                         }                                 }                 break; // end of case                                 case 'B': // functions B through F are not implented per lab assignment                 case 'b':                         cout << "\n Sorry this function has not yet been implemented please try again." << endl;                         return 0;                         break;                                 case 'C':                 case 'c':                         cout << "\n Sorry this function ahs not yet been implemented please try again." <<endl;                         return 0;                         break;                                 case 'D':                 case 'd':                         cout << "\n Sorry this function has not yet been implemented, please try again." << endl;                         return 0;                         break;                 case 'E':                 case 'e':                         cout << "\n Sorry this function has not yet been implemented, please try again." << endl;                         return 0;                         break;                 case 'F':                         return 0;                         break;         }         cout << endl;         return 0; }```
Also someone had previously mentions using a counter to keep track of and then display the number of correct answers and the percentage. Would that be something similar to the

Code:

`For(i=0; i < maxnum; i++)`
I just don't see how you would work another equation into there. I assume it would go after the if else but still within the primary for loop correct?
• 01-08-2008
rogster001
An easy way to achieve this is simply create two new integer variables and add them to your list at the start...

Call them say, ' right' and 'wrong' then place them within your IF and ELSE statements, then increment the total of each depending on whether the answer input by the user is right or wrong. You can then print the totals for each at the end of the program.
you will need to add curly brackets {} to the if and also the else because you are now asking for more than one statement to be read for the condition

Code:

```int right = 0, wrong = 0;                                     /*start of program here....*/                         /*then.....*/             for loop ..........             {         total = num1 + num2;                             if(total == ansr)                   { right++; cout << "etc etc"; }                 else                   { wrong++; cout << "wrong etc etc "; }                             }                                                              /*end of your FOR loop */             /* code to print out the results here eg...*/           /*you got (right) answers correct, and (wrong) answers wrong */```
You could add a master loop to control the whole program and keep running totals for each type of problem played, then give averages for each one and a total success average across all types of problem played.
if you do this remember to reset any variables that need it (except for running totals) before you go back in to play the whole game again, and also remember to keep srand() outside of the master control loop or you will end up seeding it the same or similar all the time.
• 01-08-2008
Elysia
Code:

```int right = 0, wrong = 0; /*start of program here....*/ /*then.....*/ /* for loop .......... */ {         total = num1 + num2;                     if(total == ansr)         {                 right++;                 cout << "etc etc";         }         else         {                 wrong++;                 cout << "wrong etc etc ";         } } /*end of your FOR loop */ /* code to print out the results here eg...*/ /*you got (right) answers correct, and (wrong) answers wrong */```
I take it you're not a master of writing readable code, so I took some time to clean it up a bit. It's important, especially to newbies, to write readable code.
So, the basic rules are: keep indentation consistent - don't indent a little here and everywhere with a varying number of spaces. Just indent once per block level (if, for, etc) so as to know what code belongs to the loop, the if, etc. Also, it's usually not a good idea to write two or more lines in one line, since it makes it harder to read.

Something like above would be more appropriate indented code.

Code:

`char A,a,B,b,C,c,x,D,d,E,e,F,f; // char since they are letters not numbers`
These are not necessary since you don't use them.

Code:

```        num1 = rand();         num1 = rand() &#37; 100;         num2 = rand(); // picks a second random number that will not be the first         num2 = rand() % 100;```
This is not necessary either. You're assigning each variable twice, thus it loses its previous value. Either keep the rand() or rand() % 100 assign for both variables, no more is necessary.
• 01-09-2008
got1sleeve
Quote:

Originally Posted by Elysia

Code:

```        num1 = rand();         num1 = rand() % 100;         num2 = rand(); // picks a second random number that will not be the first         num2 = rand() % 100;```
This is not necessary either. You're assigning each variable twice, thus it loses its previous value. Either keep the rand() or rand() % 100 assign for both variables, no more is necessary.

Originally I wasn't using that buy monday my professor said he reccomends writing it out that way because the first one assigns a rand value to num1 and then the second line keeps it below 100.

I thought it would just be along the lines of
Code:

`num1 = rand() % 100;`
That's basically the same thing right?

Again thanks for all the help. We just got tasked with implementing the other portions of the program except using subfunctions haha should be fun.
• 01-09-2008
Elysia
It doesn't. The first just assigns a random number to num1.
The second line takes the return from rand(), then divides it with 100 and keeps the remainder and then assigns it to the num1. Same for num2, so as you can see, the previous value that you get from the call to rand() is lost because it's overwritten.
• 01-09-2008
robwhit
Perhaps this is more in line with the comments:

Code:

```num = rand(); num &#37;= 100;```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last