Hey guys, I just finished my gen ed stuff and have started my first programming class for my CS degree. Anyways, this is the second project I was assigned.
create a program that asks the user how the service was. Depending on answer use one of two tip rates. Calculate the total cost of their meal with tip included. Make sure cost is between 0 or 1000
Professor was super insistent on us using constants
Don't know (at the moment) how to restart something when you enter something that doesn't work - so I have to tell the user to restart
My problem is this: after the cin fetches a 'y' or 'n', if the user typed something like 'yes', the 'es' is then sent to the next cin, which doesn't know how to react to letters.
Need a way to prevent the first cin from taking answer bigger than y or n and I need a way to make it so that the second cin (which gets mealcost) to not accept letters.
Code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const float TIP20=.20;
const float TIP15=.15;
const int MAXCOST = 1000;
int main() {
char service;
float tiprate, mealcost, total, tiptotal;
cout << setprecision(2) << fixed << showpoint;
cout << "Was the service tonight satisfactory?" << endl;
cin >> service;
if (service == 'y' || service == 'Y') tiprate = TIP20;
else if (service == 'n' || service == 'N') tiprate = TIP15;
else {
cout << "Please enter a y or n and try again!" << endl;
return 1;
}
sleep(2);
cout << "Thank you for your feedback. Please enter the cost of your meal tonight" << endl;
cin >> mealcost;
if (mealcost < 0 || mealcost > MAXCOST ) {
cout << "Error: You may only enter a number between 0 and 1000. Please try again." << endl;
return 1;
}
tiptotal = (tiprate * mealcost);
total = (mealcost + tiptotal);
cout << "Your meal cost was " << mealcost << "$" << endl;
cout << "Your tip was " << tiptotal << "$" << endl;
cout << "The total cost of your meal with tip tonight is " << "$" << total << endl;
return 0;
}
thanks!