#include <stdio.h>
#include <stdlib.h>
main()
{
int number;
int guess;
int tries;
number = rand() % 101 + 1;
printf("Let's play a game!\n");
printf("I will think of a number 1-100. Try to guess it.\n");
printf("Guess: ");
scanf("%d",&guess);
for (tries = 0;tries<100;tries++);
{
if (guess == number)
{
printf("You guessed it!\n");
scanf("%d",&guess);
printf("And it only took you: ");
scanf("%d",&tries);
}
elseif (guess < number)
{
printf("Higher\n");
printf("Guess again: ");
scanf("%d",&guess);
}
elseif (guess > number)
{
printf("Lower\n");
printf("Guess again: ");
scanf("%d",&guess);
}
else
printf("That's not even in range!");
return 0;
#include <stdio.h>
#include <stdlib.h>
main()
{
int number;
int guess;
int tries;
number = rand() % 101 + 1;
printf("Let's play a game!\n");
printf("I will think of a number 1-100. Try to guess it.\n");
printf("Guess: ");
scanf("%d",&guess);
for (tries = 0;tries<100;tries++);
{
if (guess == number)
{
printf("You guessed it!\n");
scanf("%d",&guess);
printf("And it only took you: ");
scanf("%d",&tries);
}
elseif (guess < number)
{
printf("Higher\n");
printf("Guess again: ");
scanf("%d",&guess);
}
elseif (guess > number)
{
printf("Lower\n");
printf("Guess again: ");
scanf("%d",&guess);
}
else
printf("That's not even in range!");
}
return 0;
}
You probably want to put a break; so that you exit the loop if the guess is correct.
I also highly, highly recommend you initialize your variables. Just make it a habit to give every variable an initial value.
hello Jayhawk, Thnak you for your inputs, however, it seems that the for loop isnt working. It only allows me to have 2 tries then loop is exiting (w/o getting guess==number). Thanks in advance.
yeah, got that. Thank you. However, im having to trouble as the looping seems to be not working.
Any possible mistakes? Sorry Im still studying loops.
Im also trying to work out as to how to count the number of attempts by having this: for (tries = 0;tries<100;tries++)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number;
int guess;
int tries;
srand (time(NULL));
number = rand() % 100 + 1;
printf("Let's play a game!\n");
printf("I will think of a number 1-100. Try to guess it.\n");
for (tries = 0;tries<=5;tries++);
printf("Guess: ");
scanf("%d",&guess);
{
if (guess == number)
{
printf("You guessed it!\n");
printf("It only took you: %d tries");
scanf("%d",&tries);
}
elseif (guess < number)
{
printf("Higher\n");
printf("Guess again: ");
scanf("%d",&guess);
}
elseif (guess > number)
{
printf("Lower\n");
printf("Guess again: ");
scanf("%d",&guess);
}
}
{
if (tries=5);
printf("You have now reached the maximum tries. Thank you for playing!");
}
}
It only allows me to have two attempts then program breaks already. Also, everytime I put break; it always gives me an error that it should be within a loop.
Your help will be very much appreciated. Thank you.
Yes, break; must be within a loop. I suggested you use it in order to exit your loop early.
You only put part of your code, but this chunk should be looked at:
1 2 3
{if (tries=5);
printf("You have now reached the maximum tries. Thank you for playing!");
}
This is legal, but I don't think it's the logic you were going for. Should you be using = or ==?
You would also need to manually exit the loop yourself inside that chunk, since 5 is too low for the loop to automatically exit.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number = 0; // <--- ALWAYS INITIALIZE VARIABLES
int guess = 0; //<---
srand (time(NULL));
number = rand() % 100 + 1;
printf("Let's play a game!\n");
printf("I will think of a number 1-100. Try to guess it.\n");
for (int tries = 0; tries < 5; tries++) //<---
{
printf("Guess: "); // <-- ONCE ONLY
scanf("%d", &guess);// <-- ONCE ONLY
if (guess == number)
{
printf("You guessed it!\n");
printf("It only took you: %d tries", tries); // <---
break; // <--
}
elseif (guess < number)
{
printf("Higher\n");
}
else
{
printf("Lower\n");
}
}
printf("You have now reached the maximum tries. Thank you for playing!");
return 0;
}