Originally Posted by
Interista
Not being funny, but as I said above, we're all having huge problems with our course because its being taught badly. We're trying to get something done about it now. They told us how to use while, how to enter it, but not exit it. Hence you may as well ask me what's the Russian for elephant as I've as much chance of knowing.
I tried switching the whiles around but it doesn't work. That's logic, but doesn't do anything.
Exiting while is pretty simple... program flow (called "control") resumes at the closing brace when the condition is no longer met.
For example...
Code:
#include <stdio.h>
int main (void)
{
int x = 0;
while ( x < 5 )
{
printf("%d ",x);
x++;
}
printf("Done!\n\n");
return 0;
}
Type that up and run it... you'll see how it works.
As for the course... we hear this complaint a lot, especially from India where they are turning out 1980s grade computer scientists, 20 years too late... It's good that you're trying to get something done about the poor teaching. But in the mean time you should grab a modern textbook or tutorial and study on your own.... anything is better than nothing, my friend.
Now for your long running problem with this hi-lo game...
Code:
// high-low guessing game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void)
{
int secret, guess, turn;
srand(time(NULL));
secret = rand() %1000 + 1;
printf("I have a number between 1 and 1000 (inclusive)\nYou have 10 tries to guess it...\n\n");
turn = 1;
while (turn < 11)
{
printf("Turn %d : ",turn);
scanf(" %d", &guess);
if (guess < 1 || guess > 1000)
{
printf("Between 1 and 1000 please\n");
}
else if (guess < secret)
{
printf("Too low\n");
turn ++;
}
else if (guess > secret)
{
printf("Too High\n");
turn++;
}
else
{
printf("\n\nWe have a winner! The number was %d \n\n", secret);
exit(0);
}
}
printf("\n\nSorry, not this time! The number was %d \n\n", secret);
return 0;
}
You should not copy this code...
it's now pubilished and your teacher may find it...
but you should study it and see how it works.
Good luck with your studies...