I was able to set a variable and increase it each time to show the number of tries. Yay for me. Anyone know best way of stopping the loop when the user has guessed the number, thus ending the loop before it has reached 10? Any help would be greatly appreciated.
Code:
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
int main(void)
{
int x = 10;
int i = 0;
int target, guess;
int numGuess = 0;
/*create a random number*/
//create random function
srand(time(NULL));//this creates new number based on time which changes every second :)
target = rand() % 99; //create a random number using the rand() function, from 0 -99
do{
//increase the loop until it meets the x variable
i++;
numGuess++;
//allow user to input a number for guess
scanf("%d", &guess);
if (guess == target)
{
printf("You win! \n\n");
}
else if (guess > target)
{
printf("You are too high. Guess a number:\n\n");
}
else if (guess < target)
{
printf("You are too low. Guess a number:\n\n");
}
}while(i < x);
printf("You lose, the number was %d. \n", target);
printf("Number of tries %d\n", numGuess);
printf("Enter any key to exit...");
getchar();
getchar();
return 0;
}