为什么使用float时x,y的位置很重要?
C++ Why does the position of x,y matter while using float?
所以,我不知道如何解释这个。我是一个c++新手,如果有人能给我解释一下,我会很感激。我写float y,x而不是float x,y,程序工作得很好。编译器没有报告任何错误,但是程序没有做它被指示做的事情。它忽略了整个'while'循环,直接执行到末尾。我通过改变x和y的位置来固定它,但是为什么x,y的位置会产生差异?
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
float x,y; /*This is the problem. Why does float y,x make a difference?*/
int s=0;
int s1=0;
int max=0;
cout<<"Unesi stanje racuna:"<<endl;
cin>>y;
while(x!=0)
{
cout<<"Unesi vrijednost:"<<endl;
cin>>x;
max==x;
if (x>0) s=s+x;
if (x<0) s1=s1+x;
if(x>max) max=x;
}
cout<<"suma pozitivnih :"<<s<<endl;
cout<<"suma negativnih :"<<s1<<endl;
cout<<"najveci unos je :"<<max<<endl;
cout<<"ukupno stanje je: "<<s+s1+y<<endl;
return 0; }
位置无关紧要。float x, y;
和float y, x;
一样。这里重要的是,您的程序是病态的,因此您不能依赖输出。你检查x != 0
,但x还没有初始化,这是未定义的行为。你可能指的是y != 0
。
在while循环中使用比较之前,应该初始化x和y变量。否则,它只是随机的,如果它工作。
此外,您应该将浮动变量与浮动值(0.0,而不仅仅是0)进行比较
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何将元素添加到向量的字符串位置
- 如何选择在 csv 文件中输出的位置
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 为什么Float和double在小数位置不识别0
- 为什么使用float时x,y的位置很重要?