繁体   English   中英

C++ 错误未初始化的变量

[英]C++ bug uninitialized variable

我被指示编写一个程序,用户输入两个值,可以是小数。 然后创建一个将精度更改为 4 位的 void 函数。 我创建了一个程序,它运行第一个值 (x) 就好了,但由于某种原因,它给了我一个错误,说第二个变量未初始化 (h) 任何建议将不胜感激! 我想我已经看它太久了,只是无法发现它!

下面是代码:

#include <iostream>
#include<cmath>

using namespace std;

void display_it(double x, double h);

int main(void) // getting input from user and displaying output
{
    double x, h;
    cout << "Please enter 2 values to be displayed with precision.\n";
    cin >> x, h;
    cout << x << endl;
   cout << h << endl;
   return 0;
}

void display_it(double x,double h) //does the precision change of x and h
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(4);
}

这一行:

cin >> x, h;

没有cin读入 2 个值。 它实际上只读取1个值,表达之后h后,这是,评估(这什么都不做)。 所以关于h未初始化的警告/错误是正确的。

读取 2 个值的正确方法是:

cin >> x >> h;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM