#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循环中打印下几行而不要求我输入。我该如何解决这个问题?
答案 0 :(得分:0)
您使用的是错误的格式说明符:
scanf("%@", &sg);
^^
由于scanf()
是C运行时库函数,因此不能使用Foundation
类型。相反,您需要使用char
数组和%s
说明符,但OSX可能支持%ms
说明符,因为它通过{{1}分配char *
更安全所以不能轻易溢出。