Reputation: 27
I need to use a for loop to end the program. However, I am unsure where I should be placing my for loop. The for loop should end the program after three increments, per assignment it must be a for loop.
Originally, I was thinking the for should be at the beginning of the program. However this would have the user run the program three times then end.
Then, I was going to put the for loop in the data validation while loop, before the first if statement. However, this requires the user to put the correct number in three times before ending the loop.
I have tried adding some if statements that will check when increment i is three, and if it is, it will break; to the next statement. This, however just causes the "Congratulations!" message to pop up after each input.
Not really sure where else to place this loop. Posted is my original number guessing game.
int main()
{
char userInput = 'Y';
int number, guess, i;
srand(time(NULL));
number = rand() % 20 + 1;
printf("I am thinking of a number between 1 and 20.\n");
printf("Can you guess what it is?\n");
scanf("%d", &guess);
for (i = 0; i = 3 || guess == number; i++)
{
printf("%d\n", i);
if (guess > number)
printf("Your guess is too high.\n\n");
else if (guess < number)
printf("Your guess is too low.\n\n");
printf("Please enter another guess. ");
scanf("%d", &guess);
}
printf("Congratulations! You have guessed correctly.\n");
printf("The number was %d\n", number);
return 0;
}
When creating the for loop, I would use for (i = 0; i != 3; ++i). I expect the user to be able to guess for the correct number three times, and if they are unable to guess the number, the program to end with "Sorry, the number was not guessed". Please feel free to suggest readability improvements, my professor has not taught us proper white space usage.
Edit: Restructured as I realized the while loop is probably not necessary, if I could increment the loop 3 times and end the program, or end the program when the number is achieved.
Upvotes: 1
Views: 799
Reputation: 2318
You probably only need one for loop to check for the number of attempts. When you get the correct answer, you can use break
to exit the loop without executing the next lines in the loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, i;
srand(time(NULL));
number = rand() % 20 + 1;
printf("I am thinking of a number between 1 and 20.\n");
printf("Can you guess what it is?\n");
scanf("%d", &guess);
for (i = 0; i < 3; i++) {
if (guess > number)
printf("Your guess is too high.\n\n");
else if (guess < number) {
printf("Your guess is too low.\n\n");
} else {
printf("\nCongratulations! You have guessed correctly.\n");
break;
}
if (i < 2) {
printf("Please enter another guess.\n");
scanf("%d", &guess);
} else {
printf("Sorry, the number was not guessed\n");
}
}
printf("The number was %d\n", number);
return 0;
}
Upvotes: 0
Reputation: 10849
The while loop is redundant
You can do this in a single loop, and you can also move the call to scanf
at the beginning of the loop so that you don't have to repeat it manually beforehand.
Working example here on repl.it
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ATTEMPTS 3
int main() {
int number, guess, i;
srand(time(NULL));
number = rand() % 20 + 1;
printf("I am thinking of a number between 1 and 20.\n");
printf("Can you guess what it is?\n");
for (i = 0; i < MAX_ATTEMPTS; i++) {
scanf("%d", &guess);
if (guess > number)
printf("Your guess is too high.\n\n");
else if (guess < number) {
printf("Your guess is too low.\n\n");
} else {
break;
}
if (i < MAX_ATTEMPTS - 1) {
printf("Please enter another guess. ");
}
}
if (guess == number) {
printf("\nCongratulations! You have guessed correctly.\n");
printf("The number was %d\n", number);
} else {
printf("\nSorry, the number was not guessed.\n");
}
return 0;
}
Upvotes: 2
Reputation: 1582
To use the for loop to end the program, I'd think you'd want to put the terminating conditions in the for, like this pseudocode:
number = random();
guess = -1 /* something invalid that can never be returned by random() */
for (i=0; i<3 && number != guess; ++i) {
print("please guess")
scanf(guess)
}
if (guess == number)
win()
Remember that
for (start; test; increment) {
statement;
}
is exactly equivalent to:
start;
while (test) {
statement;
increment;
}
Upvotes: 0