Cant Find the Error

As you may guessed i am trying to write a code where the user puts the amount of water that they use and the program should show the price depending on the amount of usage if its less than 10m3 it is multipled by 2.35 it is between 10 and 20 it is multiplied by 3.54 and if it is more than 20 it is multiplied by 4.71.
but somehow when i type 9 it gives me 2 prices it multiplies 1 of them with 2.35 and the other one with 3.54.
My code may look sluggish changed it so much i lost track anymore so sorry :(

btw does else need a statement ?

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
  #include <iostream>

using namespace std;


int main() {

	double water;
 
	cout << "Please enter the amount of water you have used \t";
	cin >> water;
	if (water < 10) {
		double x = 2.35;
		double price = water*x;
		cout << "price:" << price << endl;
	}
	if (water > 20) {
		double x = 4.71;
		double price = water*x;
		cout << "price:" << price << endl;
	
	 else ();
		double x = 3.54;
		double price = water*x;
		cout << "price:" << price << endl;
	}

	return 0;
}
Last edited on
It is because you have two different if statments essentially.

One says if under 10 do this

one says if over 20 do this otherwise if under 20 do this. You need to combine your if statments to look like this:


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
#include <iostream>

using namespace std;


int main() {

	double water;
 
	cout << "Please enter the amount of water you have used \t";
	cin >> water;
	if (water < 10) {
		double x = 2.35;
		double price = water*x;
		cout << "price:" << price << endl;
	} else if (water > 20) {
		double x = 4.71;
		double price = water*x;
		cout << "price:" << price << endl;
	} else {
		double x = 3.54;
		double price = water*x;
		cout << "price:" << price << endl;
	}

	return 0;
}
Thank you
Topic archived. No new replies allowed.