【问题标题】:cout doesn't works properly on my program, can somebody help me?cout 在我的程序上无法正常工作,有人可以帮助我吗?
【发布时间】: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


【解决方案1】:

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

一旦我们测试 10 时输入 5,5,10,我们希望将 suma 再次设置为 0 以使其正常工作,但现在它将变为 30。 这可以通过在外循环内移动 suma 来解决。

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

【讨论】:

    【解决方案2】:

    我认为您可能有几个问题...

    在您的 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;
        }
    }
    

    【讨论】:

    • 非常有用,就像下面的答案一样,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-09
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多