我正在尝试通过编写一个小刽子手应用程序来学习绳索。我有大部分需要的东西;但是,我有一个功能,提示用户输入一个字母(使用旧的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;
}