944,200 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5730
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 10th, 2005
0

Problem with Switch statement ?

Expand Post »
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)

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005
Aug 10th, 2005
0

Re: Problem with Switch statement ?

try changing the

int main (void) ...

to int main () {

should read like this

int main () {

float ......

and no { here.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Aug 10th, 2005
0

Re: Problem with Switch statement ?

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;
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 10th, 2005
0

Re: Problem with Switch statement ?

I tried that and I still got some errors

I changed it ti this:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005
Aug 10th, 2005
0

Re: Problem with Switch statement ?

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 }
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 11th, 2005
0

Re: Problem with Switch statement ?

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005
Aug 11th, 2005
0

Re: Problem with Switch statement ?

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:
C++ Syntax (Toggle Plain Text)
  1. case 2:
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 11th, 2005
0

Re: Problem with Switch statement ?

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:
C++ Syntax (Toggle Plain Text)
  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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005
Aug 11th, 2005
0

Re: Problem with Switch statement ?

Three times you've been told not to declare variables between this
C++ Syntax (Toggle Plain Text)
  1. int main()
and this
C++ Syntax (Toggle Plain Text)
  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.
Quote 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;
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 11th, 2005
0

Re: Problem with Switch statement ?

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

C++ Syntax (Toggle Plain Text)
  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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bandm is offline Offline
19 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: strings in c
Next Thread in C++ Forum Timeline: bin trees





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC