Hey guys, I'm here once again! I need help for something that is simple, but I'm not getting the results I should be getting!

I want my output to look like this:

Enter hours worked: 44

Enter pay rates: 10

Hours worked: 44
Pay Rate: 10
This week's salary: 460
Enter an integer
7
In main: x = -7 and y = 0
Press any key to continue....

I know that seems a lot, but I got the code and everything working. I am having problems getting the number I want for "This week's salary which is 460" and " x = -7 and y = 0 ". If you guys can help me with this problem, I would appreciate it.

[LIST=1]
[*]#include <iostream>
[*]#include <iomanip>
[*]using namespace std;

[*]void getHoursRate (double& hours, double& rate);
[*]void printCheck (double hours,double rate,double& amount);
[*]void initialize (int& x,int& y,char& z);
[*]void funcOne (int& x,int& y);
[*]int main ()
[*]{
[*]         int x = 0;
[*]         int y = 0;
[*]         char z;
[*]         double rate, hours;
[*]         double amount;
[*]         
[*]         getHoursRate(hours,rate);
[*]         printCheck(hours,rate,amount);
[*]         initialize (x,y,z);
[*]         funcOne (x,y);
[*]         
[*]         
[*]         system ("PAUSE");
[*]         return 0;
[*]}

[*]         void getHoursRate(double& hours,double& rate)
[*]         {
[*]         double amount; 
[*]         double perhour = 1.5;
[*]         
[*]         cout<<"Enter hours worked: "<<endl;
[*]         cin>>hours;
[*]         cout<<"Enter pay rate: "<<endl;
[*]         cin>>rate;
[*]         
[*]         if (hours <= 40)
[*]         
[*]         amount = (hours * rate);
[*]         
[*]         else if (hours >= 40)
[*]         
[*]         amount = (hours * rate + perhour);
[*]         }
[*]         
[*]         
[*]         void printCheck(double hours,double rate,double& amount)
[*]         {
[*]         cout<<"Hours worked: "<<hours<<endl;
[*]         cout<<"Pay Rate: "<<rate<<endl;
[*]         cout<<"This week's salary: "<<amount<<endl;
[*]         }
[*]         
[*]         void initialize (int& x,int& y,char& z)
[*]         {
[*]          int num;
[*]          cout<<"Enter Integer"<<endl;
[*]          cin>>num;
[*]         }
[*]         
[*]         void funcOne(int& x,int& y)
[*]         {
[*]         cout<<"In main: x = "<<x<<" and y = "<<y<<endl;
[*]         }
[/LIST]

One thing I forgot to point out. There are 40 hours in a work week and the rate is 10 dollars. When an hour above 40 such as (41,42,43) etc., the rate is 1.5 for addition hour above 40. I forgot to mention that. Sorry

The extra is
( hours - 40 ) * ( rate * 1.5 )

the amount that i get in the output for the week's salary is 1.62537e-307. I get that with any number I put in for hours and rate. o_O

Look at how many times you've declared amount, and how they're related to one another.

If you didn't get it from the above post:

on line 14, you declare amount as a double, but don't initialize it to anything. then, in the getHoursRate() function, you declare a SECOND double called amount, which will go out of scope as soon as the function ends. you're saving the value you want for amount in getHoursRate()'s version of amount, and then, on line 16, your using main()'s version of amount, which is still uninitialized, and that's why you get some really weird number for your output.

getHoursRate() either needs to take a third reference to amount, or it needs to return the amount, which you would then store in main()'s version of amount.

Edit: you've got a simmilar situation with initialize. you've got three parameters, but you're not doing anything with them

I figured out the placement for amount, and I settled that correctly. For my calculations, When I did code posted above:

amount = ( hours - 40 ) * ( rate * 1.5 );

I am recieving the answer 60 instead of 460.


Here is my changed code:

[LIST=1]
[*]#include <iostream>
[*]#include <iomanip>
[*]using namespace std;

[*]const double perhour = 1.5;

[*]void getHoursRate (double& hours, double& rate);
[*]void printCheck (double hours,double rate,double& amount);
[*]void initialize (int& x,int& y,char& z);
[*]void funcOne (int& x,int& y);
[*]int main ()
[*]{
[*]         int x = 0;
[*]         int y = 0;
[*]         char z;
[*]         double amount;
[*]         double rate, hours;
[*]         
[*]         getHoursRate(hours,rate);
[*]         printCheck(hours,rate,amount);
[*]         initialize (x,y,z);
[*]         funcOne (x,y);
[*]         
[*]         system ("PAUSE");
[*]         return 0;
[*]}

