Beginner Program

Pages: 12
Well, I've made a little progress from last week when I worked my first program using "getline" and "strings", but I'm still not good at using mathematics in programs? Any help I can get to push me forward is greatly appreciated.


You are to write a program to compute the amount of a person's water bill. The input to the program will be the customer name (read with getline), the type of customer (a string) and the number of gallons of water used for the month (a double). The customer name will have a variable number of words in the name, so the name needs to be read with getline. The type of customer will be either "R" for residential customers, "B" for business customers, "G" for government or "NP" for non-profit customers.

The charges depend on the customer type.

For residential customers, there is a water base fee of $15 and a charge of $0.02 per gallon for all gallons over 1000 used in a month.

For business customers, there is a water base fee of $25 and a charge of $0.03 per gallon for all gallons over 2000 used in a month.

For non-profit and government customers, there is a water base fee of $5 and a charge of $0.01 per gallon for all gallons over 500 used in a month.

The sewage base fee equals the water base fee for all customers and the sewage usage fee is 80% of the water usage fee for all customers.


Jones County Junior College
Gallons used ................ 100000
Water base fee .............. $ 5.00
Water usage fee ............. $ 995.00
Sewage base fee ............. $ 5.00
Sewage usage fee ............ $ 796.00
Total due ................... $ 1801.00


This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;


int main () 
{
    
    double gallons;
    string str;
    cout << "Please enter full name: ";
    getline (cin,str);
    string R;
    R = "Residential";
    cout << "R  is: " << R << endl;
    cout<<"Enter Gallons :";
    cin>>gallons;
    
  
    
    
system("pause");
    return 0;

}

Last edited on
Instead of calling your string "str" call it something that is more obvious what it contains like "name".

Not sure what these lines are for:
1
2
    string R;
    R = "Residential";

Are you maybe wanting something like:
1
2
3
4
5
string customerType;
cout <<"What type of customer are you?" << endl
  <<"Enter \"R\" for residential customers, \"B\" for business customers," << endl 
  <<"\"G\" for government or \"NP\" for non-profit customers." << endl;
getline (cin,customerType);


Then probably best to just do a series of if statements for each different customerType. And with in that just do the calculations.

Also it's probably better to either declare all your varibles in the same place, at the start. Or declare each of them just before they are used if you prefer, you are kind of doing both there, which because the program is so small it doesn't really matter, but it might start getting messy with bigger programs.

Also, report the code, with your attempts at the calculations and ask if you need further help. You didn't really make it very clear in the question what you wanted help with, so I just pointed out the first few things that came to mind.

Meerkat.
Thanks, that string cleaned it up a bit, and the main problem I'm having is getting all of the fees. I don't know how to use an if statement for the algorithms. =X
Last edited on
I don't know how to use algorithms?


Do you mean C++ STL algorithms? If you are totally new to STL aka Standard Template Library, it may take you a while to get used to it's syntax and usage. But once you cross your hurdle, all will flow naturally and you will know how to call those algorithm functions.
I don't know if I used alorithms right, but I mean I don't know how to use mathematics in my programs yet to work out math programs. Like, I don't know where to start if I wanted to go about calculating the fees in this program.
Ok I see. How about not looking at the program code first. Use a piece of paper and pen and write down the fees calculation as you would as a user instead of a programmer. Then from the piece of paper you slowly 'transfer' them over to the program code.

I don't know if this approach is better but for complex algorithm I use the paper and pen approach and revise on it heavily until I know I get it right will I commit to write the code in the program. At all times, my program code will reflect what I have on the paper I wrote earlier.
Okay, I believe this is the correct math. Do you know how I would make an if statement for each kind of customer from this:


Residential: $15(which is water base fee) + $0.02 x (number of gallons over 1000 in a month)
Business: $25(which is water base fee) + $0.03 x (number of gallons over 2000 in a month)
Non-profit & government: $5(which is water base fee) + $0.01 x (number of gallons over 500 in a month)
Sewage base fee = water base fee for all customers
Sewage usage fee = 0.80 or 80% of the water usage of all customers
You would do something along the lines of:

