Results 1 to 10 of 10

Thread: Whats wrong with this code?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Whats wrong with this code?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    cint >> x;
    {
    	switch (x) {;
    	 Case 1:
    		cout << "X = 1";
    		Case 2:
    			Cout << "x=2";
    				Case 3:
    					cout << "x =3";
    					Case else
    						cout << "x =";
    					cout << n;
        }
    	system( "pause" )
    		 return 0;
    }
    i get this:
    ompiling...
    Cpp1.cpp
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(6) : error C2146: syntax error : missing ';' before identifier 'cint'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(6) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    Cpp1.exe - 2 error(s), 0 warning(s)

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Whats wrong with this code?

    Its before the opening '{' following the 'main' function. Plus i think you want 'cin' not 'cint'

    Don't forget that with a case statement, the code will run all code from the first matching case statement. For example if x is 1 it'll run all of the case statements.
    Use the 'break' keyword to get out of the switch statement after you've done stuff specific to that case.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Whats wrong with this code?

    Code:
    #include <iostream> //<iostream.h> is deprecated
    
    using namespace std;
    
    int main()
    cin >> x;
    {
    	switch (x) {;
    	 Case 1:
    		cout << "X = 1";
    		break;
    		Case 2:
    			Cout << "x=2";
    			break;
    				Case 3:
    				 cout << "x =3";
    					break;
    					Case else
    						cout << "x =";
    							cout << n;
    								break;
        }
    	system( "pause" )
    		 return 0;
    }
    thanks for the break, but can you explain this, or show me how youd do it:
    ts before the opening '{' following the 'main' function. Plus i think you want 'cin' not 'cint'

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Whats wrong with this code?

    C++ is case-sensitive. You must write "case".

    Code:
    int main()
    cin >> x;
    {
    You've got the order of the lines mixed up here. The "{" should be before the "cin >> x;"

    Code:
    	switch (x) {;
    This semicolon is misplaced.

    Code:
    Case else
    "case else" doesn't exist. It's called "default" and needs a colon like all the case marks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Whats wrong with this code?

    thanks, (case else = vb )

    Code:
    #include <iostream> //<iostream.h> is deprecated
    
    using namespace std;
    
    int main();
    {
    cin >> x;
    
    switch(x){ ;
    	 Case 1:
    		cout << "X = 1";
    		break;
    		Case 2:
    			Cout << "x=2";
    			break;
    				Case 3:
    				 cout << "x =3";
    					break;
    					default;
    						cout << "x =";
    							cout << n;
    								break;
        }
    	system( "pause" )
    		 return 0;
    }
    Code:
    Compiling...
    Cpp1.cpp
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(6) : error C2447: missing function header (old-style formal list?)
    Error executing cl.exe.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Whats wrong with this code?

    You didn't remove the semicolon as I told you, and there's another wrong one at the end of the main() line.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Whats wrong with this code?

    Code:
    #include <iostream> //<iostream.h> is deprecated
    
    using namespace std;
    
    int main()
    {
    cin >> x;
    
    switch(x){
    	 Case 1:
    		cout << "X = 1";
    		break;
    		Case 2:
    			Cout << "x=2";
    			break;
    				Case 3:
    				 cout << "x =3";
    					break;
    					default;
    						cout << "x =";
    							cout << n;
    								break;
        }
    	system( "pause" );
    		 return 0;
    }
    Compiling...
    Cpp1.cpp
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(7) : error C2065: 'x' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(10) : error C2065: 'Case' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(10) : error C2143: syntax error : missing ';' before 'constant'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(10) : error C2143: syntax error : missing ';' before ':'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(13) : error C2143: syntax error : missing ';' before 'constant'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(13) : error C2143: syntax error : missing ';' before ':'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(16) : error C2143: syntax error : missing ';' before 'constant'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(16) : error C2143: syntax error : missing ';' before ':'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(19) : error C2143: syntax error : missing ':' before ';'
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(21) : error C2065: 'n' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(23) : warning C4065: switch statement contains 'default' but no 'case' labels
    Cpp1.exe - 10 error(s), 1 warning(s)

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Whats wrong with this code?

    'x' : undeclared identifier
    Ever seen what happens when you enable Option Strict in VB?

    'Case' : undeclared identifier
    It's a matter of case. (BTW, that's the second time I need to tell you this.)

    All C2143 errors are follow-ups.

    'n' : undeclared identifier
    Big surprise, when you called the variable 'x' earlier.

    I think you need to unlearn VB before you can learn C++. I also think you need to learn to read the answers to your questions and apply what is in there - everything, not just a single thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Whats wrong with this code?

    im sorry, didnt see this part at all

    C++ is case-sensitive. You must write "case".

    ill stop buggin you, you seem really angry/ornery...

  10. #10
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Whats wrong with this code?

    Sample code directed by CornedBee.
    VB Code:
    1. #include <iostream>
    2. using namespace std;
    3. int main(){
    4.     int x; // declare x first
    5.     cin>>x;
    6.     switch(x){
    7.         case 1: // case not Case
    8.             cout<<"x = 1";
    9.             break;
    10.         case 2:
    11.             cout<<"x = 2";
    12.             break;
    13.         case 3:
    14.             cout<<"x = 3";
    15.             break;
    16.         default:
    17.             cout<<"x = ";
    18.             cout<<x; // not n coz we're dealing with x                                              
    19.     }
    20.     cout<<endl;
    21.     return 0;
    22. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width