My Cout repets itself twice, how do i stop it?

Below is the code for my electricity bill calculator.
and once i enter the values it prints "your electricity bill is " twice. how do i stop it?

#include <iostream>
#include <string>
using namespace std;

int main() {

string name;
int amount;
float cost = 0;
float cost2 = 0;
float cost3 = 0;

// ask their name
cout << "Please enter yor name: ";
cin >> name;

// ask the unit consumed
cout << "Enter units of electricity used: ";
cin >> amount;

// calulation to the bill
while (amount<0)
{
cout << "negative value, please enter units again. ";
cin >> amount;
}


while (amount <= 200)
{
cost = amount * 0.1 + 15;
cout << name << ", your electric bill is " << cost;
cin.get();
}



while (200 < amount < 500)
{
cost2 = (200 * 0.1) + ((amount - 200) * 0.2) + 15;
cout << name << ", your electricity bill is " << cost2;
cin.get();
}

while (500 < amount)
{
cost3 = ((200 * 0.1) + ((amount - 200) * 0.2) + 15) + ((amount - 500) * 0.3);
cout << name << ", your electricity bill is " << cost3;
cin.get();
}// end calculation


}
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
44
45
46
47
48
49
int main() {

string name;
int amount;
float cost = 0;
float cost2 = 0;
float cost3 = 0;

// ask their name
cout << "Please enter yor name: ";
cin >> name; // Use getline() function

// ask the unit consumed
cout << "Enter units of electricity used: ";
cin >> amount;

// calulation to the bill
while (amount<0)
{
cout << "negative value, please enter units again. ";
cin >> amount;
}

// Instead of using while loops below, use if/else statements 
while (amount <= 200) 
{
cost = amount * 0.1 + 15;
cout << name << ", your electric bill is " << cost;
cin.get(); // Remove this
}



while (200 < amount < 500)  // Change to (200 < amount && amount < 500)
{
cost2 = (200 * 0.1) + ((amount - 200) * 0.2) + 15;
cout << name << ", your electricity bill is " << cost2;
cin.get(); // Remove this
}

while (500 < amount)
{
cost3 = ((200 * 0.1) + ((amount - 200) * 0.2) + 15) + ((amount - 500) * 0.3);
cout << name << ", your electricity bill is " << cost3;
cin.get(); // Remove this
}// end calculation


}
Last edited on
Below is the question i have from my class.

Demonstrate your ability to use C++ syntax to develop code to calculate the electricity bill of the domestic users. The program uses a loop to prompt the user to enter the name of the user and units consumed and calculates the charges based on the following:

 For the first 200 units – 10c per unit
 For the next 300 units - 20c per unit
 Beyond 500 units (that is 501 and above)– 30c per unit

All uses are charged a minimum of $15. If the total amount is more than $100 then an additional surcharge of 15% of the total amount is added.

The program prints the total charges for the user with the name. The loop is control via the sentinel value, ‘E’.

how do i do it with a sentinel value?
below is the program i have

#include <iostream>
#include <string>
using namespace std;

int main() {

string name;
int amount;
char reenter;
float cost = 0;
float cost2 = 0;
float cost3 = 0;
float cost4 = 0;
reenter = 'E';


// ask their name
cout << "Please enter yor name: ";
cin >> name;

// ask the unit consumed
cout << "Enter units of electricity used, enter E to continue: ";
cin >> amount;

// calulation to the bill
while ()
while (amount < 0)
{
cout << "negative value, please enter units again. ";
}


while (amount <= 200)
{
cost = amount * 0.1 + 15;
cout << name << ", your electric bill is " << cost << endl;
cin.get();
}

while (200 < amount < 500)
{
cost2 = (200 * 0.1) + ((amount - 200) * 0.2) + 15;
if (cost2 > 100)
{
cost4 = (cost2 *0.15) + cost2;
cout << name << ", your electricity bill is " << cost4 << endl;
}
else
{
cout << name << ", your electricity bill is " << cost2 << endl;
cin.get();
}

}


while (500 < amount)
{
cost3 = ((200 * 0.1) + ((amount - 200) * 0.2) + 15) + ((amount - 500) * 0.3);
cout << name << ", your electricity bill is " << cost3 << endl;
if (cost3 > 100)
{
cost4 = (cost3 *0.15) + cost3;
cout << name << ", your electricity bill is " << cost4 << endl;
}
else
{
cout << name << ", your electricity bill is " << cost3 << endl;
cin.get();
}
cin.get();
}



// end calculation
cin.get();
return 0;


}
You need to understand the difference between if/else and while loop.
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
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

	int units, totalUnits;;
	bool E = true; // Sentinal value
	double cost;
	string name;
	cout << "Enter your name: ";
	getline(cin,name); // Reads the name including whitespace
	while(E == true){ // Loop keeps running as long as the sential value stays true
		cout << "Enter the number of units used: ";
		cin >> units;
		while(units < 0){ // Error check
			cout << "You cannot have a negative number, please re-enter the number: ";
			cin >> units;
		}
		totalUnits += units; // Keeps track of total unit
		// The price range for the units
		if(totalUnits <= 200){ 
			cost += units * .1;
		}
		else if(201 < totalUnits && totalUnits <= 500){
			cost += units *.2;
		}
		else{
			cost += units * .3;
		}
		// Asks if the user would like to input more information. 0 == false, 1 == true. 0 will terminate the loop
		cout << "Would you like to contiue the program? Type '0' to end and '1' to enter more units to your bill.";
		cin >> E; 
	}
	// Minimum is $15, therefore the price will have to be at least 15
	if(cost < 15){
		cout << name << " you are being charged: $15.00\n";  
	}
	// If the price is over 100, add 15% surcharge
	else if(cost > 100){
		cost *= 1.15;
		cout << "We will be adding a 15% surcharge to your bill.\n";
		// Fixed and setprecision allows you to have 2 digits after decimal place
		cout << name << " you are being charged: $" << fixed << setprecision(2) << cost << '\n'; 
	}
	else{
		cout << name << " you are being charged: $" << fixed << setprecision(2) << cost << '\n'; 
	}
    return 0;
}
Topic archived. No new replies allowed.