【发布时间】:2020-04-21 08:05:27
【问题描述】:
enter image description here我在 C++ 中使用 STL,但在 bucle 内,cout 无法正确打印浮点数。 我的程序将值广告到向量,然后将其传递给函数以查看条件是否存在,实际上它工作得很好但只是 cout 不说话,我已经尝试使用 printf() 但它给出了相同的结果。 注意:请给我反馈我的问题是我第一次做一个,英语不是我的母语
我的代码:
#include<bits/stdc++.h>
#include<vector>
using namespace std;
void isthereanumber(vector<float> array);
int main(){
string ans;vector<float> array;float number;
do{
fflush(stdin);
cout<<"insert a value for the vector: "<<endl;
cin>>number;
array.push_back(number);
fflush(stdin);
cout<<"would you like to keep adding values to the vector? "<<endl;
getline(cin,ans);
}while(ans.compare("yes")==0);
isthereanumber(array);
return 0;
}
void isthereanumber(vector<float> array){
float suma =0;
for(vector<float>::iterator i=array.begin();i!=array.end();i++){
for(vector<float>::iterator j=array.begin();j!=array.end();j++){
if(i!=j){
suma = suma+array[*j];
}
}
if(suma=array[*i]){
cout<<"there is a number that the addition of every number in the array except the number is equal to the number \n";fflush(stdin);
cout<<"the number is: "<<suma;/*here is the cout that doesnt works properly or perhabs is something else i don't know*/
return;
}
}
cout<<"there is not a number with such a condition: ";
return;
}
【问题讨论】:
-
你能解释一下代码是如何“不起作用”的吗?我不清楚你的问题是什么。
-
if(suma=array[*i])-- 看到那行代码有什么问题吗?您应该这样做,因为您在代码的其他地方使用了正确的比较运算符。如果这样可以解决问题,可能会作为错字关闭。 -
好的@NathanOliver 让我编辑帖子以添加捕获
-
@VirMurilloOchoa 请用文字解释,而不是图像。不是每个人都能看到图像。
-
您可能不会在这里遇到这个问题,但是在使用浮点数时要非常小心地测试是否完全相等。推荐阅读:Is floating point math broken?
标签: c++ function iterator output stdvector