HELP!

someone please tell me what im doing wrong! im trying to write a simple program to calculate hours*rate=pay in using...
#include<iostream>

using namespace std;

int main()

{

double hours, rate, pay;

pay=hours*rate;

cout<<"How many hours did you work?";
cin>> hours;
cout<<"What is your pay per hour?";
cin>> rate;
cout<< pay;


system("pause");
return 0;
}
but it keeps auto assigning numbers for hours, rate, and pay??? and never uses the numbers i input!
The = operator assigns pay the value of whatever hours and rate were at the exact instant that line is computer (simplification), meaning if hours's and rate's values change then pay won't change with them.

Hint: Move that line to somewhere after cin >> rate;.

Happy coding!

-Albatross
You have only declared rate and pay at: double hours, rate, pay , you didn't initialize it, but you have used it at pay = hours * rate; . Just move that line to below cin >> rate; .

Happy coding!
HINT: [code]sad code[/code] -> happy code
closed account (zb0S216C)
A variable that is defined but not initialized contains unused memory, or more commonly referred to a garbage. That's the value you're seeing. It's easy to initialize a variable:

1
2
3
4
5
6
int main( )
{
    int Var1;       // Uninitialized.
    int Var2( 0 );  // Initialized.
    int Var3 = 0;   // Initialized. 
}


kcomp11 wrote:
 
pay=hours*rate;

If you print this, you'll probably get something like this: 7234792. This is because of those uninitialized variables. Initialize them and do what the two previous posts suggested, and you'll be laughin'.

Wazzak
Last edited on
Thank you all so much, such a simple mistake!
Topic archived. No new replies allowed.