开发者

The multiples of 2 Condition is not working(Counter Problem)

开发者 https://www.devze.com 2023-04-06 23:30 出处:网络
Technically this code of mine is a kettle simulation, but the problem now is that. when it reach that 20.00 , the code for it is not working

Technically this code of mine is a kettle simulation, but the problem now is that. when it reach that 20.00 , the code for it is not working

#include<stdio.h>
#include<stdlib.h>
#include<cstdlib>
#include<time.h>


//Global Declaration  for waterTemperature
double nWaterTemp;

//Function for seconds :)
void wait ( int seconds ){
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {
    }
}

//Function for increasing of hit of Kettle
void behaviourOn(){


    int counter = 1;

    while(nWaterTemp <=100.0){

        wait(1);
        printf("Ticking! %d\n",counter);
        if( counter%2 == 0&& nWaterTemp >=20.0 ){
                      nWaterTemp+=1.5/2.0;  
            printf("The water temp is %.2f\n",nWaterTemp); 

        }else if(counter%3==0){
            nWaterTemp+=2.0/3.0;
            printf("The water temp is %.2f\n",nWaterTemp);
        }else if(nWaterTemp == 20.0){
            system("cls");
            printf("THE KETTLE IS NOW ON!\n");
        }
            counter++;
    }
}

//Function for Envinronment
void environment(){
    int counter2 = 0;

    system("cls");
    printf("THE WATER IS NOW COOLING\n");
    while(nWaterTemp>=0){
        counter2++;
        wait(1);
        printf("Ticking! %d\n",counter2);

        if(counter2%3==0){
            nWaterTemp -=2.0/3.0;
            printf("The water temp is %.2f\n",nWaterTemp);
        }

    }
}

//main
int main(void){
    behaviourOn();
    environment();
    system("pause");    
}

you see, if the water temp is not higher than 20.00, it will only increase every 2 seconds but in my code there are times that each 1 second its value is changing, and also there are times that every 2 seconds it is changing ... what are the errors in this code?

if( counter%2 == 0&& nWaterTemp >=20.0 ){//THe equation changes if it reach 20.00, and the heat increases every 2 seconds
                      nWaterTemp+=1.5/2.0;  
                      printf("The water temp is %.2f\n",nWaterTemp);

That's the part where it is confusing me, as you see in the condition if should increase the valu开发者_JS百科e the temperature every 2 seconds but the thing is it's changing every 1 second and then sometimes changed every 1 second help please


You should write like this:

 if( nWaterTemp >=20.0 ) {
     if (counter%2 == 0) {
         nWaterTemp+=1.5/2.0;  
         printf("The water temp is %.2f\n",nWaterTemp); 
     }
 } else if ...

otherwise when counter % 2 != 0, it will do something wrong

0

精彩评论

暂无评论...
验证码 换一张
取 消