What's wrong with my switch function?

This is a discussion on What's wrong with my switch function? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am very new to C++... in fact I started last night at 10:00. I started so I can ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    What's wrong with my switch function?

    Hi,
    I am very new to C++... in fact I started last night at 10:00. I started so I can make a extra credit function for math (if anyone wants to help me out with that, email me). Anyways, I am having alot of fun, but I can't seem to get "switch" statements to work. Whenever I input the selection of the case I want to run, the window crashes.
    Here's my source code of a simple program I was trying to write:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      int x;
      int y;
      int selection;
      cout<<"Please enter 2 numbers: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"What would you like to do with these numbers:\n";
      cout<<"1. Add them\n";
      cout<<"2. Subtract them\n";
      cout<<"Selection: ";
      cin>> selection;
      switch (selection) {
             case 1:
                  return x + y;
                  break;
             case 2:
                  return x - y;
                  break;
             default:
                  cout<<"Error\n";
                  break;
                  }  
      cin.get();
    }
    I got a simpler switch statement to work, but whenever I try more advanced stuff, it crashes. Can someone help me? Am I missing a measly equal sign or period somewhere?

    Thanks!

    P.S. I'm using Dev-C++ 5
    Last edited by Patton; 11-06-2005 at 12:04 PM.

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Since you're main() function is an int, it will terminate when you return an integer. It returns, though, to the system, not the output stream. You want:
    Code:
    case 1:
        cout << x + y;
        break;
    case 2:
    .
    .
    .
    That way, the result will be printed to the output screen instead.
    Then, when you want the program to terminate, use
    Code:
    return 0;
    at the end of int main() to signifiy a successfull run of the program.
    There is a difference between tedious and difficult.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why don't you show us this "more advanced stuff"

    Of course I have to question why you are returning in your switch statement when it is inside of main(). Since it'll most likely not be zero you are returning you are telling the OS that an error occured in your program.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    thank you, decrypt. But I replaced return with cout and the window still crashes.
    Last edited by Patton; 11-06-2005 at 12:17 PM.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by Patton
    Hi,
    I am very new to C++... in fact I started last night at 10:00.
    There is no reason that you should be doing programs with switch statements if you're that new. Don't read through the tutorials and immediately say "I understand it, next" and go onto the next tutorial. It's clear in your program that you still don't know many of the basics. Take your time with the tutorials. Read the first few and try programs with those for a few days. Then move on.
    Sent from my iPadŽ

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by Patton
    thank you, decrypt. But I replaced return with cout and the window still crashes.
    The window doesn't crash, it just simply closes because that's what Dev-Cpp does when it gets a return on a program. Put a cin.ignore() after you get the input for selection and remember to return an int.
    Sent from my iPadŽ

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Patton
    thank you, decrypt. But I replaced return with cout and the window still crashes.
    If what you mean by the "window crashes" is that the window closes and the cin.get() seems not to have any effect then the answer is:
    there is still a newline in cin's buffer and you could put another cin.get() to the end of your program. But a far better solution would be to call a commandline program from the commandline.
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    It works now with the cin.ignore(), thanks
    I think I tried putting that in there, but the returns were still there so it didn't matter
    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21