Thread: switch and random num generator

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.

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

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

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And int protoType should be char protoType, if you want to compare it against characters.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    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.
    Last edited by got1sleeve; 01-06-2008 at 10:59 PM. Reason: added question

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by got1sleeve View Post
    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').

    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)
    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.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    [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;
    					}
    				}

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Elysia View Post
    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?

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Last edited by Elysia; 01-08-2008 at 12:05 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Elysia View Post

    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Perhaps this is more in line with the comments:

    Code:
    num = rand();
    num &#37;= 100;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  2. Random math problems
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2008, 11:20 PM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 12:18 PM