Verify whether an input is an number and whether a number has been repeated or not?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















i know there have been a few questions on this game, but mine has a few more instructions so it makes it a bit more difficult. I am almost done, I just have a couple more things to complete. my code is as follows:



- (NSString *)output {

NSMutableString *resultOutput = [[NSMutableString alloc] init];
int secretNumber= arc4random_uniform(10);
int numberChosen;
int attempt=0;

NSMutableArray<NSNumber*> *myArray = [[NSMutableArray alloc] init];

printf("Guess a Numbern");
printf("n");
scanf("%d", &numberChosen);

while (numberChosen != secretNumber) {

if (numberChosen < secretNumber) {
printf("Too Lown");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen > secretNumber) {
printf("Too highn");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen ==secretNumber) {
attempt++;
break;
}

[myArray addObject:[NSNumber numberWithInt:numberChosen]];
}
if (numberChosen == secretNumber) {
NSLog(@"nGood job, you guessed in %i tries", attempt);

}

return resultOutput;
}


My output should be:



Guess a number:
> 12
Too Low!
Guess a number:
> 65
Too High!
Guess a number:
> 65
Already Guessed!
Guess a number:
> asdf
Not a number! Try again!
Guess a number:
> 42
You got it! You took 3 attempts!


I got as far as creating the loop that verifies whether you're higher or lower and how many tries it took. What I am stuck on now is verifying whether the input was a letter, and if so to display on the console that it is not valid. I am also having trouble with verifying whether a number has already been inputted and displaying a message saying that this number has already been guessed.



Help would be appreciated, thanks!










share|improve this question


















  • 1





    This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

    – matt
    Jan 3 at 18:05











  • Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

    – OmieiOS
    Jan 3 at 19:48











  • Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

    – matt
    Jan 3 at 19:57











  • Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

    – matt
    Jan 3 at 19:57













  • Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

    – OmieiOS
    Jan 4 at 4:32


















0















i know there have been a few questions on this game, but mine has a few more instructions so it makes it a bit more difficult. I am almost done, I just have a couple more things to complete. my code is as follows:



- (NSString *)output {

NSMutableString *resultOutput = [[NSMutableString alloc] init];
int secretNumber= arc4random_uniform(10);
int numberChosen;
int attempt=0;

NSMutableArray<NSNumber*> *myArray = [[NSMutableArray alloc] init];

printf("Guess a Numbern");
printf("n");
scanf("%d", &numberChosen);

while (numberChosen != secretNumber) {

if (numberChosen < secretNumber) {
printf("Too Lown");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen > secretNumber) {
printf("Too highn");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen ==secretNumber) {
attempt++;
break;
}

[myArray addObject:[NSNumber numberWithInt:numberChosen]];
}
if (numberChosen == secretNumber) {
NSLog(@"nGood job, you guessed in %i tries", attempt);

}

return resultOutput;
}


My output should be:



Guess a number:
> 12
Too Low!
Guess a number:
> 65
Too High!
Guess a number:
> 65
Already Guessed!
Guess a number:
> asdf
Not a number! Try again!
Guess a number:
> 42
You got it! You took 3 attempts!


I got as far as creating the loop that verifies whether you're higher or lower and how many tries it took. What I am stuck on now is verifying whether the input was a letter, and if so to display on the console that it is not valid. I am also having trouble with verifying whether a number has already been inputted and displaying a message saying that this number has already been guessed.



Help would be appreciated, thanks!










share|improve this question


















  • 1





    This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

    – matt
    Jan 3 at 18:05











  • Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

    – OmieiOS
    Jan 3 at 19:48











  • Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

    – matt
    Jan 3 at 19:57











  • Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

    – matt
    Jan 3 at 19:57













  • Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

    – OmieiOS
    Jan 4 at 4:32














0












0








0








i know there have been a few questions on this game, but mine has a few more instructions so it makes it a bit more difficult. I am almost done, I just have a couple more things to complete. my code is as follows:



