Objective C:如何修复 scanf() 跳过行
Objective C: How to fix scanf() skipping over lines
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *sg, *ge, *pr, *ln;
NSString *done = @"";
int actn = 0;
NSError *error;
printf("Welcome to Your Playlist\n");
while(actn != 6)
{
printf("\nWhat would you like to do?");
printf("\n1. Enter a New Song, Performer, Genre, and the Length of the Song");
printf("\n2. Show all Songs in Your Playlist");
printf("\n3. Show all Songs in Your Playlist and Sort by Song Title");
printf("\n4. Show all Songs in Your Playlist and Sort by Genre");
printf("\n5. Show all Songs in Your Playlist and Sort by Performer");
printf("\n6. Save and Quit\n");
scanf("%d", &actn);
if(actn == 1)
{
while([done isEqualToString:@"No"] == false)
{
printf("What is the Song Title? ");
scanf("%@", &sg);
printf("Who is the Performer? ");
scanf("%@", &pr);
printf("What is the Genre of this Song? ");
scanf("%@", &ge);
printf("What is the length of the Song? ");
scanf("%@", &ln);
printf("Would you like to quit(Yes or No)? ");
scanf("%@", &done);
}
}
if(actn == 2)
{
NSString *contents = [NSString stringWithContentsOfFile:@"E:/Playlist" encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(@"ERROR while loading from file: %@", error);
}
[contents writeToFile:@"E:/Playlist" atomically:YES encoding:NSUnicodeStringEncoding error:&error];
printf("%@",contents);
}
[pool release];
return 0;
}
它允许输入数字,我首先输入 1。它要求我输入歌曲的标题。在那之后,它会无限地打印出 while 循环中接下来的几行,而不要求我输入。我该如何解决这个问题?
您使用了错误的格式说明符:
scanf("%@", &sg);
^^
由于 scanf()
是一个 C 运行时库函数,您不能使用 Foundation
类型。相反,您需要使用 char
数组和 %s
说明符,但是 OSX 可能支持 %ms
说明符,因为它通过分配 char *
更安全malloc()
所以不容易溢出
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *sg, *ge, *pr, *ln;
NSString *done = @"";
int actn = 0;
NSError *error;
printf("Welcome to Your Playlist\n");
while(actn != 6)
{
printf("\nWhat would you like to do?");
printf("\n1. Enter a New Song, Performer, Genre, and the Length of the Song");
printf("\n2. Show all Songs in Your Playlist");
printf("\n3. Show all Songs in Your Playlist and Sort by Song Title");
printf("\n4. Show all Songs in Your Playlist and Sort by Genre");
printf("\n5. Show all Songs in Your Playlist and Sort by Performer");
printf("\n6. Save and Quit\n");
scanf("%d", &actn);
if(actn == 1)
{
while([done isEqualToString:@"No"] == false)
{
printf("What is the Song Title? ");
scanf("%@", &sg);
printf("Who is the Performer? ");
scanf("%@", &pr);
printf("What is the Genre of this Song? ");
scanf("%@", &ge);
printf("What is the length of the Song? ");
scanf("%@", &ln);
printf("Would you like to quit(Yes or No)? ");
scanf("%@", &done);
}
}
if(actn == 2)
{
NSString *contents = [NSString stringWithContentsOfFile:@"E:/Playlist" encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(@"ERROR while loading from file: %@", error);
}
[contents writeToFile:@"E:/Playlist" atomically:YES encoding:NSUnicodeStringEncoding error:&error];
printf("%@",contents);
}
[pool release];
return 0;
}
它允许输入数字,我首先输入 1。它要求我输入歌曲的标题。在那之后,它会无限地打印出 while 循环中接下来的几行,而不要求我输入。我该如何解决这个问题?
您使用了错误的格式说明符:
scanf("%@", &sg);
^^
由于 scanf()
是一个 C 运行时库函数,您不能使用 Foundation
类型。相反,您需要使用 char
数组和 %s
说明符,但是 OSX 可能支持 %ms
说明符,因为它通过分配 char *
更安全malloc()
所以不容易溢出