Problem with Switch statement ?

Reply

Join Date: Jun 2005
Posts: 19
Reputation: bandm is an unknown quantity at this point 
Solved Threads: 0
bandm's Avatar
bandm bandm is offline Offline
Newbie Poster

Problem with Switch statement ?

 
0
  #1
Aug 10th, 2005
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)

  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int main (void)
  5.  
  6. float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
  7.  
  8. {
  9. char quantity;
  10.  
  11. cout << "Enter the quantity of resistors" << endl;
  12. cout << "A maximum of 6 resistors please" << endl;
  13. cin >> quantity;
  14. switch (quantity)
  15.  
  16. {
  17. case '1':
  18. cout << " You must enter at least 2";
  19. break;
  20. case '2':
  21. cout << " Enter the value of the first resistor";
  22. cin >> Rone;
  23. cout << " Enter the value of the second resistor";
  24. cin >> "Rtwo;
  25. Rpar = (Rone * Rtwo) / (Rone + Rtwo);
  26. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  27. break;
  28. case '3':
  29. cout << " Enter the value of the first resistor";
  30. cin >> Rone;
  31. cout << " Enter the value of the second resistor";
  32. cin >> "Rtwo;
  33. cout << " Enter the value of the third resistor";
  34. cin >> "Rthree;
  35. Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree);
  36. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  37. break;
  38. case '4':
  39. cout << " Enter the value of the first resistor";
  40. cin >> Rone;
  41. cout << " Enter the value of the second resistor";
  42. cin >> "Rtwo;
  43. cout << " Enter the value of the third resistor";
  44. cin >> "Rthree;
  45. cout << " Enter the value of the fouth resistor";
  46. cin >> "Rfour;
  47. Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour);
  48. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  49. break;
  50. case '5':
  51. cout << " Enter the value of the first resistor";
  52. cin >> Rone;
  53. cout << " Enter the value of the second resistor";
  54. cin >> "Rtwo;
  55. cout << " Enter the value of the third resistor";
  56. cin >> "Rthree;
  57. cout << " Enter the value of the fouth resistor";
  58. cin >> "Rfour;
  59. cout << " Enter the value of the fifth resistor";
  60. cin >> "Rfive;
  61. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive);
  62. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  63. break;
  64. case '6':
  65. cout << " Enter the value of the first resistor";
  66. cin >> Rone;
  67. cout << " Enter the value of the second resistor";
  68. cin >> "Rtwo;
  69. cout << " Enter the value of the third resistor";
  70. cin >> "Rthree;
  71. cout << " Enter the value of the fouth resistor";
  72. cin >> "Rfour;
  73. cout << " Enter the value of the fifth resistor";
  74. cin >> "Rfive;
  75. cout << " Enter the value of the sixth resistor";
  76. cin >> "Rsix;
  77. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix);
  78. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  79.  
  80.  
  81. }
  82. return 0;
  83. }
  84.  


Any help would be appreciated.

Thanks, BandM
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Problem with Switch statement ?

 
0
  #2
Aug 10th, 2005
try changing the

int main (void) ...

to int main () {

should read like this

int main () {

float ......

and no { here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Problem with Switch statement ?

 
0
  #3
Aug 10th, 2005
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;
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: bandm is an unknown quantity at this point 
Solved Threads: 0
bandm's Avatar
bandm bandm is offline Offline
Newbie Poster

Re: Problem with Switch statement ?

 
0
  #4
Aug 10th, 2005
I tried that and I still got some errors

I changed it ti this:

  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6.  
  7.  
  8. ;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
  9. char quantity;
  10. {
  11.  
  12.  
  13.  
  14. cout << "Enter the quantity of resistors" << endl;
  15. cout << "A maximum of 6 resistors please" << endl;
  16. cin >> quantity;
  17. switch (quantity)
  18.  
  19.  
  20. case '1':
  21. cout << " You must enter at least 2";
  22. break;
  23. case '2':
  24. cout << " Enter the value of the first resistor";
  25. cin >> Rone;
  26. cout << " Enter the value of the second resistor";
  27. cin >> Rtwo;
  28. Rpar = (Rone * Rtwo) / (Rone + Rtwo);
  29. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  30. break;
  31. case '3':
  32. cout << " Enter the value of the first resistor";
  33. cin >> Rone;
  34. cout << " Enter the value of the second resistor";
  35. cin >> Rtwo;
  36. cout << " Enter the value of the third resistor";
  37. cin >> Rthree;
  38. Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree);
  39. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  40. break;
  41. case '4':
  42. cout << " Enter the value of the first resistor";
  43. cin >> Rone;
  44. cout << " Enter the value of the second resistor";
  45. cin >> Rtwo;
  46. cout << " Enter the value of the third resistor";
  47. cin >> Rthree;
  48. cout << " Enter the value of the fouth resistor";
  49. cin >> Rfour;
  50. Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour);
  51. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  52. break;
  53. case '5':
  54. cout << " Enter the value of the first resistor";
  55. cin >> Rone;
  56. cout << " Enter the value of the second resistor";
  57. cin >> Rtwo;
  58. cout << " Enter the value of the third resistor";
  59. cin >> Rthree;
  60. cout << " Enter the value of the fouth resistor";
  61. cin >> Rfour;
  62. cout << " Enter the value of the fifth resistor";
  63. cin >> Rfive;
  64. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive);
  65. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  66. break;
  67. case '6':
  68. cout << " Enter the value of the first resistor";
  69. cin >> Rone;
  70. cout << " Enter the value of the second resistor";
  71. cin >> Rtwo;
  72. cout << " Enter the value of the third resistor";
  73. cin >> Rthree;
  74. cout << " Enter the value of the fouth resistor";
  75. cin >> Rfour;
  76. cout << " Enter the value of the fifth resistor";
  77. cin >> Rfive;
  78. cout << " Enter the value of the sixth resistor";
  79. cin >> Rsix;
  80. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix);
  81. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  82.  
  83.  
  84.  
  85. return 0;
  86. }

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Problem with Switch statement ?

 
0
  #5
