0

enter image description hereI am using the STL in c++ and inside a bucle the cout doesn't prints correctly a float. my program ads values to a vector and then passes it to a function to see if the condition exist, actually it works perfectly but just the cout doesn't word, I already tried using printf() but it gave the same result. note:please algo give me feedback on my question is the first time i do one and English is not my natal language

my code:

#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;
}
  • Can you please explain a little more on how the code "does not work"? It's unclear to me what your issue is. – NathanOliver Jan 02 '20 at 16:51
  • 2
    `if(suma=array[*i])` -- See anything wrong with that line of code? You should, since you use the proper comparison operator in other places in the code. If that fixes the issue, probably closing as a typo. – PaulMcKenzie Jan 02 '20 at 16:51
  • okay @NathanOliver let me edit the post to add a capture – Vir Murillo Ochoa Jan 02 '20 at 16:56
  • Probably unrelated: Watch out for [Cargo Cults](https://en.wikipedia.org/wiki/Cargo_cult_programming). If you knew what `#include` does you either would not `#include` (still wrong) or not `#include` and instead include the correct headers. [More on what's really going on](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Jan 02 '20 at 16:56
  • 1
    @VirMurilloOchoa Please explain with text, not images. Not everyone can see images. – NathanOliver Jan 02 '20 at 16:57
  • @PaulMcKenzie mm the only cout that does't works properly is the < – Vir Murillo Ochoa Jan 02 '20 at 16:58
  • @VirMurilloOchoa You still haven't stated whether you see the error in the line I posted. – PaulMcKenzie Jan 02 '20 at 16:59
  • "`#include`" <-- No. Just No. Don't *ever* do that. – Jesper Juhl Jan 02 '20 at 17:03
  • You will find that most of the time `cout` is innocent and the incorrect output is the result of an error that happened elsewhere. – user4581301 Jan 02 '20 at 17:03
  • @PaulMcKenzie sorry i really don't see the error, can you explain? – Vir Murillo Ochoa Jan 02 '20 at 17:05
  • 1
    You're probably not going to encounter this problem here, but be very careful with testing for exact equality when using floating point numbers. Recommended reading: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Jan 02 '20 at 17:06
  • The missed piece here is `=` is an assignment. `==` tests for equality, so `if(suma=array[*i])` assigns `array[*i]` to `suma`. It does not compare the two values. Typically this produces a compiler warning, so it's in your best interests to understand and resolve all of the warnings. Even though they do not halt compilation, they do tell you that something is probably not right in the logic of the program. – user4581301 Jan 02 '20 at 17:09
  • @NathanOliver okay so i add the values to the vector [5,5,10] so 10 should be what is printed in the cout because is the number that has the condition, because 5+5=10, so it correctly prints; "there is a number that the addition of every number in the array except the number is equal to the number \n" "the number is: " but where it should print 10 it prints 2.36017e+029 – Vir Murillo Ochoa Jan 02 '20 at 17:10
  • @user4581301 wow how extrange haha, i agree that is an error,but just now I already tried it and gave me the same result – Vir Murillo Ochoa Jan 02 '20 at 17:13
  • @VirMurilloOchoa -- If your vector is of `float`, you do know that your function may not work if you used fractional numbers? In other words, Try as a vector `[0.1, 0.1, 0.2]`. You will see that `0.1 + 0.1` may not equal `0.2`. – PaulMcKenzie Jan 02 '20 at 18:12

2 Answers2

0

I think you may have a couple of issues...

In your for loops you are creating iterators to the vector, but rather than just dereferencing them to access the indexed element you are dereferencing them and then using that as an index to the same vector.

Also Your last if statement has an assignment = rather than a comparison ==.

I believe this is closer to what you are trying to achieve (sorry I haven't had time to compile and check):

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

As stated by cleggus already there are some issues present. Those need to be adressed first. After that there's a logical error in that suma just keeps growing.

Given the input 5,5,10 once we test for 10 we would like suma to be set to 0 again for it to work but it will be something like 30 now instead. That can be solved by moving suma inside the outer loop.

Working example for input 5,5,10: https://godbolt.org/z/gHT6jg

koitimes3
  • 84
  • 3