2012-12-01 5 views
1

오늘 내 프로그램에 대한 일반적인 우려가 있습니다. 저는이 한 프로그램에서 좋은 시간을 보냈습니다. (결승에서 공부할 수있는 시간이었습니다.) 나는 조금 뒤로 물러 설 필요가 있습니다. 프로그램을 실행하려고하면 이러한 오류가 표시됩니다.'strcmp'의 인수 1을 전달하는 중 오류/예상 'const'char * '인수가'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' 

(이 진행중인 작업이 있습니다, 정말 TOO 기능적 의미하지,하지만 당신은 무엇을 볼 경우, 알려주세요)

내 클래스는 매우 ... 재미있다 비슷한 흥미로운 방법으로 가르치는 교수님, 그래서이 프로그램 요구 사항의 일부 측면을 이해하려고 시도하는 데 어려움을 겪었습니다. 당신이 제공 할 수있는 모든 도움이 크게 감사드립니다. 사전

#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 액션 추천 좋은 C 책을 읽어 보시기 바랍니다; fscanf (fin, "% s", action); 문자열을 정수로 스캔하고 있습니다. 이 경우'action'은 잘못된 타입으로 선언됩니다. 읽을 문자 배열이 필요합니다. – Joe

+1

질문을 편집하고 묻는 질문과 관련이없는 과도한 코드를 모두 제거하십시오. 실제로 문제를 포함하고있는 8 개 또는 10 개를 찾기 위해 처음 100 개 이상의 라인을 걸어 다닐 것을 기대하며 그 원인은 매우 중요하지 않습니다. 감사. –

+0

SSCCE ([단편, 자체 포함, 전체 예제] (http://sscce.org/))가 무엇인지 배워보십시오. 당신이 제공 한 것은 그렇지 않습니다. 컴파일러의 오류 메시지는 매우 명확합니다. 의 말을 들으면서, 인자 1에 대해'const char *'를'strcmp()'에 넘겨야 만하지만, 줄 번호 정보와 함께'int'._를 넘겨 줘야합니다. 더 많은 정보가 필요하십니까? –

답변

3

, 당신은 대신 char 배열을 사용한다 문자 버퍼를해야합니다 수 있습니다 :

char action[200]; 
fscanf(fin, "%199s", action); 

그리고 여기 당신은 boughtTickets

의 주소가 필요 여기
fscanf(fin, "%d", &boughtTickets); 

대신 그 값의 변수의 주소를 인쇄하고 있습니다 :

당신은 아이디어를 얻을
printf("Sold tickets %d - %d", &preSold+1, &preSold+boughtTickets); 

, 나는 K & R.

1

strmp 감사는 다른 문자열에 문자열을 비교하지만 프로그램에 당신은 문자열로 int (action)를 비교한다. fscanf(fin, "%s", action);fscanf에도

는 파일 입력 설명 fin에서 문자열을 읽는 것은 너무 action의 유형 당신은 int에 문자열을 읽고

관련 문제