#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Error using if else statement


    I'm getting the run-around trying to use an if-else-statement. Picking out the relevant section and simplifying for readability:
    Code:
    void cylinderSetup(IncomprFlowParam<T> const& parameters, T cxl_divisor)
    {
        const T nx = parameters.getNx();
        const T ny = parameters.getNy();
        T cxl; 
        
        if (cxl_divisor == 0.) {
    	T cxl = 0.;
    	}
         else {
    		T cxl = nx/cxl_divisor;
    	}
    T here is just a typedef shorthand for double, and IncomprFlowParam is a special type used in this program.

    So what I want to happen here is that the program checks whether cxl_divisor is 0. If it is, it should return the variable cxl as 0, if not it should return the parameter value cx divided with whatever cxl_divisor happens to be. However, this does not happen. cxl turns out as 0, regardless of input. I've tried scaffolding with printouts at every step, and just to compound my confusion it actually does enter the correct branches given the state of cxl_divisor. End result, however, doesn't care and keeps cxl as 0. Is there perhaps someone here who has any thoughts on what I'm doing wrong?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    You say "return" a lot, but the function is void.

    > if (cxl_divisor == 0.)
    Are you sure this isn't just a single = in the original code?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by salem
    You say "return" a lot, but the function is void.

    > if (cxl_divisor == 0.)
    Are you sure this isn't just a single = in the original code?
    Ah, good point; I'm saying "return" in the generic sense and not in the c++ sense. What happens right after the if-else-statement is that cxl is used in creating an object of a class representing a cylinder, so there's no need for this function to return a value. And yes, I do know that it's been compiled with "==", not "=". Still, the if-else-statement does not modify cxl correctly.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    You're going to have to come up with a compilable test case we can try for ourselves.

    Code:
    $ cat foo.cpp
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdarg>
    using namespace std;
    
    template<typename T>
    class IncomprFlowParam {
    private:
      T x;
      T y;
    public:
      T getNx() const { return x; }
      T getNy() const { return y; }
      IncomprFlowParam() : x(0), y(0) { }
      IncomprFlowParam(T x, T y): x(x), y(y) { }
    };
    
    template<typename T>
    void cylinderSetup(IncomprFlowParam<T> const& parameters, T cxl_divisor)
    {
      const T nx = parameters.getNx();
      const T ny = parameters.getNy();
      T cxl;
      cout << "Got x = " << nx << ", param=" << cxl_divisor << endl;
      if (cxl_divisor == 0.) {
        cout << "It's zero!" << endl;
        T cxl = 0.;
      }
      else {
        cout << "We're golden" << endl;
        T cxl = nx/cxl_divisor;
      }
    }
    
    int main ( ) {
      IncomprFlowParam<double> var(12.0,34.0);
      cylinderSetup<double>(var,22.0);
    }
    $ g++ foo.cpp
    $ ./a.out 
    Got x = 12, param=22
    We're golden
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo