cout 在我的程序上不能正常工作,有人可以帮助我吗?

cout doesn't works properly on my program, can somebody help me?

enter image description here我在 c++ 中使用 STL,但在 bucle 中,cout 无法正确打印浮点数。 我的程序将值添加到一个向量,然后将其传递给一个函数以查看条件是否存在,实际上它工作得很好但只是 cout 没有字,我已经尝试使用 printf() 但它给出了相同的结果。 note:please algo 给我反馈我的问题是我第一次做,英语不是我的母语

我的代码:

#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;
    }
}

正如 cleggus 所说,已经存在一些问题。这些需要首先解决。在那之后有一个逻辑错误 suma 一直在增长。

给定输入 5、5、10,一旦我们测试 10,我们希望 suma 再次设置为 0 以使其工作,但现在它会变成 30 之类的东西。 这可以通过在外循环中移动 suma 来解决。

输入 5、5、10 的工作示例:https://godbolt.org/z/gHT6jg