1
2
3
4
5
6
7
8
9
10
if(customerType == "R" || customerType =="r"){ //testing both R and r, as they might type it in either way
	//calculation for residential customers here
}else if(customerType == "B" || customerType =="b"){
	//calculation for Business customers here
}else if(customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np")
	//as government and non profit are the same you may as well use the same one
}else{
	cout << "incorrect input";
	//do something about the error
};


You should have a look at this though and learn about them properly. Learning to use if statements is something you simply most know to do any programming at all.
http://www.cplusplus.com/doc/tutorial/control/

and for the calculations, it is much what you you think, just use + - * / and I would recommend using loads of brackets all the time, until your more comfortable with it.

Meerkat
Okay, I know this isn't right, but do you know what I can do about the different number of gallons? Let's say I use 1001 gallons for R, 2001 gallons for B, and 501 gallons for G.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;


int main () 
{
    
    double gallons;
    string name;
    cout << "Please enter full name: ";
    getline (cin,name);
    string customerType;
cout <<"What type of customer are you?" << endl
  <<"Enter \"R\" for residential customers, \"B\" for business customers," << endl 
  <<"\"G\" for government or \"NP\" for non-profit customers." << endl;
getline (cin,customerType);
    cout<<"Enter Gallons :";
    cin>>gallons;
    if(customerType == "R" || customerType =="r"){ $15 + ($0.02 x gallons)
}else if(customerType == "B" || customerType =="b"){
	$25 + ($0.03 x gallons)
}else if(customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np")
	$5 + ($0.01 x gallons)
}else{
	cout << "incorrect input";
	//do something about the error
};
    
  
    
    
system("pause");
    return 0;

}
Ah so in your program will there one residental customer, one buisness customer, one non-profit customer and one govermental customer? And each of these will have used a different amount of water? And the name of the name that it asks for is just the person who is inputting the information, not the person that actually used the water?

