Thread: Switch stat error

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    Switch stat error

    Can someone tell me why I can't compile this? I'm using Dev-C++ and I recieve this error:

    9 D:\Mijn Documenten\My Projects\Code\cpp\hw3.cpp expected primary-expression before "int"
    Code:
    #include <iostream>
    #define x 3
    
    using namespace std;
    
    int main(int argc, char *argv[])
    
    {
          int x; //de waarde die we gaan controleren
          
          switch(x)	//begin van het switch statement
          {
                    case 1:	//als x de waarde 1 bevat doe dit
                         cout <<" x bevat de waarde 1";
    
                    case 2:	//als x de waarde 2 bevat doe dit
                         cout <<" x bevat de waarde 2";
    
                    case 3:	//als x de waarde 3 bevat doe dit
                         cout <<" x bevat de waarde 3";
    
                    case 4:	//als x de waarde 4 bevat doe dit
                         cout <<" x bevat de waarde 4";
    
                    default:	//als x geen van deze waarden bevat doe dit
                         cout <<" Ik zou niet weten welke waarde x bevat";
                                     
                    cin.get();
           }
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    since x is defined as 3, the preprocessor will replace all occurences of x with 3, like this
    Code:
    #define x 3
    
    int 3;

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    I know, but after I try to fix it:

    Code:
    int x = 3; //de waarde die we gaan controleren
    It does compile, and when I run it:

    x bevat de waarde 3 x bevat de waarde 4 Ik zou niet weten welke waarde x bevat
    and cout's all of the cases, or whatever

    Edit: I did remove
    Code:
    #define x 3
    as well ofcourse.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need break statements for each case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM