c++ - 简易计算器
问题描述
大家好,我是C++自学者的初学者。今天我试着做一个简单的计算器,但调试器不断地向我显示同样的错误。使用单化变量 "X" ; 单化变量使用“Z”
这是代码:
#include <iostream>
using namespace std;
int main()
{
float x, z, a;
a = x + z;
cout << "Welcome to the calculator" << endl;
cout << "State the first number " << endl;
cin >> x ;
cout << "State the second number " << endl;
cin >> z ;
cout << "If you wanted to time number" << x << "by this number" << z << "The result would be : " << a << endl;
system("pause");
return 0;
}
解决方案
你做事的顺序很重要。
int x = 5, z = 2;
int a = x + z; // a is 7
z = 5; // a is still 7
a = x + z; // now a is updated to 10
因此,在您的代码中,当您同时执行a = x + z;
这两项操作x
并且z
未初始化时。使用未初始化的变量是未定义的行为。
要修复它,请a = x + z;
在输入值后将 移至x
和z
。
推荐阅读
- r - 使用带有 R 包的 docker buildkit 缓存
- typescript - 加载一个与捆绑包分开的库
- mysql - 如何从 varchar 转换为 float 并忽略非数字字符?
- kotlin - 有没有办法从命令行引导 Kotlin 项目?
- amazon-web-services - Lambda reading file on S3 - flushing S3 cache
- java - java - 如何在Java中没有Random类的情况下创建每个字符的随机字符顺序并且不重复
- python - 序列化器字段可能命名不正确,并且与 `list` 实例上的任何属性或键都不匹配
- javascript - 如何使用 easyimage ckeditor 将照片上传到本地服务器?
- haskell - convert list of Tupel into list of lists
- android - 在 VS2017 中无法让 Admob 在我的 Cordova Android 应用程序中工作