954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with Switch statement ?

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)

#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

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

try changing the

int main (void) ...

to int main () {

should read like this

int main () {

float ......

and no { here.

jhdobbins
Junior Poster
105 posts since Apr 2005
Reputation Points: 10
Solved Threads: 3
 

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:"<

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

I tried that and I still got some errors

I changed it ti this:

#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

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Move this:
float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;

to be above int main()

Move this:
char quantity;

to be after:
int main()
{
char quantity;

Also after switch( quantity), you need a { and after all of the case logic you need another }

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Well, I have gotten down to one error.
missing function header.
Anyone have any ideas?

#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

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This:

int main()

;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
int quantity;

{


should be like this:

int main()
{
   float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
   int quantity;


Then some more errors that can be fixed by enclosing the statements for theswitch within {}. And this:

case '2':


should be this:

case 2:
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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:

#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:

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Three times you've been told not to declare variables between this

int main()


and this

{ // the opening brace of main


Three times you've ignored it. If you implement the suggestions already mentioned in this thread, your errors should be gone.This:

int main()

;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
int quantity;

{


should be like this:

int main()
{
   float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
   int quantity;
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

It's not that I ignored it, I tried it and it and got tons more errors than before I tried it.

#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:

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
It's not that I ignored it, I tried it and it and got tons more errors than before I tried it.

That's good. They were hidden by this one previously. Now the compiler can point them out.Then some more errors that can be fixed by enclosing the statements for the switch within {}.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.You didn't put them outside the function so much as in no-man's-land.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

All Right!

Now we're talkin. I just didn't understand the {} thing in the earlier post.
I'm still on a learning curve, but I sur appreciate the help here.

I thought the comiler caught all the errors and listed them, but now I see that it sometimes will return before checking all of them. Correct?

Thanks, Guy's :)

BandM

bandm
Newbie Poster
19 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
I thought the comiler caught all the errors and listed them, but now I see that it sometimes will return before checking all of them. Correct?

Some errors can cause the whole rest of the program look like a single statement and cut short the number of true errors. Other errors, like a missing semicolon, can produce piles of false error messages.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You