Thread: unwanted number in switch

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    unwanted number in switch

    Hi

    I got a switch statement which, as well as the sentance it prints out, it also prints out the switch number for example
    hello
    2
    here is the function code for the switch statment,
    What am I doing wrong for that to print out?
    Code:
    int rightremark()
    {
    int statements;
    
    				statements = 1 + rand() % 4;
    
    				switch (statements)
    				{
    
    				case 1: 
    					cout << "Very good" << endl;
    					
    					break;
    				case 2 :
    					cout << "Excellent" << endl;
    					
    					break;
    				case 3:
    					cout << "Nice Work"<< endl;
    					break;
    				case  4:
    					cout << "Keep up the good work" << endl;
    					break;
    					
    				}
    	return statements;
    Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I notice you return an int. Have you checked the calling function to see if it prints the return value of rightremark?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Hi, sorry I'm a little confused, I get one of my statements printed, so I thought that did mean it was printing the return value of rightremark. Mabey it's not and I am not realising due to my lack of experience, here is some of my programme plus the calling function
    Code:
    while(number != -1)
    	{
    		cout << " Please answer this question or press -1 to exit"  << endl;
    
    			first = 1 + rand() % 12;
    			second = 1 + rand() % 12;
    
    			cout << " What is " << first << " times " << second << " ?" << endl;
    				answer = first * second;
    			cin >> number;
    			if( number == answer ){
    			
    			ok = rightremark();
    				cout << ok << endl;}
    Thank you.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Looks like it does to me:
    Code:
    ok = rightremark();
    cout << ok << endl;
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Have you created the variables in the above snippet before?

    Also, if ok is of type int..your attempting to output 'ok', which isn't a type of *char..it's a type of string. I don't know whether you wish to output the string, or use the function _to_ output the string. But right now if you just want the case number then the code seems to work.

    But if you do want to use that function and output the string, you have to pass the value to the function..and not assign it to a variable..that make sense?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Yeah thanks, I worked that out, with a little experiement, all I needed to do was not put in
    cout << ok << endl;
    and now I dont get the number

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM