Guest_imported
New member
- Jan 1, 1970
- 0
When I enter a letter this goes into an infinite loop asking to input correct data but not waiting for input.
float getGallons()
{
/* Description: Gets # of gallons for one purchase.
Preconditions: None
PostConditions: Returns gallons for 1 purchase
PseudoCode:
----------------------------------------
While Gallons is not correct
Prompt and input gallons
If cin stream is bad or gallons <= 0
Display error and get gallons again
If gallons is > 50
Make sure gallons is correct
return gallons
---------------------------------------- */
float gallons=0;
char yn='n';
while ( yn == 'n' ){
yn = 'y';
cout << "How many gallons did you get at your last purchse?: ";
cin >> gallons;
while ( !cin || gallons <=0 ){
cout << "Please enter a gallon amount above 0: ";
cin >> gallons;
}
if ( gallons > 50 ){
cout << gallons << " is a large amount of gas for one car."
<< endl << "Are you sure this is the right amount? (y/n): ";
cin >> yn;
while ( yn != 'n' && yn != 'y' ){
cout << "Please type y (yes) or n (no): ";
cin >> yn;
}
}
}
return gallons;
}
float getGallons()
{
/* Description: Gets # of gallons for one purchase.
Preconditions: None
PostConditions: Returns gallons for 1 purchase
PseudoCode:
----------------------------------------
While Gallons is not correct
Prompt and input gallons
If cin stream is bad or gallons <= 0
Display error and get gallons again
If gallons is > 50
Make sure gallons is correct
return gallons
---------------------------------------- */
float gallons=0;
char yn='n';
while ( yn == 'n' ){
yn = 'y';
cout << "How many gallons did you get at your last purchse?: ";
cin >> gallons;
while ( !cin || gallons <=0 ){
cout << "Please enter a gallon amount above 0: ";
cin >> gallons;
}
if ( gallons > 50 ){
cout << gallons << " is a large amount of gas for one car."
<< endl << "Are you sure this is the right amount? (y/n): ";
cin >> yn;
while ( yn != 'n' && yn != 'y' ){
cout << "Please type y (yes) or n (no): ";
cin >> yn;
}
}
}
return gallons;
}