I will begin by outright saying that I have no prior c++ experience so pardon my incompetence. This is my third day in class and I'm doing all I can to grasp this new concept.
This is the problem:
You go out to dinner once a week with a group of friends at the neighborhood Thai restaurant. You always have trouble dividing the bill. Write a simple program to calculate the tip and divide the total equally among a specified number of friends. The program should prompt the user for the number of people who are sharing the bill and the amount of the total bill. To give you a range of values, it should output the amount each person should pay based on both a 16% tip and a 20% tip. Display the output in monetary format.
..and this is what I have so far:
#include<iostream>
using namespace std;
void main()
{
float bill;
cout<<"Welcome to the Tip Calculator"<<endl<<endl;
cout<<"How much was your bill?"<<endl;
cin>>bill;
int people;
cout<<"How many people are in your party"<<endl;
cin>>people;
int tip;
tip=bill*0.2
result = tip/people;
cout<<"You each need to pay "<<result<<endl;
}
I'm not getting any errors but the calculations are way off. Any help would be appreciated.
#include<iostream>
usingnamespace std;
int main()
{
float bill;
cout<<"Welcome to the Tip Calculator"<<endl<<endl;
cout<<"How much was your bill?"<<endl;
cin>>bill;
int people;
cout<<"How many people are in your party"<<endl;
cin>>people;
double tip;
tip=bill*0.2;
double result = tip/people;
cout<<"You each need to pay "<<result<<endl;
}
First, you have tip as an integer type which can't hold decimal values. The outcome of tip will most likely be a decimal value so I would recommend using the float type.
Secondly, there is no type assigned to result, and since once again this value will be a decimal I would use the float type.
So I would refine your code as follows:
1 2
float tip = bill * 0.2;
float result = tip/people;