943,972 Members | Top Members by Rank

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

program w/ switch AND nested if. six error messages

Expand Post »
Hello,
this is my first post, so i'm sure i'll fumble it a little bit.

I'm writing a program that has a switch, a nested if, and an output choice of console or new file on a:.
I feel like all my switch and nest is good, but i'm missing something, like a preprocessor. at least i hope I'm that close

here's a list of errors:

LINE 49 call to undefined function 'SWITCH' in function main()
LINE 49statement missing ; in function main()
LINE 96possible use of tot_chrg' before definition in function main()
LINE 101undefined symbol 'choice in function main()
LINES 113 115 117 119 statement missing ; in function main // this appears four times
LINE 127[three of my different variable names: water_chrg school_fee elec_chrg] are used but never defined in function main()
LINE 127[two of my variable names: bal_due SAN_CHARGE] is assigned a value that is never used in function main()


thanks
jamie
Attached Files
File Type: cpp hopewll.cpp (3.7 KB, 40 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamievmi2 is offline Offline
21 posts
since Nov 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

Start by remembering that C++ is case sensitive. Then make sure that every variable you use is defined before it's used.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

I guess i'm not exactly sure what 'defined' means; doesn't the result of my switch and nested if statements define them?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamievmi2 is offline Offline
21 posts
since Nov 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

>I guess i'm not exactly sure what 'defined' means
Your variable tells the compiler, "Here I am! I exist. Set aside storage for me." And you do it like so:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int variable; // variable is now defined
  4.  
  5. // ...
  6. }
>doesn't the result of my switch and nested if statements define them?
Not in C++.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
-1

Re: program w/ switch AND nested if. six error messages

I guess I'm in over my head; are we talking about this section?

const double SAN_CHARGE = 12.50,
TAX_RATE = 0.07;

char account [6], // No dashes
date [20]; // Month Day, Year

ofstream outfile;

int children;

long int electric,
water;

double bal_due,
elec_chrg,
school_fee,
tax,
tot_chrg,
water_chrg;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamievmi2 is offline Offline
21 posts
since Nov 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

>I guess I'm in over my head
One problem at a time. Change your keywords (switch, if, else, and so on) to lower case first, then you should compile with only warnings and we'll work from there.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

it still won't compile.. i think i'm up the proverbial creek!

should it be:

int main ()

?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamievmi2 is offline Offline
21 posts
since Nov 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

>it still won't compile..
Then post the code (don't attach it).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: program w/ switch AND nested if. six error messages

thank you so much!


C++ Syntax (Toggle Plain Text)
  1.  
  2. //*****************************PRE PROCESSORS***********************************
  3. #include <iomanip.h>
  4. #include <fstream.h>
  5. //**********************************MAIN****************************************
  6. int main ()
  7. {
  8. const double SAN_CHARGE = 12.50,
  9. TAX_RATE = 0.07;
  10.  
  11. char account [6], // No dashes
  12. date [20]; // Month Day, Year
  13.  
  14. ofstream outfile;
  15.  
  16. int children;
  17.  
  18. long int electric,
  19. water;
  20.  
  21. double bal_due,
  22. elec_chrg,
  23. school_fee,
  24. tax,
  25. tot_chrg,
  26. water_chrg;
  27.  
  28. cout << "Enter today's date:" << endl;
  29. cin.getline (date, sizeof (date));
  30. cout << "Enter the account number:" << endl;
  31. cin.getline (account, sizeof (account));
  32. cout << "Enter the number of children in the household:" << endl;
  33. cin >> children;
  34. cout << "Enter the number of gallons of water used in the household:" << endl;
  35. cin >> water;
  36. cout << "Enter the amount of electricity used in the household (in kwh):" << endl;
  37. cin >> electric;
  38.  
  39. switch (children)
  40. {
  41. case 0:
  42. school_fee = 100.00;
  43. break;
  44. case 1:
  45. case 2:
  46. school_fee = 200.00;
  47. break;
  48. case 3:
  49. case 4:
  50. case 5:
  51. school_fee = 250.00;
  52. default:
  53. school_fee = 300.00;
  54. break;
  55. }
  56.  
  57.  
  58. if (water < 1500){
  59. water_chrg = .0125;}
  60. else if (water == 1500){
  61. water_chrg = 21.20;}
  62. else if (water <= 1999){
  63. water_chrg = 21.2142;}
  64. else if (water == 2000){
  65. water_chrg = 25.85;}
  66. else{
  67. water_chrg = 25.8665;}
  68.  
  69.  
  70. if (electric < 400){
  71. elec_chrg = .053;}
  72. else if (electric == 400){
  73. elec_chrg = 21.20;}
  74. else if (electric <= 699){
  75. elec_chrg = 21.261;}
  76. else if (electric == 700){
  77. elec_chrg = 39.50;}
  78. else if (electric <= 999){
  79. elec_chrg = 39.573;}
  80. else if (electric == 1000){
  81. elec_chrg = 61.40;}
  82. else{
  83. elec_chrg = 61.488;}
  84.  
  85.  
  86. tot_chrg = school_fee + water_chrg + elec_chrg + SAN_CHRG;
  87. tax = TAX_RATE * tot_chrg;
  88. bal_due = tax + tot_chrg;
  89.  
  90.  
  91. cout << "Direct output to the console (1) or to a disk file (2): ";
  92. cin >> choice;
  93. if (choice == 1)
  94. outfile.open ( "con" );
  95. else
  96. outfile.open ( "a:result.dta" );
  97.  
  98.  
  99. outfile << setiosflags (ios::showpoint | ios::fixed) << setprecision (0);
  100. outfile << setw(47) << "Town of Hopewell" << endl;
  101. outfile << setw(47) << "----------------" << endl << endl;
  102. outfile << "Date: " << endl << endl;
  103. outfile << "Account: " << endl << endl;
  104. outfile << setw(20) << "Children" << setw(13) "School Fee" << setw(18)
  105. << "Water (gallons)" << setw(15) << "Water Charge" << endl;
  106. outfile << setw(20) << children << setw(13) school_fee << setw(18)
  107. << water << setw(15) << water_chrg << endl << endl;
  108. outfile << setw(26) << "Electric (kwh)" << setw(19) "Electric Charge" << setw(21)
  109. << "Sanitation Charge" << endl;
  110. outfile << setw(26) << electric << setw(19) elec_chrg << setw(21)
  111. << SAN_CHRG << endl << endl;
  112. outfile << "Total Charge: " << endl << endl;
  113. outfile << "Tax: " << endl << endl;
  114. outfile << "Balance Due: " << endl << endl;
  115.  
  116.  
  117. outfile.close ( );
  118. }
Last edited by alc6379; Nov 10th, 2004 at 7:42 pm. Reason: added [code] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamievmi2 is offline Offline
21 posts
since Nov 2004
Nov 11th, 2004
0

Re: program w/ switch AND nested if. six error messages

Okay, now include <iostream.h> and declare your variables. Notice that what we're doing here is incremental fixes. There are so many problems that you can only work with one at a time, so you walk down the list of errors from the top, recompiling as you fix them because sometimes one fix will remove several error messages.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: assignment needs a little help, well i do
Next Thread in C++ Forum Timeline: problem with Chars and Strings





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


Follow us on Twitter


© 2011 DaniWeb® LLC