3 Replies - 1269 Views - Last Post: 24 February 2012 - 05:36 PM Rate Topic: -----

#1 Famlyman45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-February 12

computer programming 101 problem with switch statement and if-else

Posted 24 February 2012 - 10:16 AM

#include <iostream>
using namespace std;

/*John Whiting
  C101
  2/25/2012
*/

int main()
{
char waterType;
int waterUse;
double waterBill;

cout << "Enter a code for water type: H, C, or I: ";
cin  >> waterType;
cout << endl;

cout << "Enter the gallons used: ",
cin >> waterUse,
cout << endl;


switch (waterType)
{
	case 'H':
		if (waterType=='H'||waterType=='h'){
			waterBill==(5.00+(0.0005*waterUse)),
			cout << "Amount due: " << waterBill << endl;
		}
		break;
	case 'C':
		if (waterType=='C'||waterType=='c'){
			(waterBill==(waterUse<=1000000*0.001)&&(waterUse>=1000001*0.00025)),
			cout << "Amount due: " << waterBill <<endl;
		}
		break;
	case 'I':
		if (waterType=='I'||waterType=='i'){
			if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else waterUse>10000001;
				cout << "Amount Due:$3000\n";
		break;
	default: 
		cout << "Code is incorrect.";
		}

return 0;
}


This is my solution so far to a problem to select a billing type of Home, Commercial, or Industrial. The program has to do the math associated with the selection according to the gallons input. I need to use switch and if-else statements to complete this homework. My code will not run and I am at a loss for why.

The errors are:Warning 1 warning C4805: '==' : unsafe mix of type 'double' and type 'bool' in operation c:\users\john\documents\visual studio 2010\projects\whiting_homework_5\whiting_homework_5\whiting_homework_5.cpp 34 1 Whiting_Homework_5

and:Warning 2 warning C4552: '>' : operator has no effect; expected operator with side-effect c:\users\john\documents\visual studio 2010\projects\whiting_homework_5\whiting_homework_5\whiting_homework_5.cpp 44 1 Whiting_Homework_5

and finally: Error 3 error C1075: end of file found before the left brace '{' at 'c:\users\john\documents\visual studio 2010\projects\whiting_homework_5\whiting_homework_5\whiting_homework_5.cpp(10)' was matched c:\users\john\documents\visual studio 2010\projects\whiting_homework_5\whiting_homework_5\whiting_homework_5.cpp 53 1 Whiting_Homework_5

Any great assistance by you expert programmers out there will be greatly appreciated. I am at the "Kindergarden Level". Please be nice.

Mod edit - fixed code tags :code:

Is This A Good Question/Topic? 0
  • +

Replies To: computer programming 101 problem with switch statement and if-else

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • Posts: 2,246
  • Joined: 31-December 10

Re: computer programming 101 problem with switch statement and if-else

Posted 24 February 2012 - 10:24 AM

There is a difference between using one equal sign and having two equal signs. The first is for assignment and the second is for comparison. From line 34 of your code you have:
(waterBill==(waterUse<=1000000*0.001)&&(waterUse>=1000001*0.00025)),

Are you just trying to set the water bill? Also the line ends with a comma and not a semicolon.
The next problem with your code is that you try to attach a comparison to an else statement on line 44:
if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else waterUse>10000001;
				cout << "Amount Due:$3000\n";


The last else should be else if(waterUse > 10000001).

Your last problem that I can see is because you forgot to close an if-statement with a curly brace:
case 'I':
		if (waterType=='I'||waterType=='i'){
			if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else waterUse>10000001;
				cout << "Amount Due:$3000\n";
		break;
	default: 
		cout << "Code is incorrect.";
		}

return 0;
}

Inside the switch statement for case 'I', you have an if-statement that starts an opening curly brace '{', but you never close it with a '}'.

EDIT: You should look at other people's code to get a feel for the different indentation styles. You probably would've saw the last error easily if you had indented all of your code properly and consistently. It also helps people (including yourself) to be able to read your code easier. That's one thing that students need to be taught more about, and that is that not only do programmers write new programs, sometimes they have to maintain code written by other people. If those people had a sloppy style or didn't put many comments in their code, then it becomes a nightmare to read and to maintain.

This post has been edited by vividexstance: 24 February 2012 - 10:28 AM

Was This Post Helpful? 1
  • +
  • -

#3 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: computer programming 101 problem with switch statement and if-else

Posted 24 February 2012 - 10:29 AM

i dont think that if statement would fulfill the requirements as you are already checking a value that has been checked by the case statement. Not to mention if it value was lower case it would not execute because the case is looking for an upper case.

also in case I you forgot the closing bracket for your if statement
case 'I':
		if (waterType=='I'||waterType=='i'){
			if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else waterUse>10000001;
				cout << "Amount Due:$3000\n";
} // This is the one you forgot to add
		break;

Was This Post Helpful? 1
  • +
  • -

#4 Famlyman45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-February 12

Re: computer programming 101 problem with switch statement and if-else

Posted 24 February 2012 - 05:36 PM

#include <iostream>
using namespace std;

/*John Whiting
  C101
  2/25/2012
*/

int main()
{
char waterType;
double waterUse, waterBill;

cout << "Enter a code for water type: H, C, or I: ";
cin  >> waterType;
cout << endl;

cout << "Enter the gallons used: ",
cin >> waterUse,
cout << endl;


switch (waterType)
{
	case 'H':
		if (waterType='H'){
			waterBill=(5.00+(0.0005*waterUse)),
			cout << "Amount due: " << waterBill << endl;
		}
		break;
	case 'C':
		if (waterType='C'){
			if (waterUse<=1000000)
			   (waterBill=(waterUse*0.001)),
			   cout << "Amount due: " << waterBill <<endl;
			else if
				(waterUse>1000001)
				(waterBill=1000+((waterUse-1000000)*0.00025)),
				cout << "Amount due: " << waterBill <<endl;
		}
		break;
	case 'I':
		if (waterType='I'){
			if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else if (waterUse>10000000)
				cout << "Amount Due:$3000\n";
		}
		break;
	default: 
		cout << "INVALID CODE!\n";
}
return 0;
}




Program is now working great. Thank You.

What I need now is style pointers and to put in my abstract statements.

May I get pointers for style?
#include <iostream>
using namespace std;

/*John Whiting
  C101
  2/25/2012
*/

int main()
{
char waterType;
double waterUse, waterBill;

cout << "Enter a code for water type: H, C, or I: ";
cin  >> waterType;
cout << endl;

cout << "Enter the gallons used: ",
cin >> waterUse,
cout << endl;


switch (waterType)
{
	case 'H':
		if (waterType='H'){
			waterBill=(5.00+(0.0005*waterUse)),
			cout << "Amount due: " << waterBill << endl;
		}
		break;
	case 'C':
		if (waterType='C'){
			if (waterUse<=1000000)
			   (waterBill=(waterUse*0.001)),
			   cout << "Amount due: " << waterBill <<endl;
			else if
				(waterUse>1000001)
				(waterBill=1000+((waterUse-1000000)*0.00025)),
				cout << "Amount due: " << waterBill <<endl;
		}
		break;
	case 'I':
		if (waterType='I'){
			if (waterUse<=1000000)
				cout << "Amount Due:$1000\n";
			else if ((waterUse>=1000001)&&(waterUse<=10000000))
				cout << "Amount Due:$2000\n";
			else if (waterUse>10000000)
				cout << "Amount Due:$3000\n";
		}
		break;
	default: 
		cout << "INVALID CODE!\n";
}
return 0;
}


Forget ir. I cannot seem to get this thing to take my code as written and indented. I will figure it out.


:code:

This post has been edited by jimblumberg: 24 February 2012 - 06:32 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1