Prog unable to re-enter username

Dear all,
Newbie here. My prog is unable to re-enter username after do-while loop.
please guide me.

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
#include <iostream> 
#include <string> 
#include <cctype> 
using namespace std;

int main()
{
	
	string username;
	char choice = 'Y';
	double unit, X;
	
	do
	{
		
		cout << "Please enter username"<<endl;
		getline(cin,username);
		cout << "Please enter total water usage: " " ";
		cin >> unit;
		if (usage <=10)
		X = 15;
		else if (unit >10 && unit <=25)
		X = unit *11;
		else if (unit <=55)
		X = 200 + (unit -25)*22;
		else
		{
		X = (80 + (unit - 55) * 50); 
		}
		cout <<username<<" " "your total bill is<< X << endl;
		cout << "Press any key to continue or E to exit" <<endl; 
		cin >> choice;
	}while (toupper(choice) != 'E');

} 
Last edited on
newline character '\n' is left in the input buffer after std::cin on lines 20 and 35. so you'd need
1
2
3
#include <limits>
//after lines 20 and 35:
cin.ignore(numeric_limits<streamsize>::max(), '\n');

http://stackoverflow.com/questions/25020129/cin-ignorenumeric-limitsstreamsizemax-n
Hi Gunnerfunner,
Many thanks.
so i include cin.ignore(numeric_limits<streamsize>::max(), '\n'); after if (usage <=10) and after } ?
its still dont work.. :S
Last edited on
//after lines 20 and 35:

and try and understand why you're doing what you're doing
Hi Gunnerfunner,
many thanks. will try to figure out..
Topic archived. No new replies allowed.