Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 4th, 2005, 09:53 PM
Registered User
 
Join Date: Nov 2005
Location: , TX, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DweeLer
Default Water Bill Homework

I need help with this damn program. Its suppose to be like a water bill for commercial and home use. Home is 5.00+.0005 per gal and the commercial is 1000.00 (for 4 million gallons) and .00025 for every gallon after. The program prompts the user to enter a "C" or "H" for type of water bill, then how many gallons used, and it calculates and tell you how much the bill will be. If any other character is entered, it is suppose to return an error.

no matter what letter i enter, after i enter the gallons, it returns an error. Im getting owned by my own program. Someone help please.

the files are on my site, listed below.

http://www.bradh.net/jacob/prog

Reply With Quote
  #2 (permalink)  
Old November 6th, 2005, 08:37 PM
Authorized User
 
Join Date: Nov 2005
Location: Chennai, Tamil Nadu, India.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

Hi Dweeler,
The problem is this area:
Code:
int getBill (char x)
{
        if (x!='C' || x!='H')
                return 2;
        if (x!='c' || x!='h')
                return 2;
        if (x=='c' || x=='C')
                return 1;
        if (x=='h' || x=='H')
                return 0;
}
If you modify the code like this:
Code:
int getBill (char x)
{
        if(x == 'c' || x == 'C')
          return 0;
        else if (x == 'h' || x == 'H')
          return 1;
        else
          return 2;
}
then the code would run better.

Can you understand where have you made the error?

Regards,
Paramesh.
Reply With Quote
  #3 (permalink)  
Old November 6th, 2005, 08:46 PM
Registered User
 
Join Date: Nov 2005
Location: , TX, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DweeLer
Default

Awesome, totally didn't think about it that way. this is my first year of programming and its still a lot hard for me to understand. Thanks for thinking outside of the box. :D

Reply With Quote
  #4 (permalink)  
Old November 6th, 2005, 09:20 PM
Registered User
 
Join Date: Nov 2005
Location: , TX, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DweeLer
Default

The math or something is wrong and It outputs this weird output:

help please :(

Enter bill type (H = home, C = commercial): C
Enter gallons of water used: 100,000,001

**** WATER BILL ****
Account Type: Commercial
Total Amount Owed: 10000x4375b4
Thank you for your business.


Reply With Quote
  #5 (permalink)  
Old November 6th, 2005, 11:01 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your code should have lines like this:

 cout << "Total Amount Owed: " << result;

But you have

 cout << "Total Amount Owed: " << result <<
 cout << endl;

I honestly was surprised that compiled.

Reply With Quote
  #6 (permalink)  
Old November 7th, 2005, 12:20 AM
Authorized User
 
Join Date: Nov 2005
Location: Chennai, Tamil Nadu, India.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

Hi middledd,
Where did you find a line like that?

Dweeler,
Please post your updated code by using the code tags.

Regards,
Paramesh.

"Don't walk behind me; I may not lead.
 Don't walk in front of me; I may not follow.
 Just walk beside me and be my friend."
Reply With Quote
  #7 (permalink)  
Old November 7th, 2005, 08:56 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The user posted his code to this location:

http://www.bradh.net/jacob/prog

Reply With Quote
  #8 (permalink)  
Old November 7th, 2005, 09:08 AM
Authorized User
 
Join Date: Nov 2005
Location: Chennai, Tamil Nadu, India.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

Now, the link is no longer available.
I'll post the code here for others to take a look at it.
Code:
#include <iostream>

using namespace std;

const double HOUSEPGAL = 0.0005;
const double HOUSEINIT = 5.00;
const double FOURMIL = 4000000;
const double COMINIT = 1000.00;
const double COMADDGAL = 0.00025;

int getBill (char);

int main()
{
        char x;
        double g;
        double result;

        cout << "Enter bill type (H = home, C = commercial): ";
cin >> x;
cout << "Enter gallons of water used: ";
cin >> g;
cout << endl;


        switch(getBill (x))
        {

                case 0: result = (g*HOUSEPGAL)+HOUSEINIT;
                        cout << "The amount for your Home water bill is "
                        << result << ". Thank you for your business.";
                                break;
                case 1: if (g<FOURMIL)
                        {
                        result = COMINIT;
                        }
                        else if (g>FOURMIL)
                        {
                        result = (g-FOURMIL)*COMADDGAL+(g*COMINIT);
                        }
                        cout << "The amount for your Corporate bill is "
                                << result << ". Thank you for your business.";
                                break;
                case 2: cout << "Error. Please ask for assistance.";
                                break;
        }

        getchar();
        getchar();
        return 0;
}

int getBill (char x)
{
        if(x == 'c' || x == 'C')
          return 0;
        else if (x == 'h' || x == 'H')
          return 1;
        else
          return 2;
}
and middledd,
There is no line in this code as you said.

Regards,
Paramesh.

"Don't walk behind me; I may not lead.
 Don't walk in front of me; I may not follow.
 Just walk beside me and be my friend."
Reply With Quote
  #9 (permalink)  
Old November 7th, 2005, 01:22 PM
Registered User
 
Join Date: Nov 2005
Location: , TX, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to DweeLer
Default

sorry, the link is still active, just moved it around.

http://www.bradh.net/jacob/Cosc1415/rpgram3/

Reply With Quote
  #10 (permalink)  
Old November 7th, 2005, 08:51 PM
Authorized User
 
Join Date: Nov 2005
Location: Chennai, Tamil Nadu, India.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

Sorry middledd.
I didnt take a look at the updated program.

Quote:
quote:I honestly was surprised that compiled.
The lines:
Code:
                        cout << "Total Amount Owed: " << result <<
                        cout << endl;   
can be viewed like this:
Code:
                        cout << "Total Amount Owed: " << result << cout << endl;   
So, printing cout will return the address.

Dweeler, you should change your code as shown by middledd.
So, your code should be like this:
Code:
#include <iostream> // to read cin, cout
#include "conio.h"
#include <iomanip>

using namespace std;

// CONSTANTS

const double HOUSEPGAL = 0.0005; //Constant for price per gallon on house
const double HOUSEINIT = 5.00; //house initial price
const double FOURMIL = 4000000; //Commercial amount for 1000
const double COMINIT = 1000.00; //Commercial initial price for water
const double COMADDGAL = 0.00025; //Commercial price for additional gallon

int getBill (char);

//main

int main()
{
    //variables
        char x;
        double Gal;
        double result = 0;

            cout << fixed << showpoint;

            cout << "Enter bill type (H = home, C = commercial): ";
            cin >> x;
            cout << "Enter gallons of water used: ";
            cin >> Gal;
            cout << endl;


        switch(getBill (x))
        {
                //case 0 is for the House Bill
                case '0': result = (Gal * HOUSEPGAL)+ HOUSEINIT;
                        cout << "**** WATER BILL ****";
                        cout << endl;
                        cout << "Account Type: Home";
                        cout << endl;
                        cout << setprecision(1);
                        cout << "Total Amount Owed: " << result << endl;     
                        cout << "  Thank you for your business.";
                        cout << endl;
                        cout << "********************";
                                break;
                //case 1 is for the Commercial Bill
                case '1': if (Gal < FOURMIL)
                        {
                        result = COMINIT;
                        }
                        else if (Gal > FOURMIL)
                        {
                        result = (Gal - FOURMIL)* COMADDGAL + (Gal * COMINIT);
                        }
                        cout << "**** WATER BILL ****";
                        cout << endl;
                        cout << "Account Type: Commercial";
                        cout << endl;
                        cout << setprecision(1);
                        cout << "Total Amount Owed: " << result;
                        cout << endl;     
                        cout << "Thank you for your business.";
                        cout << endl;
                        cout << "********************";
                                break;
                //case 2 is for the error part
                case '2': cout << "Error. Please ask for assistance.";
                                break;
        }

        return 0;
}

//how the program chooses which case to go with based on the character input
int getBill (char x)
{
        if(x == 'c' || x == 'C')
                return '1';
        else if (x == 'h' || x == 'H')
                return '0';
        else
                return '2';
}
Regards,
Paramesh.

"Don't walk behind me; I may not lead.
Don't walk in front of me; I may not follow.
Just walk beside me and be my friend."
Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Homework - Help please satish_appasani BOOK: Professional WCF Programming: .NET Dev with Windows Communication Found ISBN: 9780470089842 0 July 29th, 2008 06:50 AM
Bill are you here??? ramone_johnny Classic ASP Components 3 July 29th, 2008 12:57 AM
My Homework (I solved it) yui0329 C# 1 June 10th, 2005 11:25 AM
frames and pop-up menus like oil and water? bee Dreamweaver (all versions) 1 October 26th, 2003 02:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.