c++ - 错误 1 ​​错误 C4700 : uninitialized local variable 'rate' and 'hours' in C++

标签 c++

我是编程初学者。我正在编写一个 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/

相关文章:

c++ - 将错误代码映射到 C++ 中的字符串

c++ - Qt TCP 套接字 - 写入超过 15 个字节

c++ - snprintf Format String 安全漏洞问题

c++ - 如何在 ARC 环境中为 C++ 指针指定属性?

c++ - 优化求和循环

c++ - boost::变体用法

c++ - PE注入(inject)图像重定位

c++ - map 上 max_element 的模板函数需要知道这两种类型

c++ - 如何通过按 Escape 键重置 QLineEdit 文本?

c++ - c++ 11 atomic是否自动解决变量读写上的多核竞争?