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:
Quote:
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)
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.
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'
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;"
This semicolon is misplaced.
"case else" doesn't exist. It's called "default" and needs a colon like all the case marks.
Re: Whats wrong with this code?
thanks, (case else = vb :D )
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.
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.
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)
Re: Whats wrong with this code?
Quote:
'x' : undeclared identifier
Ever seen what happens when you enable Option Strict in VB?
Quote:
'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.
Quote:
'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.
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...
Re: Whats wrong with this code?
Sample code directed by CornedBee.
VB Code:
#include <iostream>
using namespace std;
int main(){
int x; // declare x first
cin>>x;
switch(x){
case 1: // case not Case
cout<<"x = 1";
break;
case 2:
cout<<"x = 2";
break;
case 3:
cout<<"x = 3";
break;
default:
cout<<"x = ";
cout<<x; // not n coz we're dealing with x
}
cout<<endl;
return 0;
}