[*]         
[*]         void getHoursRate(double& hours,double& rate)
[*]         {

[*]         cout<<"Enter hours worked: "<<endl;
[*]         cin>>hours;
[*]         cout<<"Enter pay rate: "<<endl;
[*]         cin>>rate;
[*]         }
[*]         
[*]         
[*]         void printCheck(double hours,double rate,double& amount)
[*]         {
[*]         if (hours <= 40)
[*]         
[*]         amount = (hours * rate);
[*]         
[*]         else if (hours >= 40)
[*]         
[*]         amount = (( hours - 40 ) * ( rate * perhour));
[*]         
[*]         cout<<"Hours worked: "<<hours<<endl;
[*]         cout<<"Pay Rate: "<<rate<<endl;
[*]         cout<<"This week's salary: "<<amount<<endl;
[*]      
[*]         }
[*]         
[*]         void initialize (int& x,int& y,char& z)
[*]         {
[*]          int num;
[*]          cout<<"Enter Integer"<<endl;
[*]          cin>>num;
[*]         }
[*]         
[*]         void funcOne(int& x,int& y)
[*]         {
[*]         cout<<"In main: x = "<<x<<" and y = "<<y<<endl;
[*]         }
[/LIST]

Allright, so as I understand it: the fact that perhour = 1.5 means that for any hours worked over 40 hours, you receive 1.5 times your regular rate (perhour seems like a weird name for that). If that's the case, then this line (35):

amount = (( hours - 40 ) * ( rate * perhour));

is only calculating the EXTRA money earned. Not the total money earned. To get the total, you would replace the line above with:

amount = (hours*rate) + (( hours - 40 ) * ( rate * perhour));

The amount went to 500. I need it to be 460 though. I feel we are almost there though.

The amount went to 500. I need it to be 460 though. I feel we are almost there though.

Sorry - it's

amount = (40*rate) + (( hours - 40 ) * ( rate * perhour));

That's should do it.

And just fyi, on line 34 you have

else if (hours >= 40)

. This won't affect your program at all, but it should be:

else if (hours > 40)

, since if amount == 40, you would want the first if block to handle the code

I figured it out.

amount = (40 * rate) + ((hours - 40) * (rate * 1.5)); gave me the answer I needed


I still need help with the 2nd part of my output.

Enter hours worked: 44

Enter pay rates: 10

Hours worked: 44
Pay Rate: 10
This week's salary: 460
Enter an integer
7
In main: x = -7 and y = 0
Press any key to continue....

I keep getting x = 0 and y = 0.

Do you guys know where I am doing something wrong?

Updated Code:

[LIST=1]
[*]#include <iostream>
[*]#include <iomanip>
[*]using namespace std;

[*]void getHoursRate (double& hours, double& rate);
[*]void printCheck (double hours,double rate,double& amount);
[*]void initialize (int& x,int& y,char& z);
[*]void funcOne (int& x,int& y);
[*]int main ()
[*]{
[*]         int x = 0;
[*]         int y = 0;
[*]         char z;
[*]         double amount;
[*]         double rate, hours;
[*]         
[*]         getHoursRate(hours,rate);
[*]         printCheck(hours,rate,amount);
[*]         initialize (x,y,z);
[*]         funcOne (x,y);
[*]         
[*]         system ("PAUSE");
[*]         return 0;
[*]}

[*]         
[*]         void getHoursRate(double& hours,double& rate)
[*]         {

[*]         cout<<"Enter hours worked: "<<endl;
[*]         cin>>hours;
[*]         cout<<"Enter pay rate: "<<endl;
[*]         cin>>rate;
[*]         }
[*]         
[*]         
[*]         void printCheck(double hours,double rate,double& amount)
[*]         {
[*]         if (hours <= 40)
[*]         
[*]         amount = (hours * rate);
[*]         
[*]         else if (hours >= 40)
[*]         
[*]         amount = (40 * rate) + ((hours - 40) * (rate * 1.5));

[*]         
[*]         cout<<"Hours worked: "<<hours<<endl;
[*]         cout<<"Pay Rate: "<<rate<<endl;
[*]         cout<<"This week's salary: "<<amount<<endl;
[*]      
[*]         }
[*]         
[*]         void initialize (int& x,int& y,char& z)
[*]         {
[*]          int num;
[*]          cout<<"Enter Integer"<<endl;
[*]          cin>>num;
[*]         }
[*]         
[*]         void funcOne(int& x,int& y)
[*]         {
[*]         cout<<"In main: x = "<<x<<" and y = "<<y<<endl;
[*]         }
[/LIST]

or in main, do i make it x = -7 and y = 0? is that how it is suppose to work? cuz in the output it says " In main:"

Um, ok, first of all, what exactly is z for?

Second - look at this:

void initialize (int& x,int& y,char& z)
{
int num;
cout<<"Enter Integer"<<endl;
cin>>num;
}

This function takes in three parameters but does nothing with them. Try to fix that... I'm not entirely sure what you're trying to do here anyway

The output asks for x and y, but not z (even though z is declared in the book).

I replaced in int main ()

x = 0 (with -7)
y = 0 (stayed with 0)

and my output came out correctly. As long as my program works, it is cool.

I think the main reason for this program was to seek global identifiers and compare them with local identifiers.

Thanks for all your help guys! You never let me down. =)

Ok. Just so you know - initialize does absolutely nothing (well it, does ask for and store a number, but then it does nothing with that number and no other part of your program can access the number since it goes out of scope).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.