我是编程初学者。我正在编写一个 C++ 程序,用户将获得他们的工资率和工作时间,然后计算工资和工作时间,然后显示它。我完成了程序,有两个错误我试图修复,但我仍然无法解决它。错误和我的代码如下。有人可以帮助我并告诉我如何解决吗?我正在使用 MSVS Express 2013。
错误:
Error 1 error C4700: uninitialized local variable 'hours'
Error 2 error C4700: uninitialized local variable 'rate'
(在 displayWeekly(rate, hours);
上出错)
我的代码:
#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;
}
最佳答案
你的新 main 应该看起来像这样:
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;
}
关于c++ - 错误 1 错误 C4700 : uninitialized local variable 'rate' and 'hours' in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351690/