James Faulkner
James Faulkner

Reputation: 91

How to assign a new value outside the while loop

I have method in c++ called getFuelLevel.

   double Sensors::getFuelLevel()
    {
        double fuelLevel= 6000.0;
        double nf = fuelLevel;
        // while loop execution
        while (fuelLevel = 0 ) {
            fuelLevel-1;
            // seting new value for fule level
            fuelLevel = nf;
        }
        return nf;
    }

I have tried using a while loop to reduce the value by 1 every time. But it is not working. Instead of outputting the value to console, I need it to return the new value outside the while loop. I want the code to reduce the value of fuellevel by 1 every second and return the new value.

Please help, I am not sure how to do this.

Upvotes: 0

Views: 105

Answers (1)

BusyProgrammer
BusyProgrammer

Reputation: 2781

I want the code to reduce the value of fuellevel by 1 every second and return the new value.

If you want to decrement the value every second, then you could try using the Win32 API function SetTimer. See more on:

MSDN Documentation

As an aside, your while-loop condition is faulty:

while (fuelLevel = 0 ) {

You should be using the comparison operator ==, not the assignment operator =:

while (fuelLevel == 0 ) {

You may also want to look over the logic of the code inside your while loop yourself. Simply doing fuellevel - 1 does not decrement fuellevel every second.

Upvotes: 1

Related Questions