[enter image description here我正在c ++中使用STL,并且在气泡内,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;
}
我认为您可能有几个问题...
在for循环中,您正在创建矢量的迭代器,而不仅仅是解引用它们以访问已索引的元素,而是要对它们进行解引用,然后将其用作对相同矢量的索引。
也是您的最后一个if语句具有赋值=而不是比较==。
我相信这更接近您要实现的目标(很抱歉,我还没有时间进行编译和检查):
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+*j;
}
}
if(suma==*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;
}
}