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

program w/ switch AND nested if. six error messages

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

Attachments hopewll.cpp (3.66KB)
jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Start by remembering that C++ is case sensitive. Then make sure that every variable you use is defined before it's used.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I guess i'm not exactly sure what 'defined' means; doesn't the result of my switch and nested if statements define them?

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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

int main()
{
  int variable; // variable is now defined

  // ...
}

>doesn't the result of my switch and nested if statements define them?
Not in C++.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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;

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

should it be:

int main ()

?

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

>it still won't compile..
Then post the code (don't attach it).

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thank you so much!

//*****************************PRE PROCESSORS***********************************
#include <iomanip.h>
#include <fstream.h>
//**********************************MAIN****************************************
int main ()
{
	  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;

	  cout  <<  "Enter today's date:" << endl;
	  cin.getline (date, sizeof (date));
	  cout  <<  "Enter the account number:"  <<  endl;
	  cin.getline (account, sizeof (account));
	  cout  <<  "Enter the number of children in the household:"  <<  endl;
	  cin   >>  children;
	  cout  <<  "Enter the number of gallons of water used in the household:"  << endl;
	  cin   >>  water;
	  cout  <<  "Enter the amount of electricity used in the household (in kwh):"  <<  endl;
	  cin   >>  electric;

	  switch (children)
      {
				case 0:
						school_fee = 100.00;
						break;
				case 1:
				case 2:
						school_fee = 200.00;
						break;
				case 3:
				case 4:
				case 5:
						school_fee = 250.00;
				default:
						school_fee = 300.00;
						break;
	  }


	  if (water < 1500){
			water_chrg = .0125;}
	  else if (water == 1500){
			water_chrg = 21.20;}
	  else if (water <= 1999){
			water_chrg = 21.2142;}
	  else if (water == 2000){
			water_chrg = 25.85;}
	  else{
			water_chrg = 25.8665;}


	  if (electric < 400){
			elec_chrg = .053;}
	  else if (electric == 400){
			elec_chrg = 21.20;}
	  else if (electric <= 699){
			elec_chrg = 21.261;}
	  else if (electric == 700){
			elec_chrg = 39.50;}
	  else if (electric <= 999){
			elec_chrg = 39.573;}
	  else if (electric == 1000){
			elec_chrg = 61.40;}
	  else{
			elec_chrg = 61.488;}


	  tot_chrg = school_fee + water_chrg + elec_chrg + SAN_CHRG;
	  tax      = TAX_RATE * tot_chrg;
	  bal_due  = tax + tot_chrg;


	  cout  <<  "Direct output to the console (1) or to a disk file (2):  ";
	  cin   >>  choice;
	  if    (choice == 1)
			  outfile.open ( "con" );
	  else
			  outfile.open ( "a:result.dta" );


	  outfile  <<  setiosflags (ios::showpoint | ios::fixed)  <<  setprecision (0);
	  outfile  <<  setw(47)  <<  "Town of Hopewell"  <<  endl;
	  outfile  <<  setw(47)  <<  "----------------"  <<  endl  <<  endl;
	  outfile  <<  "Date:                       "  <<  endl  <<  endl;
	  outfile  <<  "Account:                    "  <<  endl  <<  endl;
	  outfile  <<  setw(20)  <<  "Children"  <<  setw(13)  "School Fee"  <<  setw(18)
				  <<  "Water (gallons)"  <<  setw(15)  <<  "Water Charge"  <<  endl;
	  outfile  <<  setw(20)  <<  children  <<  setw(13)  school_fee  <<  setw(18)
				  <<  water  <<  setw(15)  <<  water_chrg  <<  endl  <<  endl;
	  outfile  <<  setw(26)  <<  "Electric (kwh)"  <<  setw(19)  "Electric Charge"  <<  setw(21)
				  <<  "Sanitation Charge"  <<  endl;
	  outfile  <<  setw(26)  <<  electric  <<  setw(19)  elec_chrg  <<  setw(21)
				  <<  SAN_CHRG  <<  endl  <<  endl;
	  outfile  <<  "Total Charge:  "  <<  endl  <<  endl;
	  outfile  <<  "Tax:           "  <<  endl  <<  endl;
	  outfile  <<  "Balance Due:   "  <<  endl  <<  endl;


	  outfile.close ( );
}
jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Okay, now include 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

More correct C++ usage is to omit the .h from the standard includes. Using the #include instead of #include turns on C compatibility mode for the module.

Depending on your compiler you'll get a warning or not about it.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

>More correct C++ usage is to omit the .h from the standard includes.
Let's get the code working first, then get into style and form issues, k?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I added the preprosessor; it didn't seem to help.

I thought I had already declared my variables; what did I do wrong?

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I added the preprosessor; it didn't seem to help.

I thought I had already declared my variables; what did I do wrong?

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

>what did I do wrong?
Without your current code, I'll never know.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

All I changed was the preprocessor; i added like you said; i thought that the declaration section was the next section and I'd already declared my variables. my code is posted at post: 11-10-2004, 05:18 PM

//*****************************PRE PROCESSORS***********************************
#include
#include
#include
//**********************************MAIN*********************************

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
>what did I do wrong? Without your current code, I'll never know.

all i did was add the preprocessor you suggested. there was no change

i thought i had already declared all my variables (see my previous post)

i'm in so much trouble if i don't finish this by monday and i'm so lost...

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
//*****************************PRE PROCESSORS***********************************
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
//**********************************MAIN****************************************
int main ()
{
  const double SAN_CHARGE = 12.50,
    TAX_RATE = 0.07;

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

  ofstream outfile;

  int children,
    choice;

  long int electric,
    water;

  double bal_due,
    elec_chrg,
    school_fee,
    tax,
    tot_chrg,
    water_chrg;

  cout  <<  "Enter today's date:" << endl;
  cin.getline (date, sizeof (date));
  cout  <<  "Enter the account number:"  <<  endl;
  cin.getline (account, sizeof (account));
  cout  <<  "Enter the number of children in the household:"  <<  endl;
  cin   >>  children;
  cout  <<  "Enter the number of gallons of water used in the household:"  << endl;
  cin   >>  water;
  cout  <<  "Enter the amount of electricity used in the household (in kwh):"  <<  endl;
  cin   >>  electric;

  switch (children)
  {
  case 0:
    school_fee = 100.00;
    break;
  case 1:
  case 2:
    school_fee = 200.00;
    break;
  case 3:
  case 4:
  case 5:
    school_fee = 250.00;
  default:
    school_fee = 300.00;
    break;
  }

  if (water < 1500){
    water_chrg = .0125;}
  else if (water == 1500){
    water_chrg = 21.20;}
  else if (water <= 1999){
    water_chrg = 21.2142;}
  else if (water == 2000){
    water_chrg = 25.85;}
  else{
    water_chrg = 25.8665;}

  if (electric < 400){
    elec_chrg = .053;}
  else if (electric == 400){
    elec_chrg = 21.20;}
  else if (electric <= 699){
    elec_chrg = 21.261;}
  else if (electric == 700){
    elec_chrg = 39.50;}
  else if (electric <= 999){
    elec_chrg = 39.573;}
  else if (electric == 1000){
    elec_chrg = 61.40;}
  else{
    elec_chrg = 61.488;}

  tot_chrg = school_fee + water_chrg + elec_chrg + SAN_CHARGE;
  tax      = TAX_RATE * tot_chrg;
  bal_due  = tax + tot_chrg;

  cout  <<  "Direct output to the console (1) or to a disk file (2):  ";
  cin   >>  choice;
  if    (choice == 1)
    outfile.open ( "con" );
  else
    outfile.open ( "a:result.dta" );

  outfile<< setiosflags (ios::showpoint | ios::fixed) << setprecision (0);
  outfile<< setw(47)  <<  "Town of Hopewell"<<endl;
  outfile<< setw(47)  <<  "----------------"<<endl<<endl;
  outfile<<"Date:                       "<<endl<<endl;
  outfile<<"Account:                    "<<endl<<endl;
  outfile<< setw(20) <<"Children"<< setw(13) <<"School Fee"<< setw(18)
    <<"Water (gallons)"<< setw(15) <<"Water Charge"<<endl;
  outfile<< setw(20) << children << setw(13) << school_fee << setw(18)
    << water << setw(15) << water_chrg <<endl<<endl;
  outfile<< setw(26) <<"Electric (kwh)"<< setw(19) <<"Electric Charge"
    << setw(21) <<"Sanitation Charge"<<endl;
  outfile<< setw(26) << electric << setw(19) << elec_chrg << setw(21)
    << SAN_CHARGE <<endl<<endl;
  outfile<<"Total Charge:  "<<endl<<endl;
  outfile<<"Tax:           "<<endl<<endl;
  outfile<<"Balance Due:   "<<endl<<endl;

  outfile.close ( );
}

Now go away.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

my current code is below, but first, here are my error messages (some of them seem really strange). if I don't get this done by monday, life as I know it will come to a crashing halt.

11 C:\Program Files\Dev-Cpp\include\c++\3.3.1\backward\iomanip.h:31, from C:\TEMP\hopewll.cpp In file included from C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward/iomanip.h:31, from C:/TEMP/hopewll.cpp

11 C:\TEMP\hopewll.cpp from C:/TEMP/hopewll.cpp

2 C:\Program Files\Dev-Cpp\include\c++\3.3.1\backward\backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated.

C:\TEMP\hopewll.cpp In function `int main()':

49 C:\TEMP\hopewll.cpp `SWITCH' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

50 C:\TEMP\hopewll.cpp syntax error before `{' token

54 C:\TEMP\hopewll.cpp case label `1' not within a switch statement

55 C:\TEMP\hopewll.cpp case label `2' not within a switch statement

58 C:\TEMP\hopewll.cpp case label `3' not within a switch statement

59 C:\TEMP\hopewll.cpp case label `4' not within a switch statement

60 C:\TEMP\hopewll.cpp case label `5' not within a switch statement

62 C:\TEMP\hopewll.cpp `default' label not within a switch statement

53 C:\TEMP\hopewll.cpp break statement not within loop or switch

57 C:\TEMP\hopewll.cpp break statement not within loop or switch

64 C:\TEMP\hopewll.cpp break statement not within loop or switch

/TEMP/hopewll.cpp C:\TEMP\C At global scope:

68 C:\TEMP\hopewll.cpp `water' was not declared in this scope

68 C:\TEMP\hopewll.cpp ISO C++ forbids declaration of `IF' with no type

WELL, THERE ARE A LOT MORE, SO I'LL LET IT GO FROM HERE.

//*****************************PRE PROCESSORS***********************************
#include
#include
#include
//**********************************MAIN****************************************
int main ()
{

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;

cout << "Enter today's date:" << endl;
cin.getline (date, sizeof (date));
cout << "Enter the account number:" << endl;
cin.getline (account, sizeof (account));
cout << "Enter the number of children in the household:" << endl;
cin >> children;
cout << "Enter the number of gallons of water used in the household:" << endl;
cin >> water;
cout << "Enter the amount of electricity used in the household (in kwh):" << endl;
cin >> electric;

SWITCH (children)
{
case 0:
school_fee = 100.00;
break;
case 1:
case 2:
school_fee = 200.00;
break;
case 3:
case 4:
case 5:
school_fee = 250.00;
default:
school_fee = 300.00;
break;
}


IF (water < 1500){
water_chrg = .0125;}
ELSE IF (water == 1500){
water_chrg = 21.20;}
ELSE IF (water <= 1999){
water_chrg = 21.2142;}
ELSE IF (water == 2000){
water_chrg = 25.85;}
ELSE{
water_chrg = 25.8665;}


IF (electric < 400){
elec_chrg = .053;}
ELSE IF (electric == 400){
elec_chrg = 21.20;}
ELSE IF (electric <= 699){
elec_chrg = 21.261;}
ELSE IF (electric == 700){
elec_chrg = 39.50;}
ELSE IF (electric <= 999){
elec_chrg = 39.573;}
ELSE IF (electric == 1000){
elec_chrg = 61.40;}
ELSE{
elec_chrg = 61.488;}


tot_chrg = school_fee + water_chrg + elec_chrg + SAN_CHRG;
tax = TAX_RATE * tot_chrg;
bal_due = tax + tot_chrg;


cout << "Direct output to the console (1) or to a disk file (2): ";
cin >> choice;
if (choice == 1)
outfile.open ( "con" );
else
outfile.open ( "a:result.dta" );


outfile << setiosflags (ios::showpoint | ios::fixed) << setprecision (0);
outfile << setw(47) << "Town of Hopewell" << endl;
outfile << setw(47) << "----------------" << endl << endl;
outfile << "Date: " << endl << endl;
outfile << "Account: " << endl << endl;
outfile << setw(20) << "Children" << setw(13) "School Fee" << setw(18)
<< "Water (gallons)" << setw(15) << "Water Charge" << endl;
outfile << setw(20) << children << setw(13) school_fee << setw(18)
<< water << setw(15) << water_chrg << endl << endl;
outfile << setw(26) << "Electric (kwh)" << setw(19) "Electric Charge" << setw(21)
<< "Sanitation Charge" << endl;
outfile << setw(26) << electric << setw(19) elec_chrg << setw(21)
<< SAN_CHRG << endl << endl;
outfile << "Total Charge: " << endl << endl;
outfile << "Tax: " << endl << endl;
outfile << "Balance Due: " << endl << endl;


outfile.close ( );
}

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

i changed all the keywords to lower case

jamievmi2
Newbie Poster
21 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You