trouble with a tip calculator code

I am making a code for a simple tip calculator but im having some troubles getting the right results.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double tip;
double bill;

tip=bill*.15;

cout << "Enter price of bill: $";
cin >> bill;
cout <<"Tip amount: " << tip;

return 0;
}
the problem i am having is when i enter the bill amount every time i get a different answer as the Tip amount. Can anyone help me figure out why everytime i get a random number as an answer?
It's the order in which you're doing things.

You have to get in the bill amount before you do the calculation.
The reason you're getting random numbers is because at the point where you're doing the calculation, the "bill" variable contains garbage (and by garbage, I mean whatever value is at that memory address).

This is what you need to do instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double tip;
    double bill; 

    cout << "Enter price of bill: $";
    cin >> bill; 
    tip=bill*.15;
    cout <<"Tip amount: " << tip;

    return 0;
}

Last edited on
Oh ok. That makes sense thank you very much :)
Topic archived. No new replies allowed.