0
我试图从标准输入中读取字符和行,并且遇到了问题。scanf第二次未读取输入
这是我想读入变量输入:
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
第一行是两个数字代表有多少零件将被列出,然后又有多少企业会尽量满足这些零件。
六条线是试图匹配部件要求的公司的名称,其后的下一行是两个数字:该车将花费多少,以及制造商可以满足多少部件要求。
然后,列出制造商可以放入汽车的零件。
然后,来自其他制造商,他们的汽车的成本,他们能够满足零件,零件清单等
最后,我打印出来谁中标。
这是我使用这样做代码:
#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两次它退出之前。任何想法为什么?