简体   繁体   中英

C++ Fuzzy Logic loop increments after first input

I wrote my first Fuzzy Logic program and the first input does its job perfectly by outputting the correct voltage value. The word Voltage is strictly a place holder. It matches my math exactly. Any value afterwards, increases the final output and NEVER works again until I reset the program, upon which, it does the same thing.

I'll try to simplify my code as much as possible.

int main()
{
int InputDistance;
double BetweenDistance1;
double BetweenDistance2;
double DoM1;
double DoM2;
double OutputComponent1;
double OutputComponent2;
double CrispOutput;
bool NN = false;
bool N = false;
bool Z = false;
const double NNVolt (0);
const double NVolt (2.25);
const double ZVolt (4.5);
for(;;)
{
        cout <<  "What is the distance?" << endl << endl;
        cin >> InputDistance;
        cout << endl;
        if(InputDistance > 0 && InputDistance <= 5)
        {
            BetweenDistance1 = InputDistance - 0;
            BetweenDistance2 = 5 - InputDistance;
            NN = true;
            N = true;
        }
        if(InputDistance > 5 && InputDistance <= 10)
        {
            BetweenDistance1 = InputDistance - 5;
            BetweenDistance2 = 10 - InputDistance;
            NN = true;
            N = true;
        }
        if(InputDistance > 10 && InputDistance <= 15)
        {
            BetweenDistance1 = InputDistance - 10;
            BetweenDistance2 = 15 - InputDistance;
            N = true;
            Z = true;
        }
        if(InputDistance > 15 && InputDistance <= 20)
        {
            BetweenDistance1 = InputDistance - 15;
            BetweenDistance2 = 20 - InputDistance;
            N = true;
            Z = true;
        }

        DoM1 = BetweenDistance1 / 5;
        DoM2 = BetweenDistance2 / 5;

        if(NN == true && N == true)
        {
            OutputComponent1 = NNVolt * DoM1;
            OutputComponent2 = NVolt * DoM2;
        }
        if(N == true && Z == true)
        {
            OutputComponent1 = NVolt * DoM1;
            OutputComponent2 = ZVolt * DoM2;
        }
        CrispOutput = OutputComponent1 + OutputComponent2;

        cout << "Voltage = " << CrispOutput << endl << endl;
    }
    return (0);
}

What is causing the values to increment every time I input a new distance value? I can't see it.

You are never resetting your bool s.

bool NN = false;
bool N = false;
bool Z = false;

Should be at that start of your for loop so that they are reset every time it loops.

As a rule of thumb any variable that you will use in a loop should be declared in the loop unless you need to access it outside the scope of the loop.

You should initialize all your variables, because even though your compiler will initialize them to null, "", preppended "\\0", 0, 0.0, 0.0f it's can be taken to be compiled under a different compiler that doesn't follow such a standard, or compiling with flags that initialize using different stuff.

int main()
{
    int InputDistance = 0;
    double BetweenDistance1 = 0.0;
    double BetweenDistance2 = 0.0;
    double DoM1 = 0.0;
    double DoM2 = 0.0;
    double OutputComponent1 = 0.0;
    double OutputComponent2 = 0.0;
    double CrispOutput = 0.0;
    bool NN = false;
    bool N = false;
    bool Z = false;
    const double NNVolt (0);
    const double NVolt (2.25);
    const double ZVolt (4.5);

And you should re-initialize at the beginning of the loop to make sure you don't end up using data from previous iterations.

    for(;;)
    {
            //----------- init ------------
            InputDistance = 0;
            BetweenDistance1 = 0.0;
            BetweenDistance2 = 0.0;
            DoM1 = 0.0;
            DoM2 = 0.0;
            OutputComponent1 = 0.0;
            OutputComponent2 = 0.0;
            CrispOutput = 0.0;
            NN = false;
            N = false;
            Z = false;
            //-----------------------------

            cout <<  "What is the distance?" << endl << endl;
            cin >> InputDistance;
            cout << endl;

            ..            

        }

        return (0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM