HELP!!! switch errors!!!

This is a discussion on HELP!!! switch errors!!! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int choice; do{ printf("1 = hi\n"); printf("2 = hello\n"); printf("3 = bye\n"); printf("4 = ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12

    HELP!!! switch errors!!!

    Code:
    #include <stdio.h>
    
    int main() {
        int choice;
        
        do{
            printf("1 = hi\n");
            printf("2 = hello\n");
            printf("3 = bye\n");
            printf("4 = see you later\n");
            printf("Enter your answer<1-4>:\n");
            scanf("%d", &choice);
            
            switch(choice)  {
              case 1:
                   printf("Hi!");
                   break;
              case 2:
                   printf("Hello");
                   break;
              case 3:
                   printf("Bye");
                   break;
              case 4:
                   printf("See you later!");
                   break;
              default:
                      printf("Out bound");
                      }
              system("pause");
              return 0;
              }
    where is the error?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    well you should start by saying the error that YOU get, reading posts is not a guessing game for forum members.

    where is the 'while' from your loop or did you just forget to paste that in? is that your problem it does not compile?
    switch looks ok apart from that
    Last edited by rogster001; 01-11-2010 at 10:56 AM.

  3. #3
    Me
    Join Date
    Jul 2006
    Posts
    71
    You also don't have a closing bracket on your main() function. You should put system("pause") and return 0 outside of the switch/case. Otherwise, the only way the program will end properly is if the user inputs an invalid value.

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    also this looks like c code. meow.
    getting some type of backward compatible error too ?
    Last edited by kryptkat; 01-11-2010 at 12:05 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by mkmk007 View Post
    where is the error?
    Somewhere between the keyboard and the chair, as they didn't make it into the first post!

    relyt_123: You've suffered at the hands of poor indentation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Me
    Join Date
    Jul 2006
    Posts
    71
    Along with the missing bracket, you also fail to include the correct library for system, as well as don't have the latter part of the do-while loop.

    Edit: On the other hand, for what you have so far, a do-while loop is pointless (that is unless you either set up a way to have the loop break, or place the system("pause") and return value outside of the loop).
    Last edited by relyt_123; 01-12-2010 at 05:58 AM.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    relyt_123: You've suffered at the hands of poor indentation.
    As noted (obliquely) here, the bracket for main is not missing, its just been indented so it looks like the end of the while loop, its the while loop close brace that has not been typed/pasted, although this is relative to how you look at it i suppose
    Last edited by rogster001; 01-12-2010 at 06:50 AM.

  8. #8
    Me
    Join Date
    Jul 2006
    Posts
    71
    Perhaps it's not there in the code, and part of the reason he's getting errors. I'm simply making a logical assumption, as all of the brackets are definitely not there.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Okay to clear this up. There is a missing brace.
    main, do, and switch are the lines the open braces appear on, and there are only two closing braces.
    However, the pause and the return are not inside the switch statement.

    Again, the cause of the problem stems directly from incorrect indentation. I can never understand why some people don't use proper indentation right from the get go. It takes all of what 5 seconds to work out that e.g. braces should line up vertically, the first time you see any code from a tutorial or book.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 07:06 AM
  3. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM

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