我今天对我的节目表示普遍关注。我已经花了很多时间(我可以用来学习决赛的时间)在这个项目上,我觉得我需要退一步。当我尝试运行它时,程序会告诉我这些错误。
lines 115, 120, 131, 136, 146:
warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
note: expected 'const char *' but argument is of type 'int'
(请注意,这是一项正在进行中的工作,并不是真正意义上的功能,但如果你看到其他任何内容,请告诉我们)
我班上有一位非常有趣的教授,他以同样有趣的方式教学,所以我在尝试理解这些课程要求的某些方面时遇到了麻烦。非常感谢您提供的任何帮助。提前致谢
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_ITEMS 1000
struct drink {
int drinkNum;
double drinkPrice;
}drinklist[10];
struct items {
double itemPrice;
};
struct raffle {
double rafValue;
};
int main(){
double preTickets, doorTickets, minBid;
int preSold, numItems, rafTickets = 0, rafPrizes, rafCost;
int i, numEvents, x;
FILE* fin;
fin = fopen("input.txt", "r");
// Line#1 from input file
fscanf(fin, "%lf %lf %d", &preTickets, &doorTickets, &preSold);
// Line#2 from input file
fscanf(fin, "%d %lf", &numItems, &minBid);
// This allocates space for the list of auction items dynamically.
struct items* itemList = (struct items*)malloc(sizeof(struct items)*numItems);
// Line#3 from input file.
for(i = 0; i < numItems; i++){
fscanf(fin, "%lf", &itemList[i].itemPrice);
}
// Line#4 fron input file.
fscanf(fin, "%d %d %d", &rafTickets, &rafCost, &rafPrizes);
// Same as the itemList. Creates enough space for the number of raffle prizes you need.
struct raffle* raffleList = (struct raffle*)malloc(sizeof(struct raffle)*rafPrizes);
// Line#5 from input file
for(i = 0; i < rafPrizes; i++){
fscanf(fin, "%lf", &raffleList[i].rafValue);
}
struct drink drinklist[10];
// Line#6 from input file
for(i = 0; i < 10; i++){
fscanf(fin, "%d", &drinklist[i].drinkNum);
}
// Line#7 from input file
for(i = 0; i < 10; i++){
fscanf(fin, "%lf", &drinklist[i].drinkPrice);
}
// This just prints out the output that has been read in so far.
// This will also show you how you can refer to the different members of data that you need later.
printf("%.2lf %.2lf %d\n", preTickets, doorTickets, preSold);
printf("%d %.2lf\n", numItems, minBid);
for(i = 0; i < numItems; i++){
printf("%.2lf ", itemList[i].itemPrice);
}
printf("\n%d %d %d\n", rafTickets, rafCost, rafPrizes);
for(i = 0; i < rafPrizes; i++){
printf("%.2lf ", raffleList[i].rafValue);
}
printf("\n");
for(i = 0; i < 10; i++){
printf("%d ", drinklist[i].drinkNum);
}
printf("\n");
for(i = 0; i < 10; i++){
printf("%.2lf ", drinklist[i].drinkPrice);
}
printf("\n");
// This frees the memory that you allocated dynamically.
free(itemList);
free(raffleList);
// loop that runs from from 0 to < numCommands and read in each command and evaluate it.
fscanf(fin, "%d", &numEvents);
fclose(fin);
int actLoop, boughtTickets, boughtRaffle, boughtDrink;
int itemPrice, currentBid, guestBid, bidOffer;
int guestRaf;
int drinkWanted;
for (actLoop=0; actLoop<numEvents; actLoop++); {
int action;
fscanf(fin, "%s", action);
if (strcmp(action, "BUY TICKET") == 0) {
fscanf(fin, "%d", boughtTickets);
printf("Sold tickets %d - %d", &preSold+1, &preSold+boughtTickets);
}
else if (strcmp(action, "BIDITEM") == 0) {
fscanf(fin, "%d%d%.2lf", &itemList[i], &guestBid, &bidOffer);
if (bidOffer < itemList[i].itemPrice + minBid) {
printf("Bid for item %d rejected for person %d at %.2lf", &itemList[i], &guestBid, &bidOffer);
}
else
printf("Bid for item %d accepted for person %d at %.2lf", &itemList[i], &guestBid, &bidOffer);
itemList[i].itemPrice == bidOffer;
}
else if (strcmp(action, "CLOSEAUCTION") == 0) {
}
else if (strcmp(action, "BUY RAFFLE") == 0) {
fscanf(fin, "%d%d", &boughtRaffle, &guestRaf);
if (boughtRaffle > rafTickets) {
boughtRaffle == rafTickets;
rafTickets = rafTickets - boughtRaffle;
}
printf("Raffle Tickets %d - %d given to person %d", &rafTickets, &rafTickets+boughtRaffle, &guestBid);
}
else if (strcmp(action, "BUY DRINK") == 0) {
fscanf(fin, "%d%d", &drinkWanted, &drinklist[i].drinkNum);
if (drinklist[i].drinkNum < drinkWanted) {
drinkWanted = drinklist[i].drinkNum - drinkWanted;
}
printf("Sold %d of drink %d", &drinklist[i].drinkNum, &drinkWanted);
drinklist[i].drinkNum = drinklist[i].drinkNum - drinkWanted;
}
}
}
答案 0 :(得分:3)
您正在将字符串读入int
,您应该使用char
数组:
char action[200];
fscanf(fin, "%199s", action);
此处您需要boughtTickets
fscanf(fin, "%d", &boughtTickets);
在这里打印变量的地址而不是它们的值:
printf("Sold tickets %d - %d", &preSold+1, &preSold+boughtTickets);
你明白了,请阅读一本好的C书,我推荐K&amp; R.
答案 1 :(得分:1)
strmp
将字符串与另一个字符串进行比较,但对于您的程序,您要将int
(action
)与字符串进行比较。
同样,fscanf(fin, "%s", action);
fscanf
正在从文件输入描述符fin
读取字符串,因此action
的类型应为char缓冲区
char action[256];
fscanf(fin, "%s", action);
if (strcmp(action, "BUY TICKET") == 0) {