Thread: newb error question

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Unhappy newb error question

    hi i have recently started using this site and its help pages i have been folowing it thus far fine but im suck atthis point i do not know what is wrong iy says:

    "expected primary-expression before "else"
    "expected " ; " before "else"

    this error happens for both the lines that begin with "else"
    if you cold tell me why this happens please . thankyou.

    Code:
    #include <stdio.h>
    
    int main()
    {
         int number;
    
         printf("please choose a number 1-5") ;
         scanf( "%d", &number ) ;
         if ( number == 1 || 3 || 5 );{
         printf("i dont like odd numbers\n");
     }
       else if ( number == 2 ); { 
           printf(" i prefer 4 but that will do\n");
     }
       else { 
           printf("YAY! my faverate number\n");
       }
    
      return 0;
    }
    and yes i know this is simple as simple can get but still im only learning

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no semicolon after the if.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Code:
         if ( number == 1 || 3 || 5 )***;***{
         printf("i dont like odd numbers\n");
     }
       else if ( number == 2 )***;*** {
    You shouldn't have the semicolons that I put *** around above. You just use the bracket. I ran it without them and it compiled fine.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    thanks =] ill try remember that even tho this time it dosnt work :s o well ill keep at it lol
    Last edited by diablo_mabbs; 06-15-2008 at 06:01 PM.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Are you still having a problem? Because it should work if you took out those two semicolons.
    Is something else wrong?

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    yes i am it compiles now but it always outputs the odd number text so i just totaly re did it after i read on some more of the tutorials and came up with this with help of my good friend thats at uni =]

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       int number;
    printf("\n\nPlease enter a number between 1-5 or 6 to exit the application \n\t");
        scanf("%d",&number);                   /* READ integer number */
    
        switch (number)                  /* select the number option */
        {
        case 1: printf ("I don't like odd numbers");
            break;
        case 2: printf ("I prefer 4, but that will do");
            break;
        case 3: printf("I don't like odd numbers");
            break;
        case 4: printf ("YAY, my fav number");
            break;
        case 5: printf ("I don't like odd numbers");
            break;
        case 6: exit (1);
            break;
        default: printf("Invalid option selected, you stupid fools\n");
        }
    
    return 0;
    
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could combine 1, 3, 5 for the same case:
    Code:
    case 1: 
    case 3:
    case 5:
        printf("I don't like odd numbers\n");
        break;
    ...
    Avoiding duplication of code is a good idea.

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

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Id also suggest initialising "number" to 0. Might not make much difference now, but can be the cause of subtle bugs in the future and it's a good habit to initialise all your variables.
    Also, don't use exit(1). If you remove the exit(1), the switch will break and you will go to the return 0 statement without anything being printed, which I'm guessing is what it's supposed to do.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Dogpile the newb!
    By lindy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2008, 08:17 AM
  3. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. if your not newb you can help me
    By Klinerr1 in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2002, 12:09 AM