using bool to exit do while loop

I am trying to make this program for my intro to c++ class. I have everything figured out except the bool to exit the do while loop. No matter if I put in y or n it loops forever all on its own. What am I missing?

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double days;
bool repeat = true;
int counter=1;
double pay=.01;
double runningTotal= 0.01;
do
{
cout << fixed << showpoint << setprecision(2);
cout<< "How many days did the employee work this month? ";
cin >> days;

while (days >31 || days < 1)
{
cout <<"Your input must be between 1 and 31." << endl ;
cout << "Try again: " ;
cin >> days ;
}
cout << "DAYS << setw(15) << endl ;
cout << "-------------------" << endl ;
cout << counter << setw(18) << pay;
cout << endl;
counter++;

for (counter ; counter <= days ; counter++)
{

pay += pay;
cout << counter << setw(18) << pay;
cout << endl;
runningTotal += pay;
}
cout << "-------------------" << endl;
cout<< "Total " << runningTotal << endl ;
cout << "Do you want to try again? (y/n) " ;
cin >> repeat ;
}while (repeat);

system("pause");
return (0);
}
Your repeat variable its of type bool what you expect to be entered by?:
cout << "Do you want to try again? (y/n) " ;
cin >> repeat ;


char,string,zero/one? you can't do that in a bool variable.

hint:
if assign: repeat=0; repeat variable is equal to false.
Last edited on
Thanks Eyenrique but I still am not getting it... I switched it to a char and got it.
cout << "Do you want to try again? (Y/N) " ;
cin >> answer ;
while (answer != 'n' && answer != 'N' && answer != 'Y' && answer != 'y')
{
cout << "You must enter (Y/N): ";
cin >> answer;
}
}while (answer != 'n' && answer != 'N');
Compile,run and look what happens in this example:

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
//percentage.cpp
//tip calculator

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

double getTipPercentage(double total);

int main(){

int total;
bool exit=false;

do{
cout<<"Enter total bill to calculate tip: ";
cin>>total;
cout<<"\nTotal :"<<total<<'\n'
	<<"Tip: "<<getTipPercentage(total)<<endl;
cout<<"Do you want to try again? (0 zero to exit 1 continue)";
cin>>exit;
}while(exit);

return 0; //indicates success
}//end main

double getTipPercentage(double total){
	double tip;
		tip=total*0.20;
return tip;
}//end function getTipPercentage 



Eyenrique-MacBook-Pro:Study Eyenrique$ ./percentage 
Enter total bill to calculate tip: 120

Total :120
Tip: 24
Do you want to try again? (0 zero to exit 1 continue)1
Enter total bill to calculate tip: 120

Total :120
Tip: 24
Do you want to try again? (0 zero to exit 1 continue)0
OHH ok I get it thanks!!
That's great!
can you help to me?
Allow private messages from your account settings!
Topic archived. No new replies allowed.