Task

Write a program where the program chooses a number between   1   and   10.

A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct.

When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits.

A   conditional loop   may be used to repeat the guessing until the user is correct.



Related tasks

   Bulls and cows
   Bulls and cows/Player
   Guess the number/With Feedback
   Mastermind


 
#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
 
        NSLog(@"I'm thinking of a number between 1 - 10. Can you guess what it is?\n");
 
        int rndNumber = arc4random_uniform(10) + 1;
 
        // Debug (Show rndNumber in console)
        //NSLog(@"Random number is %i", rndNumber);
 
        int userInput;
 
        do {
 
            NSLog(@"Input the number below\n");
            scanf("%i", &userInput);
 
            if (userInput > 10) {
 
                NSLog(@"Please enter a number less than 10\n");
            }
 
            if (userInput > 10 || userInput != rndNumber) {
 
                NSLog(@"Your guess %i is incorrect, please try again", userInput);
 
            } else {
 
                NSLog(@"Your guess %i is correct!", userInput);
            }
 
        } while (userInput > 10 || userInput != rndNumber);
    }
    return 0;
}
 