Thread: noob question re: switch case & loops

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    noob question re: switch case & loops

    So I'm brand new at this C++ business, having just spent some time learning over the past few days on my spare time. I'm trying to write a very simple menu program that will take your selection, input, and then output a simple mathematical calculation for you.

    I'm running into problems while compiling (using MS Visual Studio 2008)..

    Does anyone see what I'm doing wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int multiply(int x, int y );
    int divide(int x, int y );
    int subtract(int x, int y );
    	
    int main()
    {
      
      int l;
      int x;
      int y;
      int input;
      
      while ( input != 1, 2, 3, 4 ){
    	  if ( l > 0 ) 
    	  {
    		  cout>>"You have pressed an invalid key, please choose one of the selections below\n";
    	  }
    
      cout<<"1. Multiply\n";
      cout<<"2. Divide\n";
      cout<<"3. Subtract\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      
      
      switch ( input ) {
      
      case 1:            // Note the colon, not a semicolon
        cout>>"please enter the first number to be multiplied" ;
    	cin<< x ;
    	cin.ignore();
    	cout>>"please enter the second number to be multiplied" ;
    	cin<< y ;
        cin.ignore();
    	cout>>"the product of those two numbers is:">>multiply ( x ,y ) >>"\n"; 
    	break;
      
      case 2:            // Note the colon, not a semicolon
        cout>>"please enter the first number to be divided" ;
    	cin<< x ;
    	cin.ignore();
    	cout>>"please enter the second number to be divided" ;
    	cin<< y ;
        cin.ignore();
    	cout>>"the product of those two numbers is:">>divide ( x ,y ) >>"\n";
        break;
      
      case 3:            // Note the colon, not a semicolon
        cout>>"please enter the first number to be subtracted" ;
    	cin<< x ;
    	cin.ignore();
    	cout>>"please enter the second number to be subtracted" ;
    	cin<< y ;
        cin.ignore();
    	cout>>"the product of those two numbers is:">>subtract ( x ,y ) >>"\n";
        break;
      
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      
    
      }
      l++;
      endl;
     }
      cin.get();
    }
    int multiply(int x, int y )
    {
    	return x * y;
    }
    
    int divide(int x, int y )
    {
    	return x / y;
    }
    
    int subtract (int x, int y )
    {
    	return x - y;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you posted the error message(s) you get when you compile.

    Here are some other comments on your code that you may want to check.
    Code:
    while ( input != 1, 2, 3, 4 ){
    doesn't quite do what you want. It does: a compare to see if input != 1 - then throw away the result. Then "test" 2 to see if it's zero - throw away the result, then test 3 to see if it's zero, and finally test 4 to see if it's zero. It is not, so the while() is allowed to continue. Since 4 is ALWAYS not zero, it's ALWAYS going to continue.

    Also, the first time the while is tested, input is undefined, because you have not yet given it a value.

    Code:
    	  if ( l > 0 ) 
    	  {
    		  cout>>"You have pressed an invalid key, please choose one of the selections below\n";
    	  }
    You have not set l to anything here (the first time around), so there is a chance that it's > 0 the first time around. [Side note: Using the letter l for a variable name is not conidered good style, as it's very easy to confuse l and 1 in some typefaces, likewise for big O and 0 - those should be avoided].

    Code:
      endl;
    doesn't do anything meaningfull.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This is not valid syntax:
    Code:
     while ( input != 1, 2, 3, 4 )
    In the future if you are getting compiler errors, post those please. If you are getting runtime errors, please describe the error(s).
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by matsp View Post
    [Side note: Using the letter l for a variable name is not conidered good style, as it's very easy to confuse l and 1 in some typefaces, likewise for big O and 0 - those should be avoided].

    Mats
    Dino says Ditto.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    When you have errors, and you're looking for assistance in diagnosing these errors, you should provide them in your request for help.

    First, your arrows are facing the wrong way all over the place
    Code:
    // Send data to cout:
    cout << data;
    
    // Read data from cin:
    cin >> data;

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by Dino View Post
    Dino says Ditto.
    rags says right on.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    This is not valid syntax:
    Code:
     while ( input != 1, 2, 3, 4 )
    In the future if you are getting compiler errors, post those please. If you are getting runtime errors, please describe the error(s).
    Actually, I think it is valid.
    Code:
    void foo()
    {
      int x;
      while(x != 0, 1) ;
    }
    compiles in gcc 3.4, but with -Wall it says
    Code:
    g.c:4: warning: left-hand operand of comma expression has no effect
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, OK. It compiles with a warning, yes. But it's not doing what the OP intended, that being checking input to be not equal to 1, 2, 3 or 4. [spin alert] So from that perspective, it's not logically valid syntax.[/spin alert]
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Well, OK. It compiles with a warning, yes. But it's not doing what the OP intended, that being checking input to be not equal to 1, 2, 3 or 4. [spin alert] So from that perspective, it's not logically valid syntax.[/spin alert]
    Sure, I pointed out that it doesn't do what the OP wanted in my first post too - it is definitely "wrong in this context", but the compiler will accept it. I personally am not a fan of the compilers ability to accept a comma operator here there and everywhere - it is rarely meaningfull, but that's the language, and we don't want to solve that discussion here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Rather than testing for four separate values, you might use a separate variable to control the loop.

    Code:
    bool im_done = false;
    
    while (!im_done)
    {
         ...
         switch (input) {
             ...
            default:
                im_done = true;
        }
    }
    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).

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    you guys are great! but check out this one

    ok.. with a little help from you guys, I figured it out! It (almost) works as-designed. Not too shabby for day 2 of c++...

    But here's the thing. If you choose to multiply a number, and you enter an 11 digit number, the program goes all wacky on me. Thoughts anyone?

    p.s. I would have included the compiler errors from visual studio if there wasn't about 1000 lines of it. (Don't ask, I don't know why either)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int multiply(int x, int y );
    int divide(int x, int y );
    int subtract(int x, int y );
    	
    int main()
    {
      
      int exit;
      int x;
      int y;
      int input;
      
      while (exit != 1)
      {
      cout<<"1. Multiply\n";
      cout<<"2. Divide\n";
      cout<<"3. Subtract\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      cin.ignore();
     
      if ( input > 4 )
          {
      cout<<"You have pressed an invalid key, please choose one of the selections below\n";
    	  }
    
      switch ( input ) 
          {
      
      case 1:            // Note the colon, not a semicolon
    	cout<<"please enter the first number to be multiplied (or enter an 11 digit number to see something wacky) :\n" ;
    	cin>> x ;
    	cin.ignore();
    	cout<<"please enter the second number to be multiplied:\n" ;
    	cin>> y ;
        cin.ignore();
    	cout<<"the product of those two numbers is:"<<multiply ( x ,y ) <<"\n"; 
    	break;
      
      case 2:            // Note the colon, not a semicolon
    	  cout<<"please enter the first number to be divided:\n" ;
    	cin>> x ;
    	cin.ignore();
    	cout<<"please enter the second number to be divided:\n" ;
    	cin>> y ;
        cin.ignore();
    	cout<<"the product of those two numbers is:"<<divide ( x ,y ) <<"\n";
        break;
      
      case 3:            // Note the colon, not a semicolon
    	  cout<<"please enter the first number to be subtracted:\n" ;
    	cin>> x ;
    	cin.ignore();
    	cout<<"please enter the second number to be subtracted:\n" ;
    	cin>> y ;
        cin.ignore();
    	cout<<"the product of those two numbers is:"<<subtract ( x ,y ) <<"\n";
        break;
      
      case 4:            // Note the colon, not a semicolon
        cout<<"Are you sure you want to quit?\n";
    	cout<<"1. Exit\n";
    	cout<<"2. Keep playing with crappy math tool for some reason\n";
    	cout<<"Only the gods await your decision:";
    	cin>> exit;
        break;
          }
      }
    cin.get();
    }
    int multiply(int x, int y )
    {
    	return x * y;
    }
    
    int divide(int x, int y )
    {
    	return x / y;
    }
    
    int subtract (int x, int y )
    {
    	return x - y;
    }

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I would have included the compiler errors from visual studio if there wasn't about 1000 lines of it.
    Post the first five errors.

    Also, enable warnings. exit is uninitialized when you test it the first time, and you should get a warning about that. (Also, exit is the name of a standard library function and thus a bad choice for a variable name.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Plus there is no such thing as "an 11 digit int". ints go up to 2147483647 (on most systems, and I would guess yours as well). If you change "int" to "long" you may or may not get a larger number -- again depending on the system.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from CornedBee's suggestion of posting the first few errors (5-10 or so), I have two suggestions:
    1. Write a small piece of code at a time - compile it, fix any errors you have in that. That way, you rarely get so many errors that you can't figure out what was wrong.
    2. When fixing errors, fix one (or two if it's really obvious) each time - then compile again. Quite often when you get LOTS of errors, it's all caused by the compiler getting very confused about the line the first error is on, and it's then just going off in the wrong direction. So fixing the first error [and note that if it says missing semicolon on line X, then it's usually line X-1 that is missing a semicolon].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. another newbie question (switch...case)
    By Terran in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2008, 04:07 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Switch case, with ofstream <- noob
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2005, 01:12 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM