2012-12-01 79 views
1

今天我來關注我的程序。我一直在爲這個節目花費大量的時間(我可以用來學習決賽的時間),而我覺得我需要退出一段時間。當我嘗試運行它時,程序會告訴我這些錯誤。傳遞'strcmp'錯誤/期望'const'char *'的參數1,但參數的類型爲'int'

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

INT行動; fscanf(fin,「%s」,action); 您正在將字符串掃描爲整數。在這種情況下'action'被聲明爲錯誤的類型。你需要一個字符數組來讀入。 – Joe

+1

請編輯您的問題,並刪除所有與被問題無關的過多代碼。期望人們通過前100條線來找到實際上包含問題的8或10條線,原因是相當不合理。謝謝。 –

+0

請了解SSCCE([Short,Self-Contained,Complete Example](http://sscce.org/))是什麼。你提供的不是。來自編譯器的錯誤消息非常明確;解釋,_you應該將'const char *'參數1傳遞給'strcmp()',但是你傳遞一個'int'._以及行號信息。你需要更多的信息? –

回答

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的書,我建議ķ& R.

1

strmp比較字符串到另一個字符串,但你的程序你是比較intaction)字符串。

也爲fscanf(fin, "%s", action);fscanf是從文件輸入描述符fin讀取字符串所以action類型應該是字符緩衝區

相關問題