- (NSString *)output {

NSMutableString *resultOutput = [[NSMutableString alloc] init];
int secretNumber= arc4random_uniform(10);
int numberChosen;
int attempt=0;

NSMutableArray<NSNumber*> *myArray = [[NSMutableArray alloc] init];

printf("Guess a Numbern");
printf("n");
scanf("%d", &numberChosen);

while (numberChosen != secretNumber) {

if (numberChosen < secretNumber) {
printf("Too Lown");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen > secretNumber) {
printf("Too highn");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen ==secretNumber) {
attempt++;
break;
}

[myArray addObject:[NSNumber numberWithInt:numberChosen]];
}
if (numberChosen == secretNumber) {
NSLog(@"nGood job, you guessed in %i tries", attempt);

}

return resultOutput;
}


My output should be:



Guess a number:
> 12
Too Low!
Guess a number:
> 65
Too High!
Guess a number:
> 65
Already Guessed!
Guess a number:
> asdf
Not a number! Try again!
Guess a number:
> 42
You got it! You took 3 attempts!


I got as far as creating the loop that verifies whether you're higher or lower and how many tries it took. What I am stuck on now is verifying whether the input was a letter, and if so to display on the console that it is not valid. I am also having trouble with verifying whether a number has already been inputted and displaying a message saying that this number has already been guessed.



Help would be appreciated, thanks!










share|improve this question














i know there have been a few questions on this game, but mine has a few more instructions so it makes it a bit more difficult. I am almost done, I just have a couple more things to complete. my code is as follows:



- (NSString *)output {

NSMutableString *resultOutput = [[NSMutableString alloc] init];
int secretNumber= arc4random_uniform(10);
int numberChosen;
int attempt=0;

NSMutableArray<NSNumber*> *myArray = [[NSMutableArray alloc] init];

printf("Guess a Numbern");
printf("n");
scanf("%d", &numberChosen);

while (numberChosen != secretNumber) {

if (numberChosen < secretNumber) {
printf("Too Lown");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen > secretNumber) {
printf("Too highn");
printf("Guess Againn");
scanf("%d", &numberChosen);
attempt++;
}
else if (numberChosen ==secretNumber) {
attempt++;
break;
}

[myArray addObject:[NSNumber numberWithInt:numberChosen]];
}
if (numberChosen == secretNumber) {
NSLog(@"nGood job, you guessed in %i tries", attempt);

}

return resultOutput;
}


My output should be:



Guess a number:
> 12
Too Low!
Guess a number:
> 65
Too High!
Guess a number:
> 65
Already Guessed!
Guess a number:
> asdf
Not a number! Try again!
Guess a number:
> 42
You got it! You took 3 attempts!


I got as far as creating the loop that verifies whether you're higher or lower and how many tries it took. What I am stuck on now is verifying whether the input was a letter, and if so to display on the console that it is not valid. I am also having trouble with verifying whether a number has already been inputted and displaying a message saying that this number has already been guessed.



Help would be appreciated, thanks!







objective-c arrays loops






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 16:49









OmieiOSOmieiOS

1416




1416








  • 1





    This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

    – matt
    Jan 3 at 18:05











  • Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

    – OmieiOS
    Jan 3 at 19:48











  • Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

    – matt
    Jan 3 at 19:57











  • Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

    – matt
    Jan 3 at 19:57













  • Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

    – OmieiOS
    Jan 4 at 4:32














  • 1





    This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

    – matt
    Jan 3 at 18:05











  • Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

    – OmieiOS
    Jan 3 at 19:48











  • Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

    – matt
    Jan 3 at 19:57











  • Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

    – matt
    Jan 3 at 19:57













  • Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

    – OmieiOS
    Jan 4 at 4:32








1




1





This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

– matt
Jan 3 at 18:05





This is exactly the same as your previous stackoverflow.com/questions/54016066/…. Do not do that. If your question doesn't get the attention you desire, edit it to form a better question.

– matt
Jan 3 at 18:05













Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

– OmieiOS
Jan 3 at 19:48





Sorry! I wasn't getting much help on the one posted from before so I thought I'd ask again.

– OmieiOS
Jan 3 at 19:48













Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

– matt
Jan 3 at 19:57





Yes, and that's exactly what I'm saying you must not do - or if you do, delete the previous question.

– matt
Jan 3 at 19:57













Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

– matt
Jan 3 at 19:57







Also I gave you a very useful comment on the previous question: did you think about what I said? If you did, you should have been able to solve that part of the problem.

– matt
Jan 3 at 19:57















Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

– OmieiOS
Jan 4 at 4:32





Matt, thanks for the documentation, but I already read through it, still could not figure out what I was doing wrong.

– OmieiOS
Jan 4 at 4:32












