这在C++中不正确吗?
Isn't this correct in C++?
#include <iostream>
using namespace std;
int main()
{
int szam,tart;
cout << "Num=";
cin >> szam;
while(szam!=tart){
tart=szam;
cout << "Now insert a number which is not " << tart << "n Your number is=";
cin >> szam;
if(szam==tart)
cout << "And you failed.";
}
return 0;
}
此代码不正确吗?我的老师一直说这是不正确的,因为我应该称呼" t"变量的值,例如" cin>> tart"。
请尽快回答。
由于 tart
在使用之前尚未分配给(在比较中),因此初始比较的结果是不确定的行为。
它是不形式的海湾合作委员会,甚至会给您一个诊断:
main.cpp:13:15: warning: 'tart' may be used uninitialized in this function [-Wmaybe-uninitialized]
while(szam!=tart){
~~~~^~~~~~
使用非专业化变量是未定义的行为。
这是正确的,但是您应该给蛋t的值一个值。否则,它将使用随机的一个。
" int tart;"确实创建了一个名为int类型的变量,但是未设置其值(未定义),这意味着如果使用它,它可以并且(大多数时间)将导致错误。
#include <iostream>
using namespace std;
int main()
{
int szam;
int tart=0;//need to initialize or need to call cin >> tart;
cout << "Num=";
cin >> szam;
while(szam!=tart){
tart=szam;
cout << "Now insert a number which is not " << tart << "n Your number is=";
cin >> szam;
if(szam==tart)
cout << "And you failed.";
}
return 0;
}
相关文章:
- 使用2个键的cpp-stl::优先级队列排序不正确
- 正弦级数方程计算不正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 密码登录程序将永远循环并显示不正确的结果
- 在C++中返回不正确的楼层函数值
- 形状对象的旋转和缩放不正确C++
- C++ 读取文件读取文件不正确
- 关于类的 Python 文档 - 对C++的引用不正确
- 特征 LLT 模块给出不正确的结果?
- glibcxx STL 在实现 std::valarray::sum() 时是否不正确?
- 如果语句逻辑不正确
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 不正确的操作数类型 MSVC
- 数组填充了不正确的值
- 我是否不正确地集中了这些字符数组?
- 为什么除非我使用 cout,否则我的值不正确?
- 计算幂级数的数学结果不正确
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- atoi() 在应用于大型命令行参数时会产生不正确的值
- 不正确的比较和交换计数器输出用于快速排序功能