Aug 10th, 2005
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 }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: bandm is an unknown quantity at this point 
Solved Threads: 0
bandm's Avatar
bandm bandm is offline Offline
Newbie Poster

Re: Problem with Switch statement ?

 
0
  #6
Aug 11th, 2005
Well, I have gotten down to one error.
missing function header.
Anyone have any ideas?

  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6.  
  7. ;float RPar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
  8. int quantity;
  9.  
  10. {
  11.  
  12.  
  13.  
  14. cout << "Enter the quantity of resistors" << endl;
  15. cout << "A maximum of 6 resistors please" << endl;
  16. cin >> quantity;
  17. switch (quantity)
  18.  
  19.  
  20. case 1:
  21. cout << " You must enter at least 2";
  22. break;
  23. case '2':
  24. cout << " Enter the value of the first resistor";
  25. cin >> Rone;
  26. cout << " Enter the value of the second resistor";
  27. cin >> Rtwo;
  28. Rpar = (Rone * Rtwo) / (Rone + Rtwo);
  29. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  30. break;
  31. case 3:
  32. cout << " Enter the value of the first resistor";
  33. cin >> Rone;
  34. cout << " Enter the value of the second resistor";
  35. cin >> Rtwo;
  36. cout << " Enter the value of the third resistor";
  37. cin >> Rthree;
  38. Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree);
  39. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  40. break;
  41. case 4:
  42. cout << " Enter the value of the first resistor";
  43. cin >> Rone;
  44. cout << " Enter the value of the second resistor";
  45. cin >> Rtwo;
  46. cout << " Enter the value of the third resistor";
  47. cin >> Rthree;
  48. cout << " Enter the value of the fouth resistor";
  49. cin >> Rfour;
  50. Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour);
  51. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  52. break;
  53. case 5:
  54. cout << " Enter the value of the first resistor";
  55. cin >> Rone;
  56. cout << " Enter the value of the second resistor";
  57. cin >> Rtwo;
  58. cout << " Enter the value of the third resistor";
  59. cin >> Rthree;
  60. cout << " Enter the value of the fouth resistor";
  61. cin >> Rfour;
  62. cout << " Enter the value of the fifth resistor";
  63. cin >> Rfive;
  64. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive);
  65. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  66. break;
  67. case 6:
  68. cout << " Enter the value of the first resistor";
  69. cin >> Rone;
  70. cout << " Enter the value of the second resistor";
  71. cin >> Rtwo;
  72. cout << " Enter the value of the third resistor";
  73. cin >> Rthree;
  74. cout << " Enter the value of the fouth resistor";
  75. cin >> Rfour;
  76. cout << " Enter the value of the fifth resistor";
  77. cin >> Rfive;
  78. cout << " Enter the value of the sixth resistor";
  79. cin >> Rsix;
  80. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix);
  81. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  82.  
  83.  
  84.  
  85. return 0;
  86. }

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with Switch statement ?

 
0
  #7
Aug 11th, 2005
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 the switch within {}. And this:
   case '2':
should be this:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: bandm is an unknown quantity at this point 
Solved Threads: 0
bandm's Avatar
bandm bandm is offline Offline
Newbie Poster

Re: Problem with Switch statement ?

 
0
  #8
Aug 11th, 2005
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:
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6.  
  7. float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
  8. int quantity;
  9.  
  10. {
  11.  
  12.  
  13. {
  14. cout << "Enter the quantity of resistors" << endl;
  15. cout << "A maximum of 6 resistors please" << endl;
  16. cin >> quantity;
  17. switch (quantity)
  18. }
  19.  
  20. case 1:
  21. cout << " You must enter at least 2";
  22. break;
  23. case 2:
  24. cout << " Enter the value of the first resistor";
  25. cin >> Rone;
  26. cout << " Enter the value of the second resistor";
  27. cin >> Rtwo;
  28. Rpar = (Rone * Rtwo) / (Rone + Rtwo);
  29. 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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with Switch statement ?

 
0
  #9
Aug 11th, 2005
Three times you've been told not to declare variables between this
  1. int main()
and this
  1. { // 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.
Originally Posted by Dave Sinkula
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;
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: bandm is an unknown quantity at this point 
Solved Threads: 0
bandm's Avatar
bandm bandm is offline Offline
Newbie Poster

Re: Problem with Switch statement ?

 
0
  #10
Aug 11th, 2005
It's not that I ignored it, I tried it and it and got tons more errors than before I tried it.

  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6.  
  7.  
  8.  
  9. {
  10. float Rpar, Rone, Rtwo, Rthree, Rfour, Rfive, Rsix;
  11. int quantity;
  12.  
  13.  
  14. cout << "Enter the quantity of resistors" << endl;
  15. cout << "A maximum of 6 resistors please" << endl;
  16. cin >> quantity;
  17. switch (quantity)
  18.  
  19.  
  20. case 1:
  21. cout << " You must enter at least 2";
  22. break;
  23. case 2:
  24. cout << " Enter the value of the first resistor";
  25. cin >> Rone;
  26. cout << " Enter the value of the second resistor";
  27. cin >> Rtwo;
  28. Rpar = (Rone * Rtwo) / (Rone + Rtwo);
  29. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  30. break;
  31. case 3:
  32. cout << " Enter the value of the first resistor";
  33. cin >> Rone;
  34. cout << " Enter the value of the second resistor";
  35. cin >> Rtwo;
  36. cout << " Enter the value of the third resistor";
  37. cin >> Rthree;
  38. Rpar = (Rone * Rtwo * Rthree) / (Rone + Rtwo + Rthree);
  39. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  40. break;
  41. case 4:
  42. cout << " Enter the value of the first resistor";
  43. cin >> Rone;
  44. cout << " Enter the value of the second resistor";
  45. cin >> Rtwo;
  46. cout << " Enter the value of the third resistor";
  47. cin >> Rthree;
  48. cout << " Enter the value of the fouth resistor";
  49. cin >> Rfour;
  50. Rpar = (Rone * Rtwo * Rthree * Rfour) / (Rone + Rtwo + Rthree + Rfour);
  51. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  52. break;
  53. case 5:
  54. cout << " Enter the value of the first resistor";
  55. cin >> Rone;
  56. cout << " Enter the value of the second resistor";
  57. cin >> Rtwo;
  58. cout << " Enter the value of the third resistor";
  59. cin >> Rthree;
  60. cout << " Enter the value of the fouth resistor";
  61. cin >> Rfour;
  62. cout << " Enter the value of the fifth resistor";
  63. cin >> Rfive;
  64. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive) / (Rone + Rtwo + Rthree + Rfour + Rfive);
  65. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  66. break;
  67. case 6:
  68. cout << " Enter the value of the first resistor";
  69. cin >> Rone;
  70. cout << " Enter the value of the second resistor";
  71. cin >> Rtwo;
  72. cout << " Enter the value of the third resistor";
  73. cin >> Rthree;
  74. cout << " Enter the value of the fouth resistor";
  75. cin >> Rfour;
  76. cout << " Enter the value of the fifth resistor";
  77. cin >> Rfive;
  78. cout << " Enter the value of the sixth resistor";
  79. cin >> Rsix;
  80. Rpar = (Rone * Rtwo * Rthree * Rfour * Rfive * Rsix) / (Rone + Rtwo + Rthree + Rfour + Rfive + Rsix);
  81. cout <<"The value of parallel resistance is: " << Rpar << "\n";
  82.  
  83.  
  84.  
  85. return 0;
  86. }
  87.  
  88. Compiling...
  89. ResistancePar.cpp
  90. G:\VC++60\ResistancePar\ResistancePar.cpp(21) : error C2043: illegal break
  91. G:\VC++60\ResistancePar\ResistancePar.cpp(22) : error C2046: illegal case
  92. G:\VC++60\ResistancePar\ResistancePar.cpp(29) : error C2043: illegal break
  93. G:\VC++60\ResistancePar\ResistancePar.cpp(30) : error C2046: illegal case
  94. G:\VC++60\ResistancePar\ResistancePar.cpp(39) : error C2043: illegal break
  95. G:\VC++60\ResistancePar\ResistancePar.cpp(40) : error C2046: illegal case
  96. G:\VC++60\ResistancePar\ResistancePar.cpp(51) : error C2043: illegal break
  97. G:\VC++60\ResistancePar\ResistancePar.cpp(52) : error C2046: illegal case
  98. G:\VC++60\ResistancePar\ResistancePar.cpp(65) : error C2043: illegal break
  99. G:\VC++60\ResistancePar\ResistancePar.cpp(66) : error C2046: illegal case
  100. Error executing cl.exe.
  101.  
  102. 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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC