Objective C 读取带有空格的字符串
我有以下代码,但根本无法让它显示名称。 如果我有 scanf("%s", inputBuffer);
我只得到第一个单词。它在空白处中断。所以我将其更改为 scanf("%[\n]", inputBuffer);
但仍然不起作用。有什么帮助请...
> int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number;
double payRate, hours, totalPay;
NSString *name;
char inputBuffer[200];
NSLog (@"Enter the number of entries to be processed: ");
scanf ("%i", &number);
for(int i = 1; i <= number; i++){
NSLog (@"Enter the name:");
scanf("%[\n]", inputBuffer);
name = [[NSString alloc] initWithUTF8String:inputBuffer];
NSLog(@"Name: %@", name);
NSLog(@"Hours:%.2lf", hours);
NSLog(@"Pay Rate:%.2lf",payRate);
NSLog(@"Total Pay:%.2lf", totalPay);
}
I have the following code, but can't get it to display the name at all.
If I have scanf("%s", inputBuffer);
i get only the first word. It Breaks at the whitespace. So I changed it to scanf("%[\n]", inputBuffer);
but still does not work. Any help please...
> int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number;
double payRate, hours, totalPay;
NSString *name;
char inputBuffer[200];
NSLog (@"Enter the number of entries to be processed: ");
scanf ("%i", &number);
for(int i = 1; i <= number; i++){
NSLog (@"Enter the name:");
scanf("%[\n]", inputBuffer);
name = [[NSString alloc] initWithUTF8String:inputBuffer];
NSLog(@"Name: %@", name);
NSLog(@"Hours:%.2lf", hours);
NSLog(@"Pay Rate:%.2lf",payRate);
NSLog(@"Total Pay:%.2lf", totalPay);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用:
This works for me: