quick little divison problem

Hey guys, I need to create a program that will:

take a meal total, tip amount, and output the meal total, tip amount and tip+meal.

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
36
37
38
39
40
41
42
43
44

#include <iostream>
using namespace 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.

Welcome to the forum.

EDIT: -Albatross
Last edited on
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;
}
Last edited on
Topic archived. No new replies allowed.