2 Answers
2






active

oldest

votes


















0














Use the return value of scanf.



From the documentation




RETURN VALUE



This function returns the number of input items assigned. This can be
fewer than provided for, or even zero, in the event of a matching failure.
Zero indicates that, although there was input available, no conversions
were assigned; typically this is due to an invalid input character,
such as an alphabetic character for a `%d' conversion
. The value EOF is
returned if an input failure occurs before any conversion such as an end-of-file
occurs. If an error or end-of-file occurs after conversion has
begun, the number of conversions which were successfully completed is
returned.




Edit:



If you use the return value you have to empty the buffer in case letters have been entered.



This is an implementation of all conditions. Rather than an array a set is used to avoid duplicates:



int secretNumber = arc4random_uniform(10);
int numberChosen = 0;
int returnValue = -1;
int attempt = 0;

NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

printf("Guess a Numbern");
printf("n");

while (numberChosen != secretNumber) {
returnValue = scanf("%d", &numberChosen);
attempt++;
if (numberChosen == secretNumber) {
break;
}
else if ([set containsObject:@(numberChosen)]) {
printf("Already Guessed!n");
}
else if (returnValue == 0) {
printf("Not a number!n");
int c;
while((c = getchar()) != 'n' && c != EOF);
}
else if (numberChosen < secretNumber) {
printf("Too Lown");
[set addObject:@(numberChosen)];
}
else if (numberChosen > secretNumber) {
printf("Too highn");
[set addObject:@(numberChosen)];
}
printf("Guess Againn");
printf("n");

}
NSLog(@"nGood job, you guessed in %i tries", attempt);





share|improve this answer


























  • But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

    – OmieiOS
    Jan 3 at 17:27











  • I updated the answer with a full solution.

    – vadian
    Jan 3 at 17:47











  • Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

    – OmieiOS
    Jan 4 at 4:27



















0














Where possible, use Objective-C types so you can take advantage of built-in classes such as a number formatter to parse input and an NSArray to record prior guesses. Use methods to organize the code so the game algorithm is clear.



@property(strong,nonatomic) NSMutableArray *priorGuesses;

- (NSNumber *)promptForNumber {
char cstring[256];

NSLog(@"Guess a Numbern");
scanf("%s", cstring); // note that input exceeding 256 chars will write past this buffer

NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
return [formatter numberFromString:string];
}

- (BOOL)isUnique:(NSNumber *)number {
if ([self.priorGuesses containsObject:number]) return NO;
[self.priorGuesses addObject:number];
return YES;
}

- (void)game {
BOOL gameOn = YES;
self.priorGuesses = [NSMutableArray array];

NSInteger secretNumber= arc4random_uniform(10);
NSNumber *guess = @(-1);

while (gameOn) {
guess = [self promptForNumber];
if (guess && [self isUnique:guess]) {
NSInteger guessInt = [guess intValue];
if (guessInt == secretNumber) {
// notice that there's no need to count attempts in a separate var
NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
gameOn = NO;
} else if (guessInt > secretNumber) {
NSLog(@"Too high");
} else { // by implication, guessInt < secretNumber
NSLog(@"Too low");
}
} else {
NSLog((guess)? @"Already guessedn" : @"Not a numbern");
}
}
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026497%2fverify-whether-an-input-is-an-number-and-whether-a-number-has-been-repeated-or-n%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Use the return value of scanf.



    From the documentation




    RETURN VALUE



    This function returns the number of input items assigned. This can be
    fewer than provided for, or even zero, in the event of a matching failure.
    Zero indicates that, although there was input available, no conversions
    were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion
    . The value EOF is
    returned if an input failure occurs before any conversion such as an end-of-file
    occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.




    Edit:



    If you use the return value you have to empty the buffer in case letters have been entered.



    This is an implementation of all conditions. Rather than an array a set is used to avoid duplicates:



    int secretNumber = arc4random_uniform(10);
    int numberChosen = 0;
    int returnValue = -1;
    int attempt = 0;

    NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

    printf("Guess a Numbern");
    printf("n");

    while (numberChosen != secretNumber) {
    returnValue = scanf("%d", &numberChosen);
    attempt++;
    if (numberChosen == secretNumber) {
    break;
    }
    else if ([set containsObject:@(numberChosen)]) {
    printf("Already Guessed!n");
    }
    else if (returnValue == 0) {
    printf("Not a number!n");
    int c;
    while((c = getchar()) != 'n' && c != EOF);
    }
    else if (numberChosen < secretNumber) {
    printf("Too Lown");
    [set addObject:@(numberChosen)];
    }
    else if (numberChosen > secretNumber) {
    printf("Too highn");
    [set addObject:@(numberChosen)];
    }
    printf("Guess Againn");
    printf("n");

    }
    NSLog(@"nGood job, you guessed in %i tries", attempt);





    share|improve this answer


























    • But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

      – OmieiOS
      Jan 3 at 17:27











    • I updated the answer with a full solution.

      – vadian
      Jan 3 at 17:47











    • Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

      – OmieiOS
      Jan 4 at 4:27
















    0














    Use the return value of scanf.



    From the documentation




    RETURN VALUE



    This function returns the number of input items assigned. This can be
    fewer than provided for, or even zero, in the event of a matching failure.
    Zero indicates that, although there was input available, no conversions
    were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion
    . The value EOF is
    returned if an input failure occurs before any conversion such as an end-of-file
    occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.




    Edit:



    If you use the return value you have to empty the buffer in case letters have been entered.



    This is an implementation of all conditions. Rather than an array a set is used to avoid duplicates:



    int secretNumber = arc4random_uniform(10);
    int numberChosen = 0;
    int returnValue = -1;
    int attempt = 0;

    NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

    printf("Guess a Numbern");
    printf("n");

    while (numberChosen != secretNumber) {
    returnValue = scanf("%d", &numberChosen);
    attempt++;
    if (numberChosen == secretNumber) {
    break;
    }
    else if ([set containsObject:@(numberChosen)]) {
    printf("Already Guessed!n");
    }
    else if (returnValue == 0) {
    printf("Not a number!n");
    int c;
    while((c = getchar()) != 'n' && c != EOF);
    }
    else if (numberChosen < secretNumber) {
    printf("Too Lown");
    [set addObject:@(numberChosen)];
    }
    else if (numberChosen > secretNumber) {
    printf("Too highn");
    [set addObject:@(numberChosen)];
    }
    printf("Guess Againn");
    printf("n");

    }
    NSLog(@"nGood job, you guessed in %i tries", attempt);





    share|improve this answer


























    • But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

      – OmieiOS
      Jan 3 at 17:27











    • I updated the answer with a full solution.

      – vadian
      Jan 3 at 17:47











    • Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

      – OmieiOS
      Jan 4 at 4:27














    0












    0








    0







    Use the return value of scanf.



    From the documentation




    RETURN VALUE



    This function returns the number of input items assigned. This can be
    fewer than provided for, or even zero, in the event of a matching failure.
    Zero indicates that, although there was input available, no conversions
    were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion
    . The value EOF is
    returned if an input failure occurs before any conversion such as an end-of-file
    occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.




    Edit:



    If you use the return value you have to empty the buffer in case letters have been entered.



    This is an implementation of all conditions. Rather than an array a set is used to avoid duplicates:



    int secretNumber = arc4random_uniform(10);
    int numberChosen = 0;
    int returnValue = -1;
    int attempt = 0;

    NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

    printf("Guess a Numbern");
    printf("n");

    while (numberChosen != secretNumber) {
    returnValue = scanf("%d", &numberChosen);
    attempt++;
    if (numberChosen == secretNumber) {
    break;
    }
    else if ([set containsObject:@(numberChosen)]) {
    printf("Already Guessed!n");
    }
    else if (returnValue == 0) {
    printf("Not a number!n");
    int c;
    while((c = getchar()) != 'n' && c != EOF);
    }
    else if (numberChosen < secretNumber) {
    printf("Too Lown");
    [set addObject:@(numberChosen)];
    }
    else if (numberChosen > secretNumber) {
    printf("Too highn");
    [set addObject:@(numberChosen)];
    }
    printf("Guess Againn");
    printf("n");

    }
    NSLog(@"nGood job, you guessed in %i tries", attempt);





    share|improve this answer















    Use the return value of scanf.



    From the documentation




    RETURN VALUE



    This function returns the number of input items assigned. This can be
    fewer than provided for, or even zero, in the event of a matching failure.
    Zero indicates that, although there was input available, no conversions
    were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion
    . The value EOF is
    returned if an input failure occurs before any conversion such as an end-of-file
    occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.




    Edit:



    If you use the return value you have to empty the buffer in case letters have been entered.



    This is an implementation of all conditions. Rather than an array a set is used to avoid duplicates:



    int secretNumber = arc4random_uniform(10);
    int numberChosen = 0;
    int returnValue = -1;
    int attempt = 0;

    NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

    printf("Guess a Numbern");
    printf("n");

    while (numberChosen != secretNumber) {
    returnValue = scanf("%d", &numberChosen);
    attempt++;
    if (numberChosen == secretNumber) {
    break;
    }
    else if ([set containsObject:@(numberChosen)]) {
    printf("Already Guessed!n");
    }
    else if (returnValue == 0) {
    printf("Not a number!n");
    int c;
    while((c = getchar()) != 'n' && c != EOF);
    }
    else if (numberChosen < secretNumber) {
    printf("Too Lown");
    [set addObject:@(numberChosen)];
    }
    else if (numberChosen > secretNumber) {
    printf("Too highn");
    [set addObject:@(numberChosen)];
    }
    printf("Guess Againn");
    printf("n");

    }
    NSLog(@"nGood job, you guessed in %i tries", attempt);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 18:07

























    answered Jan 3 at 17:04









    vadianvadian

    155k17165191




    155k17165191













    • But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

      – OmieiOS
      Jan 3 at 17:27











    • I updated the answer with a full solution.

      – vadian
      Jan 3 at 17:47











    • Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

      – OmieiOS
      Jan 4 at 4:27



















    • But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

      – OmieiOS
      Jan 3 at 17:27











    • I updated the answer with a full solution.

      – vadian
      Jan 3 at 17:47











    • Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

      – OmieiOS
      Jan 4 at 4:27

















    But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

    – OmieiOS
    Jan 3 at 17:27





    But my input needs to be a number and if I enter a letter I get stuck in a continuous loop, I don't get a response that says 0.

    – OmieiOS
    Jan 3 at 17:27













    I updated the answer with a full solution.

    – vadian
    Jan 3 at 17:47





    I updated the answer with a full solution.

    – vadian
    Jan 3 at 17:47













    Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

    – OmieiOS
    Jan 4 at 4:27





    Thanks again, vadian. One adjustment, I had to move the NSLog at the bottom up to where it says if (numberChosen == secretNumber)

    – OmieiOS
    Jan 4 at 4:27













    0














    Where possible, use Objective-C types so you can take advantage of built-in classes such as a number formatter to parse input and an NSArray to record prior guesses. Use methods to organize the code so the game algorithm is clear.



    @property(strong,nonatomic) NSMutableArray *priorGuesses;

    - (NSNumber *)promptForNumber {
    char cstring[256];

    NSLog(@"Guess a Numbern");
    scanf("%s", cstring); // note that input exceeding 256 chars will write past this buffer

    NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    return [formatter numberFromString:string];
    }

    - (BOOL)isUnique:(NSNumber *)number {
    if ([self.priorGuesses containsObject:number]) return NO;
    [self.priorGuesses addObject:number];
    return YES;
    }

    - (void)game {
    BOOL gameOn = YES;
    self.priorGuesses = [NSMutableArray array];

    NSInteger secretNumber= arc4random_uniform(10);
    NSNumber *guess = @(-1);

    while (gameOn) {
    guess = [self promptForNumber];
    if (guess && [self isUnique:guess]) {
    NSInteger guessInt = [guess intValue];
    if (guessInt == secretNumber) {
    // notice that there's no need to count attempts in a separate var
    NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
    gameOn = NO;
    } else if (guessInt > secretNumber) {
    NSLog(@"Too high");
    } else { // by implication, guessInt < secretNumber
    NSLog(@"Too low");
    }
    } else {
    NSLog((guess)? @"Already guessedn" : @"Not a numbern");
    }
    }
    }





    share|improve this answer




























      0














      Where possible, use Objective-C types so you can take advantage of built-in classes such as a number formatter to parse input and an NSArray to record prior guesses. Use methods to organize the code so the game algorithm is clear.



      @property(strong,nonatomic) NSMutableArray *priorGuesses;

      - (NSNumber *)promptForNumber {
      char cstring[256];

      NSLog(@"Guess a Numbern");
      scanf("%s", cstring); // note that input exceeding 256 chars will write past this buffer

      NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
      NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
      return [formatter numberFromString:string];
      }

      - (BOOL)isUnique:(NSNumber *)number {
      if ([self.priorGuesses containsObject:number]) return NO;
      [self.priorGuesses addObject:number];
      return YES;
      }

      - (void)game {
      BOOL gameOn = YES;
      self.priorGuesses = [NSMutableArray array];

      NSInteger secretNumber= arc4random_uniform(10);
      NSNumber *guess = @(-1);

      while (gameOn) {
      guess = [self promptForNumber];
      if (guess && [self isUnique:guess]) {
      NSInteger guessInt = [guess intValue];
      if (guessInt == secretNumber) {
      // notice that there's no need to count attempts in a separate var
      NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
      gameOn = NO;
      } else if (guessInt > secretNumber) {
      NSLog(@"Too high");
      } else { // by implication, guessInt < secretNumber
      NSLog(@"Too low");
      }
      } else {
      NSLog((guess)? @"Already guessedn" : @"Not a numbern");
      }
      }
      }





      share|improve this answer


























        0












        0








        0







        Where possible, use Objective-C types so you can take advantage of built-in classes such as a number formatter to parse input and an NSArray to record prior guesses. Use methods to organize the code so the game algorithm is clear.



        @property(strong,nonatomic) NSMutableArray *priorGuesses;

        - (NSNumber *)promptForNumber {
        char cstring[256];

        NSLog(@"Guess a Numbern");
        scanf("%s", cstring); // note that input exceeding 256 chars will write past this buffer

        NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        return [formatter numberFromString:string];
        }

        - (BOOL)isUnique:(NSNumber *)number {
        if ([self.priorGuesses containsObject:number]) return NO;
        [self.priorGuesses addObject:number];
        return YES;
        }

        - (void)game {
        BOOL gameOn = YES;
        self.priorGuesses = [NSMutableArray array];

        NSInteger secretNumber= arc4random_uniform(10);
        NSNumber *guess = @(-1);

        while (gameOn) {
        guess = [self promptForNumber];
        if (guess && [self isUnique:guess]) {
        NSInteger guessInt = [guess intValue];
        if (guessInt == secretNumber) {
        // notice that there's no need to count attempts in a separate var
        NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
        gameOn = NO;
        } else if (guessInt > secretNumber) {
        NSLog(@"Too high");
        } else { // by implication, guessInt < secretNumber
        NSLog(@"Too low");
        }
        } else {
        NSLog((guess)? @"Already guessedn" : @"Not a numbern");
        }
        }
        }





        share|improve this answer













        Where possible, use Objective-C types so you can take advantage of built-in classes such as a number formatter to parse input and an NSArray to record prior guesses. Use methods to organize the code so the game algorithm is clear.



        @property(strong,nonatomic) NSMutableArray *priorGuesses;

        - (NSNumber *)promptForNumber {
        char cstring[256];

        NSLog(@"Guess a Numbern");
        scanf("%s", cstring); // note that input exceeding 256 chars will write past this buffer

        NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        return [formatter numberFromString:string];
        }

        - (BOOL)isUnique:(NSNumber *)number {
        if ([self.priorGuesses containsObject:number]) return NO;
        [self.priorGuesses addObject:number];
        return YES;
        }

        - (void)game {
        BOOL gameOn = YES;
        self.priorGuesses = [NSMutableArray array];

        NSInteger secretNumber= arc4random_uniform(10);
        NSNumber *guess = @(-1);

        while (gameOn) {
        guess = [self promptForNumber];
        if (guess && [self isUnique:guess]) {
        NSInteger guessInt = [guess intValue];
        if (guessInt == secretNumber) {
        // notice that there's no need to count attempts in a separate var
        NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
        gameOn = NO;
        } else if (guessInt > secretNumber) {
        NSLog(@"Too high");
        } else { // by implication, guessInt < secretNumber
        NSLog(@"Too low");
        }
        } else {
        NSLog((guess)? @"Already guessedn" : @"Not a numbern");
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 18:02









        danhdanh

        49.2k977114




        49.2k977114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026497%2fverify-whether-an-input-is-an-number-and-whether-a-number-has-been-repeated-or-n%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]