954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic C Problem

Guys help me please, the equation on 2/3 is giving the waterTemp to 0; how come it is not changing? and also, if the nWaterTemp reached 20.00or somewhere 20.00 to 21.00; it is supposed to print that "THe kettle is now on "Help please

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


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

void behaviourOn(){

	double nWaterTemp = 0;
	int counter = 0;
 
	while(nWaterTemp <=100.0){
		counter++;
		printf("Ticking! %d\n",counter);
		wait(1);
		if(nWaterTemp >=20.0 && counter%2==0){
			nWaterTemp+=0.75;  
            	printf("The water temp is %f\n",nWaterTemp);  
		}else if(counter%3==0){
			nWaterTemp+=2/3;
				printf("The water temp is %f\n",nWaterTemp);
		}else if(nWaterTemp >=20 &&  nWaterTemp< 21){
              printf("On");
              }
	}
}


int main(void){
	behaviourOn();
	system("pause");	
}
chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

2/3 equals zero because they're integers. Try using 2.0/3.0

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 
2/3 equals zero because they're integers. Try using 2.0/3.0


okay done, how about the printing of "ON" it should be only printed once?

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
else if(nWaterTemp >=20 &&  nWaterTemp< 21 && haveNotSaidOnYet){
              printf("On");
              haveNotSaidOnYet = 0;
              }
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: