我试图找出一种方式,这时候数据是像这样的输入:一种处理这种输入的方法?
Name Integer
Name Integer
Name Integer
.
每个名称和整型被输入,并检测到新行时间,东西与该名称做,整数,然后再一次,直到'。'被检测到。
我试过getchar()
循环和IF语句检测'\n'
例如,但似乎没有工作。有任何想法吗?
我试图找出一种方式,这时候数据是像这样的输入:一种处理这种输入的方法?
Name Integer
Name Integer
Name Integer
.
每个名称和整型被输入,并检测到新行时间,东西与该名称做,整数,然后再一次,直到'。'被检测到。
我试过getchar()
循环和IF语句检测'\n'
例如,但似乎没有工作。有任何想法吗?
scanf("%s%d\n", name, &intVar);
if((*name=='.') && (*(name+1)=='\0')){
return 0;
}
这应该是足够了
如何:
#include <string.h>
#include <stdio.h>
int main()
{
char input[30];
gets(input);
while(strcmp(input, ".") != 0)
{
// do stuff
gets(input);
}
return 0;
}
不要直接使用scanf
。令人惊讶的是很难使用。最好阅读整行输入(例如,用fgets
),然后解析它(可能用sscanf
)。
你为什么不尝试使用scanf(“%d \ n”,&myvar)? –
你可以发布一些你的尝试,为什么他们不工作? –
或者'fgets'和'sscanf'的组合? –