Boolean function not returning false

I have a bool type function and set it to explicitly return false, but I am still getting true as the return.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>


using namespace std;


int main(int argc, char* argv[]){
    
     double coeffs[3];
     double roots[2];

     int lengthc = sizeof(coeffs)/sizeof(double);
     int lengthr = sizeof(roots)/sizeof(double);


     readCoeffs(coeffs, lengthc);

     calcRoots(coeffs, lengthc, roots, lengthr);
     cout << "Testing whether the roots exist or not: " << calcRoots << endl;

     if(calcRoots){
          toFile(coeffs, lengthc, roots, lengthr);
      }
      else{
           outResults(coeffs, lengthc, roots, lengthr);
      }
}
bool calcRoots(double coeffs[], int lengthc, double root[], int lengthr ){
     return false;
}
Last edited on
19
20
                                calcRoots(coeffs, lengthc, roots, lengthr);
                                cout << "Testing whether the roots exist or not: " << calcRoots << endl;

This is not how you access the return value of a function from the calling code. You need to store the return value from calcRoots() in a variable. Even the most rudimentary of textbooks will explain this, so I strongly recommend you re-read the section on functions, as it's important to understand how to call functions properly.

When you use the name of a function on its own, without parentheses, then it's treated as a pointer to that function, which means it will have a non-zero value.

Last edited on
where is your function? let us see the code.
Please don't delete your code once your question has been answered. It's extremely selfish, because it means you're not contributing to this site as a resource from which others can learn.

Reported.
Sorry I was not trying to be selfish, I just didn't want my professor to Google my code and find it on here and then claim I was plagiarizing. I put the code back up, oh and for future reference you could try to be a little less rude to people. Thanks for the help.
Thanks! :D I really didn't mean to be a douche, I just didn't know
Sorry, but I don't believe I was being rude. What you did is something that happens a lot here, and it's an abuse of this forum, and of the time and effort people put in to help others.

I would note that I was the one who explained what your problem was and how to fix it. In your haste to delete your code, you couldn't even find time to acknowledge that, or say "Thanks", so I think you should, perhaps, re-examine exactly who was being rude in this thread.

EDIT: And thanks for restoring your code.
Last edited on
Topic archived. No new replies allowed.