我试图从标准输入中读取字符和行,但遇到了问题。
这是我试图读入变量的输入:
6 4
engine
brakes
tires
ashtray
vinyl roof
trip computer
Chevrolet
20000.00 3
engine
tires
brakes
Cadillac
70000.00 4
ashtray
vinyl roof
trip computer
engine
Hyundai
10000.00 3
engine
tires
ashtray
Lada
6000.00 1
tires
0 0
第一行是两个数字,表示将列出多少零件,然后将有多少公司尝试匹配这些零件。
下六行是试图匹配零件要求的公司名称,之后的下一行是两个数字:汽车的成本是多少,以及制造商可以满足多少零件要求。
然后,制造商可以在他们的汽车上安装的零件会被列出。
然后是另一个制造商,他们的汽车成本、他们可以满足的零件、零件 list 等。
最后,我打印出谁中标了。
这是我用来执行此操作的代码:
#define MAX_LEN 80
int main(){
int rfpNum = 0;
int n, p, reqMet, numMet, nCopy, numActualMet;
float maxCompliance=-1, lowestBid=-1, tmpCompliance;
float bid;
char req[MAX_LEN], bidder[MAX_LEN];
char winner[MAX_LEN];
char **reqs;
char *pos;
while (scanf("%d %d *[^\n]", &n, &p), (n && p)){
reqs = new char*[n];
nCopy = n;
maxCompliance = -1;
while (nCopy--){
fgets(req, MAX_LEN, stdin);
if ((pos=strchr(req, '\n')) != NULL)
*pos = '\0';
reqs[nCopy] = new char[MAX_LEN];
sprintf(reqs[nCopy], "%s", req);
}
while (p--){
fgets(bidder, MAX_LEN, stdin);
if ((pos=strchr(bidder, '\n')) != NULL)
*pos = '\0';
numActualMet = 0;
scanf("%f %d *[^\n]", &bid, &numMet);
while (numMet--){
fgets(req, MAX_LEN, stdin);
if ((pos=strchr(req, '\n')) != NULL)
*pos = '\0';
for (int i=0; i<n; i++){
if (strcmp(req, reqs[i]) == 0) numActualMet++;
}
}
tmpCompliance = (float) numActualMet/n;
if (tmpCompliance > maxCompliance){
maxCompliance = tmpCompliance;
strncpy(winner, bidder, MAX_LEN);
lowestBid = bid;
}
else if ((maxCompliance == tmpCompliance) && (lowestBid == -1 || bid < lowestBid)) {
lowestBid = bid;
strncpy(winner, bidder, MAX_LEN);
}
}
rfpNum++;
if (rfpNum != 1)
printf("\n");
printf("RFP #%d\n", rfpNum);
printf("%s\n", winner);
}
return 0;
}
问题是,我必须输入 0 0 两次才能退出。有什么想法吗?
最佳答案
Problem is, I have to enter 0 0 twice before it quits. Any ideas why?
线
while (scanf("%d %d *[^\n]", &n, &p), (n && p)){
不仅期望两个整数读入n
和p
,它还等待一个非空白字符与*[^\n匹配]
格式的一部分。如果您输入任何字符,而不仅仅是 0 0
,程序将停止。我能够通过输入 abc
来终止它。
更好的方法是使用 fgets()
,然后使用 sscanf
。
char buffer[100]; // Make it large enough
while ( fgets(buffer, 100, stdin) != NULL )
{
int n = sscanf(buffer, "%d %d", &n, &p);
if ( n != 2 )
{
// Deal with error.
}
if ( n == 0 && p == 0 )
{
break;
}
// Rest of the loop
}
作为一项政策,尽量避免混合使用 fgets
和 fscanf
。当一起使用时,它们充满了问题。
关于c - scanf 没有第二次读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051964/