0

我正在尝试通过编写一个小刽子手应用程序来学习一些技巧。我有我需要的大部分东西;但是,我有一个函数可以提示用户输入一个字母(使用旧的 C 方式),然后将其转换为一个NSString,这样我就可以NSString轻松地找到一个特定的字母或获取大小。

但是,我需要NSString从函数中返回(指针)并在 main 中使用它来传递给其他函数。

#import <Foundation/Foundation.h>

#define WORD_LIST_SIZE 3

void displayBlanks(float numChars);
NSString * askUserForLetter();

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSString * userLetter;

    int wrongCount = 0;
    int rightCount = 0;

    int randomWordIndex;
    float lettersInWord;

    NSString *wordList[WORD_LIST_SIZE];
    wordList[0] = @"dog";
    wordList[1] = @"bike";
    wordList[2] = @"plane";


    //
    //generates random index for word
    randomWordIndex = arc4random() % WORD_LIST_SIZE;


    //
    //gets the length of the random word
    lettersInWord = [wordList[randomWordIndex] length];


    //
    //Display title and blank
    printf("Guess the letters! Be careful! You may get hung! \n\n");
    displayBlanks(lettersInWord);


    //
    //ask for user letter the first time, return NSSTring 'userLetter'
    askUserForLetter();

    /*
     while (wrongCount < 9 && rightCount < lettersInWord) {
        if ([wordList[randomWordIndex] rangeOfString:userLetter].location != NSNotFound) {
            NSLog(@"Correct!");
            askUserForLetter();
            rightCount++;
        }
        else{
            NSLog(@"Wrong!");
            askUserForLetter();
            wrongCount++;
        }
    }
     */

    NSLog(@"Your letter is: %@", userLetter);


}
return 0;
}

void displayBlanks(float numChars){
int i;
for (i = 0; i < numChars; i++) {
    printf(" _ ");
}
printf("\n\n");
}


NSString * askUserForLetter (){
char userTry;

printf("Enter a letter you think is in the word: ");
scanf("%c", &userTry);
NSString * userGuess = [NSString stringWithFormat:@"%c", userTry];

return userGuess;
}
4

0 回答 0