#include <iostream>
usingnamespace std;
int main()//beginning of main function
{
int tiptotal, meal, percent; //delcaring our variables
cout<<"Meal Tip Program \n";
cout <<"Please enter the total of the meal. $";
cin>>meal;
if (meal < 0)//this is just a little something extra to make sure the meal cost is greater than zero, this is assuming that after an incorrect amount is entered once, the user wont mis-enter it again.
{
cout<<"You cant have a total less than zero! Please re-enter the total.";
cin>>percent;
}
cout<<"Please enter the percentage to tip. %";
cin>>percent;
if (percent < 0)//this is just a little something extra to make sure the tip percentage is greater than zero, this is assuming that after an incorrect amount is entered once, the user wont mis-enter it again.
{
cout<<"You cant have a precent less than zero! Please re-enter the percent.";
cin>>percent;
}
tiptotal = (meal/100)*percent;//this is the arithmatic performed to determine the tiptotal.
cout << "*******************************"<<endl;
cout << "The meal cost: $"<<meal<<"."<<endl;
cout << "The tip cost: $"<<tiptotal<< "."<<endl;
cout << "The meal total was: $"<<tiptotal + meal<<"."<<endl;
cout << "*******************************";
}
If the meal total is entered at anything less than 100, the tip amount will be 0. I cant quite figure out what it may be, i thought declaring tiptotal as a double would solve the problem but it doesn't. Its because its turning my decimals (i.e 10/100 = .1) into 0 and I cant seem to figure out how to avoid that..thanks in advance.
Your second solution was correct; you just need to work out a few technical details.
Operations with doubles only take floats and doubles, so you need to append .0 to all your constants (i.e. 100 should become 100.0). Then, replace all ints with doubles and provided I didn't miss anything obvious, it should work.
Note: your "cin >> " statement within the first "if" block retrieves the wrong variable (should be "meal", not "percent").
Also, for future reference, it's a bad idea to assume the user will EVER do anything correctly. You should loop until a valid entry is given. Simply change your "if" block to a "while" loop:
1 2 3 4 5 6 7
cin >> meal;
while (meal < 0) //Keep looping until user enters a non-negative amount
{
cout << "You cant have a total less than zero! Please re-enter the total.";
cin >> meal;
}