-3

Newish to C++ but been researching alot so please bear with me.

I've been trying to use 1 function to relate 8 global double variables [ mA mB ... mG which ranges from values 1 to 10] to another double value. I first obtain these 8 variables by obtaining the data from a csv file, throwing them to an array and then equating the set global variables. This all works fine. I cout the values correctly.

mA =10, mB=1 ,.... mG=2

However I need to use these variables to relate to another set value. So i Use:

double Function1(double Mvalue1)
{
    if (Mvalue1 == 1) { double value = 10; return value ; } 
    if (Mvalue1 == 2) { double value = 20; return value ; } 
    .... // cont to 10 only increasing value by 10
    if (Mvalue1 == 10) { double value = 110; return value ; }
}

void VarFunction()
{   
    mA2= Function1(mA); **//SHOULD output 110**
    cout << "Vaule A " << mA2 << endl;

    mB2= Function1(mB); **//SHOULD output 10**
    cout << "Vaule B " << mB2 << endl;
     ....//.... 8 times
    mG2 = Function1(mG); **//SHOULD output 20**
    cout << "Vaule G " << mG2 << endl;
}
int main()
{
   VarFunction()
   return 0;
}

So the output i get here is

Value A 110
Value B -1.#IND
.... 
Value G -1.#IND

Why isnt the next call of function1 with the next variable not working?

MechCub
  • 3
  • 3
  • 1
    Perhaps this might give you some clues? http://stackoverflow.com/questions/7476177/why-the-return-value-of-double-is-1-ind – MysticXG Aug 05 '14 at 20:53

5 Answers5

1

In your code you have mA set to 12, but Function1 doesn't have a case for 12. So, I'm surprised you're even getting 110 printed for the first line. You aren't handling the case inside Function1 where Mvalue1 isn't one of the desired values, so this is the first thing to fix.

Also, assigning a number to a double and then returning it is unnecessarily complicated. A case statement would work well, assuming you really want to pass integers:

double Function1(int Mvalue1)
{
    switch(Mvalue1) {
        case 1: return 10;
        case 2: return 20;
        //...
        case 10: return 110; // are you sure you don't want 100?
        default: return -1; // handle the case here so that you always return a value.
    }
}

Of course, if you really just want 10 times your input, why not:

double Function1(double mValue1)
{
    return mValue1*10;
}
Nathan S.
  • 5,244
  • 3
  • 45
  • 55
  • Hey thanks for the help eh... I tired to do the switch. I'm still getting the good output for the first variable mA BUT -1.#IND for the rest. I get the *10 thing but its ment to relate a set number to another.... IE Product mA relates to the ID 10 in my cvs file which i need to relate to the price 9.23, then Product mB relates to the ID 1 which is 5.80. etc. – MechCub Aug 05 '14 at 20:42
  • I suggest testing the code directly: `cout << "Vaule A " << Function1(20) << endl;` If this doesn't work, you need to copy and paste your *exact* code. – Nathan S. Aug 05 '14 at 20:45
  • Ya my prob is that its course related in my grad schooling, I cant post the exact code. I'm no programing so i really thankful for ya. – MechCub Aug 05 '14 at 20:50
0

Not all paths in your function return a defined value, i.e. there's no return statement after all the conditionals. The compiler is probably telling you that. If not - compile with higher warning level.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
0

Use the std::map container when building relationships like this.

#include <iostream>
#include <map>

typedef std::map<double, double> rel_t;

int main()
{
    rel_t mymap;
    // You can directly
    // std::map<double, double> mymap;


    mymap[1] = 10;
    mymap[2] = 20;
    mymap[10] = 110;

    std::cout << mymap[1] << std::endl;     // Prints 10
    std::cout << mymap[2] << std::endl;     // Prints 20
    std::cout << mymap[10] << std::endl;    // Prints 110

}
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
0

This program seems to be working for me when I run it. However, I had to add declarations for mA2, mB2, and mG2 in your VarFunction(). You're also missing a semicolon after your call to VarFunction() in main().

I'd also suggest you return some default double in the function double Function(double Mvalue1), just in case Mvalue1 does not satisfy any of the if statements.

Alice
  • 17
  • 3
0

As already said, Function1() should return a value in case all if statements are false!

If the numbers your are dealing with have no fractional digits, use short, int, long or any other integer type for the variables. Then you can use a switch()/case construct or keep on using the comparison operator ==.

In case you must deal with floating point values, never use the == operator! When reading floating point values from text files (like CSV) or a database, a conversion from text to float/double is done. The result from such conversion can end in e.g. 9.999999999 or 10.000000001 instead of 10. And then the comparison with == is false!

To compare two double variables use a method like this:

bool dEqual( double dVal1, double dVal2, double dTolerance)
{
    if( fabs( dVar1 - dVar2) < dTolerance) {
        // dVar1 is nearly equal to dVar2
        return true;
    }

    // dVar1 is not equal to dVar2
    return false;
}

Then this comparison is true:

if( dEqual( 10.0, 9.999999998, 0.000001))

Apply a value for tolerance that meets the accuracy you need.