0

I am a beginner to programming. I'm writing a C++ program that a user will their pay rate and the hours worked, and then calculate the pay and hours worked then display it. I finish the program the there are two errors that I tried to fix but I still can't figure out to fix it. The errors and my code is below. Can someone help me and tell me how to fix? I'm using MSVS Express 2013.

Errors:

Error   1   error C4700: uninitialized local variable 'hours'

Error   2   error C4700: uninitialized local variable 'rate'

(It is erroring on displayWeekly(rate, hours);)

My Code:

#include "stdafx.h"

#include<iostream>

using namespace std;
void displayWeekly(double rate, int hours);
double getRate();
int getHours();

int main()
{

double rate; 

int hours;

displayWeekly(rate, hours);

double getRate();
int getHours();

rate = getRate();
hours = getHours();

system("pause");
return 0;
}

void displayWeekly(double rate, int hours)
{
double weekPay;
weekPay = rate * hours;
cout << "Weekly pay is " << weekPay << endl;
}

double getRate()
{
double rate;
cout << "Enter your Hourly rate in the Dollars and Cents = ";
cin >> rate;
return rate;
}

int getHours()
{
int time;
cout << "Please Enter in the Hours you worked" << endl;
cout << "You must Enter a whole Number = ";
cin >> time;
return time;
}
Minh Nguyen
  • 11
  • 1
  • 2
  • 4
  • 3
    What do you expect the value of `hours` to be when you pass it to `displayWeekly`? You didn't initialize it so it has an undefined value. – Neil Kirk Oct 14 '14 at 02:22
  • Didn't I initialize by inputting in the parenthesis rate and hour, or that not it whats by initializing? – Minh Nguyen Oct 14 '14 at 02:29
  • No. You declare them at the beginning of your main function but give them no value. What do you think the value of hours will be? – Neil Kirk Oct 14 '14 at 02:30
  • I am referring you to this post http://stackoverflow.com/questions/21917424/c4700-uninitialized-local-variable , you need to initialize pointer . – Saman Dec 28 '15 at 20:32

3 Answers3

2

Your new main should look something like this:

int main()
{
    double rate;
    int hours;

    //double getRate(); --> where do you think the return value is stored to?
    //int getHours();   --> 

    rate = getRate();
    hours = getHours();

    displayWeekly(rate, hours);  // --> has to go after you put values to rate & hours

    system("pause");
    return 0;
}
Edwin
  • 825
  • 1
  • 7
  • 15
0

You pass uninitialized variables to displayWeekly ,multiply them and print them which causes the error. Instead,call that function after you call getHours and getRate so that they get initialized.

Also

double getRate(); 
int getHours();

In main are unecessary.Remove them.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

Here the main mistake that you have done is ignoring the order (flow ) of your code .

int main()
{

double rate; 

int hours;

displayWeekly(rate, hours);//-> this is the func call that lead to error,cozz the arguments rate,hours are not still initialized .You calculated them after the function call.

double getRate();
int getHours();

rate = getRate();
hours = getHours();
                   //->it helps if you call display func here.
system("pause");
return 0;
}