| | |
Problem with Switch statement ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hello all :p
I was working on this simple program with a formula for
calculating parallel resistance. I was getting all kinds
of errors at first but I am still getting these few.
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : warning C4518: 'float ' : storage-class or type specifier(s) unexpected here; ignored
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : error C2146: syntax error : missing ';' before identifier 'RPar'
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
ResistancePar.obj - 2 error(s), 1 warning(s)
Any help would be appreciated.
Thanks, BandM
I was working on this simple program with a formula for
calculating parallel resistance. I was getting all kinds
of errors at first but I am still getting these few.
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : warning C4518: 'float ' : storage-class or type specifier(s) unexpected here; ignored
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : error C2146: syntax error : missing ';' before identifier 'RPar'
G:\VC++60\ResistancePar\ResistancePar.cpp(5) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
ResistancePar.obj - 2 error(s), 1 warning(s)
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main (void) float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; { char quantity; cout << "Enter the quantity of resistors" << endl; cout << "A maximum of 6 resistors please" << endl; cin >> quantity; switch (quantity) { case '1': cout << " You must enter at least 2"; break; case '2': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> "Rtwo; Rpar = (Rone * Rtwo) / (Rone + Rtwo); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '3': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> "Rtwo; cout << " Enter the value of the third resistor"; cin >> "Rthree; Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '4': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> "Rtwo; cout << " Enter the value of the third resistor"; cin >> "Rthree; cout << " Enter the value of the fouth resistor"; cin >> "Rfour; Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '5': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> "Rtwo; cout << " Enter the value of the third resistor"; cin >> "Rthree; cout << " Enter the value of the fouth resistor"; cin >> "Rfour; cout << " Enter the value of the fifth resistor"; cin >> "Rfive; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '6': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> "Rtwo; cout << " Enter the value of the third resistor"; cin >> "Rthree; cout << " Enter the value of the fouth resistor"; cin >> "Rfour; cout << " Enter the value of the fifth resistor"; cin >> "Rfive; cout << " Enter the value of the sixth resistor"; cin >> "Rsix; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix); cout <<"The value of parallel resistance is: " << Rpar << "\n"; } return 0; }
Any help would be appreciated.
Thanks, BandM
Also,
You should probably make quantity an 'int', and then in your switch, it would be: (Note no ' ' around the number)
case 1:
cout << " You must enter at least 2";
break;
case 2:
cout << " Enter the value of the first resistor";
..etc..
Also, you should put a default case case...
default:
cout<<"In valid number:"<<quantity<<endl;
You should probably make quantity an 'int', and then in your switch, it would be: (Note no ' ' around the number)
case 1:
cout << " You must enter at least 2";
break;
case 2:
cout << " Enter the value of the first resistor";
..etc..
Also, you should put a default case case...
default:
cout<<"In valid number:"<<quantity<<endl;
I tried that and I still got some errors
I changed it ti this:
Now I am just getting this:
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
Thanks BandM
I changed it ti this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() ;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; char quantity; { cout << "Enter the quantity of resistors" << endl; cout << "A maximum of 6 resistors please" << endl; cin >> quantity; switch (quantity) case '1': cout << " You must enter at least 2"; break; case '2': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; Rpar = (Rone * Rtwo) / (Rone + Rtwo); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '3': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '4': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '5': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case '6': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; cout << " Enter the value of the sixth resistor"; cin >> Rsix; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix); cout <<"The value of parallel resistance is: " << Rpar << "\n"; return 0; }
Now I am just getting this:
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
Thanks BandM
Well, I have gotten down to one error.
missing function header.
Anyone have any ideas?
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
BandM :o
missing function header.
Anyone have any ideas?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() ;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; int quantity; { cout << "Enter the quantity of resistors" << endl; cout << "A maximum of 6 resistors please" << endl; cin >> quantity; switch (quantity) case 1: cout << " You must enter at least 2"; break; case '2': cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; Rpar = (Rone * Rtwo) / (Rone + Rtwo); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 3: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 4: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 5: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 6: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; cout << " Enter the value of the sixth resistor"; cin >> Rsix; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix); cout <<"The value of parallel resistance is: " << Rpar << "\n"; return 0; }
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
BandM :o
This: should be like this: Then some more errors that can be fixed by enclosing the statements for the switch within {}. And this: should be this:
int main()
;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
int quantity;
{int main()
{
float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
int quantity;case '2':
C++ Syntax (Toggle Plain Text)
case 2:
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
OK, I fixed the '2' problem
when I take the ; from in front of float
I get :
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : warning C4518: 'float ' : storage-class or type specifier(s) unexpected here; ignored
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : error C2146: syntax error : missing ';' before identifier 'Rpar'
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
ResistancePar.obj - 2 error(s), 1 warning(s)
Like this:
When I put the ; back in front ;float
I get:
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
:rolleyes:
when I take the ; from in front of float
I get :
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : warning C4518: 'float ' : storage-class or type specifier(s) unexpected here; ignored
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : error C2146: syntax error : missing ';' before identifier 'Rpar'
G:\VC++60\ResistancePar\ResistancePar.cpp(6) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
ResistancePar.obj - 2 error(s), 1 warning(s)
Like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; int quantity; { { cout << "Enter the quantity of resistors" << endl; cout << "A maximum of 6 resistors please" << endl; cin >> quantity; switch (quantity) } case 1: cout << " You must enter at least 2"; break; case 2: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; Rpar = (Rone * Rtwo) / (Rone + Rtwo); cout
When I put the ; back in front ;float
I get:
Compiling...
ResistancePar.cpp
G:\VC++60\ResistancePar\ResistancePar.cpp(9) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
ResistancePar.obj - 1 error(s), 0 warning(s)
:rolleyes:
Three times you've been told not to declare variables between this
and this Three times you've ignored it. If you implement the suggestions already mentioned in this thread, your errors should be gone.
C++ Syntax (Toggle Plain Text)
int main()
C++ Syntax (Toggle Plain Text)
{ // the opening brace of main
•
•
•
•
Originally Posted by Dave Sinkula
This:should be like this:int main() ;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; int quantity; {int main() { float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; int quantity;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
It's not that I ignored it, I tried it and it and got tons more errors than before I tried it.
Doesn't putting the variable outside the brackets just make them global?
I wouldn't think that it should effect anything, then I am just a greenhorn trying to learn.
:mrgreen:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix; int quantity; cout << "Enter the quantity of resistors" << endl; cout << "A maximum of 6 resistors please" << endl; cin >> quantity; switch (quantity) case 1: cout << " You must enter at least 2"; break; case 2: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; Rpar = (Rone * Rtwo) / (Rone + Rtwo); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 3: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 4: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 5: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive); cout <<"The value of parallel resistance is: " << Rpar << "\n"; break; case 6: cout << " Enter the value of the first resistor"; cin >> Rone; cout << " Enter the value of the second resistor"; cin >> Rtwo; cout << " Enter the value of the third resistor"; cin >> Rthree; cout << " Enter the value of the fouth resistor"; cin >> Rfour; cout << " Enter the value of the fifth resistor"; cin >> Rfive; cout << " Enter the value of the sixth resistor"; cin >> Rsix; Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix); cout <<"The value of parallel resistance is: " << Rpar << "\n"; return 0; } Compiling... ResistancePar.cpp G:\VC++60\ResistancePar\ResistancePar.cpp(21) : error C2043: illegal break G:\VC++60\ResistancePar\ResistancePar.cpp(22) : error C2046: illegal case G:\VC++60\ResistancePar\ResistancePar.cpp(29) : error C2043: illegal break G:\VC++60\ResistancePar\ResistancePar.cpp(30) : error C2046: illegal case G:\VC++60\ResistancePar\ResistancePar.cpp(39) : error C2043: illegal break G:\VC++60\ResistancePar\ResistancePar.cpp(40) : error C2046: illegal case G:\VC++60\ResistancePar\ResistancePar.cpp(51) : error C2043: illegal break G:\VC++60\ResistancePar\ResistancePar.cpp(52) : error C2046: illegal case G:\VC++60\ResistancePar\ResistancePar.cpp(65) : error C2043: illegal break G:\VC++60\ResistancePar\ResistancePar.cpp(66) : error C2046: illegal case Error executing cl.exe. ResistancePar.obj - 10 error(s), 0 warning(s)
Doesn't putting the variable outside the brackets just make them global?
I wouldn't think that it should effect anything, then I am just a greenhorn trying to learn.
:mrgreen:
![]() |
Similar Threads
- switch statement on String in Java (Java)
- Switch Statement (Java)
- switch statement (C)
- Switch Statement, Fall-Through. (C)
- Problems with switch statement (C++)
- Problems with switch statement (C++)
Other Threads in the C++ Forum
- Previous Thread: strings in c
- Next Thread: bin trees
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