Meerkat
Last edited on
Yep yep, there will be a different set of values(fee totals) for each type of customer. So basically, there's a different set for resident, business, and non-profit/government is one in the same. And I think the name actually is the person that used the water. Like, in the example above, Jones Count Junior College would probably be a business customer.
@shaunL.. Is there a reason you are using an 'x' in the
1
2
3
4
5
6
7
8
9
10
11
12
 if(customerType == "R" || customerType =="r")
{
        $15 + ($0.02 x gallons)
}
else if (customerType == "B" || customerType =="b")
{
	$25 + ($0.03 x gallons)
}
else if (customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np")
{
	$5 + ($0.01 x gallons)
}
instead of the '*' for multiplication? Also, you need to use semi-colons after the dollar amount + (cent * gallons);
Aha I forgot the multiplication sign was "*" instead of "x". Sorry xD and are the semicolons in the right spot after the math because it's not compiling right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;


int main () 
{
    
    double gallons;
    string name;
    cout << "Please enter full name: ";
    getline (cin,name);
    string customerType;
cout <<"What type of customer are you?" << endl
  <<"Enter \"R\" for residential customers, \"B\" for business customers," << endl 
  <<"\"G\" for government or \"NP\" for non-profit customers." << endl;
getline (cin,customerType);
    cout<<"Enter Gallons :";
    cin>>gallons;
   if(customerType == "R" || customerType =="r")
{
(        $15 + ($0.02 * gallons);
}
else if (customerType == "B" || customerType =="b")
{
(	$25 + ($0.03 * gallons);
}
else if (customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np")
{
(	$5 + ($0.01 * gallons);
}else{
	cout << "incorrect input";
	
};
Yea but is the program meant to be for one person to input the amount of gallons that they used and the type of customer that they are and the program tells them how much money they will therefore have to pay for it. (which is what I had originally thought)

Or is it for an admin person who has a list of different names of people and how much water they used infront of him/her and they just kind of go down the list typing in the information for each and it computes the total at the end?

It might seem like not a very big difference but if it is the first one, then you have almost finished it. You just need to introduce a variable to save the amount that the person owes and then output it at the end.

Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    cout<<"Enter Gallons :";
    cin>>gallons;
    float amountDue;
    if(customerType == "R" || customerType =="r"){ 
        amountDue = (15 + (0.02 * gallons));
}else if(customerType == "B" || customerType =="b"){
	amountDue = (25 + (0.03 * gallons));
}else if(customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np"){
	amountDue = (5 + (0.01 * gallons));
}else{
	cout << "incorrect input";
	//do something about the error
}
cout << "The total amount that you must pay for your water usage is: " <<amountDue<<endl;


Note the fact that the first 1000 gallons or whatever is included in the base rate is note taken into account for, I will let you have a think about how you want to do that.

However, if it is the second way that I had said, you'll need to make it a little more complicated. Try and get the above code finished properly before you worry about anything else. Let me know if you need help.

Meerkat
Last edited on
Your first thought was right. One person should be prompted for the amount of gallons that they used and type which kind of customer they are. The program should then display the water base fee, the water usage fee, the sewage base fee, the sewage usage fee, and the total amount due. Could I use waterbasefee, waterusagefee, sewagebasefee, and sewageusagefee as floats like you did with "amountDue"?
Yea you could, and then just within each of them if statements set the waterBaseFee etc.

When your naming variables it can be sometimes difficult to read it if there are several words and all small cases. It's normally better to either use camel case (http://en.wikipedia.org/wiki/CamelCase) or seperate each word with underscores. (I prefer camel case).

then just before the final cout line in my code above have something like:

1
2
3
cout << "The base water fee is: " << waterBaseFee <<endl
//something similar for each of the others
cout << "The total amount that you must pay for your water usage is: " <<amountDue<<endl;


Meerkat
Last edited on
Okay, this is the home-stretch aha. The program compiles, but do you know where and how I can insert the "setprecision" line in the source code to output like regular dollar amounts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;


int main () 
{
    
    double gallons;
    string name;
    cout << "Please enter full name: ";
    getline (cin,name);
    string customerType;
cout <<"What type of customer are you?" << endl
  <<"Enter \"R\" for residential customers, \"B\" for business customers," << endl 
  <<"\"G\" for government or \"NP\" for non-profit customers." << endl;
getline (cin,customerType);
   cout<<"Enter Gallons :";
    cin>>gallons;
    float amountDue, waterbaseFee, wusageFee, sewagebaseFee, susageFee;
    if(customerType == "R" || customerType =="r"){ 
        amountDue = (15 + (0.02 * gallons));
}else if(customerType == "B" || customerType =="b"){
	amountDue = (25 + (0.03 * gallons));
}else if(customerType == "G" || customerType =="g" || customerType == "NP" || customerType =="np"){
	amountDue = (5 + (0.01 * gallons));
}else{
	cout << "incorrect input";

}
cout << "The base water fee is: " << waterbaseFee <<endl;
cout << "The water usage fee is: " << wusageFee <<endl;
cout << "The base sewage fee is: " << sewagebaseFee <<endl;
cout << "The sewer usage fee is: " << susageFee <<endl;
cout << "The total amount that you must pay for your water usage is: " <<amountDue<<endl;
    
  
    
    
system("pause");
    return 0;

}
Hmm, I think you might want to use setfill('0')
http://www.cplusplus.com/reference/iostream/manipulators/setfill/

and setw(2)
http://www.cplusplus.com/reference/iostream/manipulators/setw/

cout << "The total amount that you must pay for your water usage is: " << setfill('0') << setw(2)<< amountDue << endl;

That might not work. And actually the more I think about it the less I think it will work. But reading though the links I gave you wil hopefully put you on the right track. If not google is your friend. You won't be the first person trying to do solve this problem.

Meerkat
Also if you do find it out, make sure to let me know, as I'm sure I'll want to do this as well some day.

Meerkat
Hmm, strangely it works for the total, but not the first four. On a side note, I'd like to thank you for walking me through this program. Sorry if I sounded annoying, but I really do appreciate the help you've given me.
Pages: 12