Tip Calculator Program

Hello programmers!

I am completely new at programming and have a question regarding an assignment for my C++ course. There has already been a thread posted about this problem, but for my version I can't seem to run the damn thing. It's not complete yet as I haven't written out the cout<<"For 16% tip, you each need to pay$"

The assignment is as follows:

/* 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. */


Here's what I have
#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;

float tip = bill*0.2;
float result==tip/people
cout<<"For 20% tip, you each need to pay$"<<result<<endl;

}


Thanks for any help, I'm very confused! It keeps displaying an error but I don't know what or how to fix it.
i'm confused about the 16% and 20% tip. what is it?
i just thought you'd like a program like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace 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;

float tip1 = bill*0.2;
float tip2 = bill*0.6;
float result1=tip1/people;
float result2=tip2/people;

cout<<"For 20% tip, you each need to pay$"<<result1<<endl;
cout<<"For 60% tip, you each need to pay$"<<result2<<endl;
system("pause");
return 0;
}


i'm using a dev c++ compiler by the way. hope this helps.
Last edited on
"float result == tip/people" will break it...

try

"float result = (tip/people);"

just a tip, you should include the error messages you're getting when you post on here, it helps a tonne.

{edit}
Also a note: when you're using floating numbers, you should put a f after the number just to force it...

so "float tip = bill * 0.2;" -> "float tip = bill * 0.2f;"
Last edited on
Hi guys, thanks for responding so quickly.

I tried both your guys' suggestions and used the parentheses for my float results as well as tidied up the 2 different tips. It's still displaying an error, however. seriphis: oh, one question. Why do we have to force the number to float? When you mention that I should put an f at the end of my float tip formula?

Here is my code so far:



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

float tip1 = bill*0.2f;
float tip2 = bill*0.6f;
float result1=(tip1/people);
float result2=(tip2/people);

cout<<"For 20% tip, you each need to pay $"<<result<<endl;
cout<<"For 60% tip, you each need to pay $"<<result2<<endl;

}




Here's the errors:

1> Touching "Debug\lab 2.unsuccessfulbuild".
1>ClCompile:
1> Source1.cpp
1>c:\users\muggy\documents\visual studio 2010\projects\source1.cpp(20): error C2065: 'result' : undeclared identifier
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.89
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
i think it depends on what compiler you are using. i'm using dev c++ compiler and the codes i have given runs just fine.
typo... :P

"float result1=(tip1/people);"
vs
"cout<<"For 20% tip, you each need to pay $"<<result<<endl;"

As far as explicitly defining floats using f... its a habit and data security thing...

0.2 is a double, as well as a float. Allowing the system at runtime to figure it out can cause all sorts of funky results, especially with implicit casting, i've seen cases where an int divided by a double, results in an int (even if there is a remainder), where as an explicitly defined float (using the f) results in a float... and all that changed was an f was added...


Last edited on
@Muggy
i can't see any result declared in your code.
correct the line 20::

cout<<"For 20% tip, you each need to pay $"<<result<<endl
-------------->

cout<<"For 20% tip, you each need to pay $"<<result1<<endl
Hey guys, I changed the typo to result1.. but it still won't run. Here are the errors when i copy+paste onto a new .cpp and run it.

Error 1 error LNK2001: unresolved external symbol _mainCRTStartup c:\Users\cisb11\documents\visual studio 2010\Projects\adfadf\adfadf\LINK adfadf

Error 2 error LNK1120: 1 unresolved externals c:\users\cisb11\documents\visual studio 2010\Projects\adfadf\Debug\adfadf.exe adfadf
i would like to suggest to you to use dev c++ compiler. it would make things much lighter.
Do not use Dev C++'s compiler. It is ancient and outdated.

As for your problem... is the new cpp file added as part of your project? (why did you make a new cpp file anyway? why not just use the old one?)

Anyway, make sure it's saved to the disk, then go to Project | Add Existing Item and add the new cpp file to the project. Then rebuild.
Do not use Dev C++'s compiler. It is ancient and outdated.


The project is running again, just dont get it from the bloodshed site... find it from the developer's blog. Current version, iirc is 5.2.somethingsomething.

back on topic...

When rebuilding, make sure you clean the build first, you'll have left overs of the previous build

Awesome! I got it to work. I'm not sure what I did, but I tried starting a new project, empty file, pasting my code into that and saving it. And it finally ran, with a few tweaks to the code! Thanks a lot guys, I love this place :)

Here's my code for the curious:

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


float tip1 = bill*0.2f;
float tip2 = bill*0.16f;
float result1=(bill+tip1)/people;
float result2=(bill+tip2)/people;

cout<<"For 20% tip, you each need to pay $"<<result1<<endl;
cout<<"For 16% tip, you each need to pay $"<<result2<<endl;
system("PAUSE");

}
Last edited on
you're welcome, glad that we can help you anyway!!!
Topic archived. No new replies allowed.