1

So I've started working with c++ for my university classes and it's been going really well so far. There's a dilemma I'm having with a current question and I've got the basic code structure all figured out, there's just one problem with my output.

What I'm looking for e.g;

if (bool variable = true){ 

  output

else
  alternate output

I'm aware that this isn't a free debugging service place, but it would really help me in future projects as well and there aren't any bugs, it executes just fine.

My code:

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
bool calculateBox(double, double, double, double *, double *);

int main()
{
    //defining variables
    double length, width, height, volume, surfaceArea;

    cout << "Hello and welcome to the program.\nPlease enter the dimensions for the box in [cm](l w h): ";
    cin >> length >> width >> height;
    calculateBox(length, width, height, &volume, &surfaceArea);

    if (bool calculateBool = true) {
        cout << "Volume: " << volume << "cm^3" << endl << "Surface Area: " << surfaceArea << "cm^2" << endl;
    }
    else
        cout << "Error, value(s) must be greater than zero!" << endl;

    system("pause");
    return 0;
}

//functions
bool calculateBox(double length, double width, double height, double * volume, double * surfaceArea) {
    if ((length > 0) && (width > 0) && (height > 0)) {
        *surfaceArea = length * width * 6;
        *volume = length * width * height;
        return true;
    }
    else
        return false;
}

*Key, if the values do not meet the requirements, the output displays not the error message, but a strange string for surfaceArea and volume. It appears to skip over the 'else' statement.

My question - does my error lie within the return statements in the function? Or is it a logic problem with my 'if' statement in the main method?

Stuart B
  • 13
  • 5
  • Your `if` condition always evaluates to true. What did you mean for that to do? – aschepler Nov 29 '17 at 23:34
  • `if (bool calculateBool = true)` creates a new variable, `calculateBool`, and initializes it to true. Not what you want. I suspect what you want is more like `if (calculateBox(length, width, height, &volume, &surfaceArea)){ `, but this is just a suspicion. – user4581301 Nov 29 '17 at 23:35
  • unrelated: in `bool calculateBox(double length, double width, double height, double * volume, double * surfaceArea)` consider using references instead of pointers: `bool calculateBox(double length, double width, double height, double & volume, double & surfaceArea)` References are generally easier to work with and harder to screw up than pointers. – user4581301 Nov 29 '17 at 23:37
  • @user4581301 My man, you are a genius. I had originally stated if ((calculateBox(length, width, height, &volume, &surfaceArea)) = true){ But now I understand that the return value itself is actually true. This actually solved all my problems, thanks so much! – Stuart B Nov 29 '17 at 23:38
  • As for the second point, I agree that would be easier to work with but the current project outlines "use of pointers for c++ solutions". Thanks for the heads up though! – Stuart B Nov 29 '17 at 23:40
  • `if ((calculateBox(length, width, height, &volume, &surfaceArea)) = true)` went wrong on you because `=` is used to assign, not test for equality. Use `==` to test. What you wrote tried to assign true to what the function returned. The compiler spits out an error message because that's not really meaningful. But read [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) to learn when it is useful. – user4581301 Nov 29 '17 at 23:46
  • Check with the instructor on what the goal of the lesson is, but that's not a good use for a pointer in C++. It is in C though. – user4581301 Nov 29 '17 at 23:48
  • Ah I see, variable assignment vs. equality checking is something I missed when proofreading my code. I'll take a look at that article for sure! I'll send him an email for clarification then - I just saw the article [reference vs pointer](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in?rq=1). Thanks again man. – Stuart B Nov 29 '17 at 23:57

1 Answers1

1

In the statement

if (bool calculateBool = true) 

the bool calculateBol part will cause a local variable named calculateBool be defined as a bool. The = true part means to assign what's on the left of =to the value true. The whole bool calculateBool = true will therefore be true so that that the else clause will never be executed.

Note that the the occurence of a single = in a condition should always ring the bell that bad signs could happen. Because comparing for equality is ==.

This being said, you could write:

 if (calculateBox(length, width, height, &volume, &surfaceArea)) {

or if you need the value later on:

bool calculateBool = calculateBox(length, width, height, &volume, &surfaceArea);
if (calculateBool) {  // or calculateBool==true if you prefer
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • This is a much more in-depth solution to my problem. And it's really clear as to what I did wrong, I'll know to look out for that next time. Creating the local variable vs calling the function seems to have been my main problem. Really appreciate it, Christophe! – Stuart B Nov 30 '17 at 00:00