Friday, May 21, 2010

Simple while loop programming help in C. guess a number program.?

This is my program. I have to get the person to guess a temperature between 70 and 75, inclusive. If the person guesses wrong, I have to say either too cold or too hot and have them guess again. The problem with this is it doesn't run there guess through the loops again, it just ends. Thanks.











#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;


#include %26lt;conio.h%26gt;


#define PI 3.14159265358979323846


int main(void)


{


float temp;





printf("Enter the temperature in degrees Farenheit");


scanf("%f", %26amp;temp);








while (temp%26gt;75)


{


printf("too Hot, guess again:");


scanf("%f", %26amp;temp);


}





while (temp%26lt;70)


{


printf("too Cold, guess again:");


scanf("%f", %26amp;temp);


}





while(75%26gt;=temp %26amp;%26amp; temp%26gt;=70)


{


printf("Just Right");


}


return 0;


}

Simple while loop programming help in C. guess a number program.?
You want to do this:





while( temp %26gt; 75 || temp%26lt;70)


{


if(temp %26gt; 75)


{


printf("Too Hot, Guess again:");


scanf("%f", %26amp;temp);


}


if(temp %26lt; 70)


{


printf("Too Cold, guess again:");


scanf("%f", %26amp;temp);


}


}


printf("Just right!");
Reply:All the code above does is say "too hot" if they guess over 75 and "too cold" if they guess lower than 70. And it doesn't go back to checking too hot if they guess too cold then too hot. Also, it doesn't check whether a guess between 70-75 matches any random number between 70-75.





Also, you could be more efficient in your code by having one loop. Each time in the loop, you would check their response and have a branched if statement or case switch.
Reply:Instead of 3 small while loops, you actually want on big while loop.





A way you can better understand your code is to step through it. Let's say the user enters 5 as his first guess.


The program will come to the first while loop. It will compare 5 to 75. Is 5%26gt;75? No. It will skip over the first loop.


The program will then come to the second loop and compare 5 to 70. Is 5%26lt; 70? Yes. It will enter the second loop.


The user then enters a new value. As long as the user keeps entering values less than 70, this loop will continue saying "too Cold, guess again:" and letting the user try again.





Finally you have the third loop. To reach this point, temp has to be greater than or equal to 70 (from the second loop). It will be compared to both 75 and 70. If it is less than or equal to 75 and greater than or equal to 70, it will enter the third loop and print "Just Right".


Then it will return to the top of the third loop and compare temp to 75 and 70 again. temp hasn't changed, so it will go through the loop again.


This is actually an infinite loop. If temp is between 70 and 75, your program will continue to print "Just Right" until you terminate it.





What you want is a single while loop that compares temp to 70 and 75 and only repeats if is outside of the acceptable range.





If it's within the acceptable range, you want to exit the loop, print "Just Right" once and then return 0;





I'll give you an outline of the code but leave some blanks for you to figure out. (I'll start at the first while loop since everything before that is fine)





...


while( temp ?? 70 || temp ?? 75) //notice that it's now || (or) instead of %26amp;%26amp;


{


if(??) //under what condition is it "too hot"


{


printf("too hot, guess again");


}


else if(??) //under what condition is it "too cold"


{


printf("too cold, guess again");


}


scanf("%f",%26amp;temp); //this is outside of the if switches to save space.





}//repeat the loop if appropriate





printf("just right");


return 0;


...





Hope this helps. Good luck.
Reply:I would do a while loop, and then a bunch of if statements.





Something like this:





while (ans == 0)


{





if (temp%26gt;75)


{


printf("too Hot, guess again:");


scanf("%f", %26amp;temp);


}





if (temp %26lt; 70)


{


printf("too Cold, guess again:");


scanf("%f", %26amp;temp);


}





if(75%26gt;=temp %26amp;%26amp; temp%26gt;=70)


{


printf("Just Right");


ans = 1;


}


return 0;


}





What this does is run a loop that runs all of these if statements. If they get the right answer it sets ans = 1 which gets out of the while loop.





The syntax may not be correct as I have not programmed in C for a year or two.





For more tech tips check out: http://www.thetechjuice.com
Reply:You exit out of the loops when the condition isn't met.





The program doesn't jump back to a loop you already exited.





Make a while loop with if-else statements.





while(temp %26gt; 75 || temp%26lt;70)


{





if(temp %26gt; 75){


printf("too Hot, guess again:");


scanf("%f", %26amp;temp);


}


else{


printf("too Cold, guess again:");


scanf("%f", %26amp;temp);


}


}


printf("Just Right");


No comments:

Post a Comment