hman123456 hman123456 - 1 year ago 80
C++ Question

Why is this program outputting this number?

I just starting learning C++. Here is my code:

#include <iostream>

using namespace std;

int main() {
double hours,rate,pay;
// get the number of hours worked
cout << "How many hours did you work?";
cin>> hours;
//Get the hourly pay rate
cout<<"How much did you get paid per hour?";
cin>> pay;
// calculates the pay
pay = hours * rate;
// Display the pay
cout<<"You have earned $" << pay <<endl;

return 0;

I have no idea why this program is outputting the wrong numbers:

How many hours did you work?19

How much did you get paid per hour?15

You have earned $4.03179e-313

Maybe I installed the IDE wrong (I am using Eclipse)?:

Answer Source

I think your cin >> pay line is wrong, because you follow it up with pay = hours * rate. Since rate is never assigned to, it just gets junk data in memory, so the output is undefined. Change cin >> pay to cin >> rate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download