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?

8
  • Your if condition always evaluates to true. What did you mean for that to do? Commented Nov 29, 2017 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. Commented Nov 29, 2017 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. Commented Nov 29, 2017 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! Commented Nov 29, 2017 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! Commented Nov 29, 2017 at 23:40

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.