2014-11-16 1 views
-1

다음 코드는 컴파일러에서 제대로 실행될 수 있지만 식별 할 수없는 사소한 오류가 있습니다. 첫 번째 자동차 세부 정보 만 입력 할 수 있지만 두 번째 레코드가 오면 두 번째 입력 문으로 이동합니다. 첫 번째 입력 문 대신 첫 번째 one.please가 가리키는 곳을 수정하고 모든 것에 대해 감사드립니다.코드를 실행할 수 있지만 어디에 있는지 모르는 사소한 오류가 있습니까?

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

struct auto_t 
{ 
    char make[20]; 
    char model[20]; 
    int oRead; 
    int manufDate; 
    int purchDate; 
    float cpct; 
    float f_level; 
}; 

int main(int argc, char** argv) 
{ 
    struct auto_t automobile[2]; 
    int i; 
    for(i=0; i<2; i++) 
    { 
     printf("component:"); 
     gets(automobile[i].make); 
     printf("model:"); 
     gets(automobile[i].model); 
     printf("Odoer reading:"); 
     scanf_s("%d", &automobile[i].oRead); 
     printf("manufacturing date(ddmmyyyy):"); 
     scanf_s("%d", &automobile[i].manufDate); 
     printf("purchasing date(ddmmyyyy):"); 
     scanf_s("%d", &automobile[i].purchDate); 
     printf("capacity:"); 
     scanf_s("%f", &automobile[i].cpct); 
     printf("fuel level:"); 
     scanf_s("%f", &automobile[i].f_level); 
     printf("\n"); 
    } 
    printf("\nName\tModel\tOReaad\tManufDate\tPurchDate\tCpct\tFuel\n"); 
    for(i=0; i<2; i++) 
    { 
     printf("%s\t%s\t%d\t%d\t%d\t%.f\t%.f\n", automobile[i].make, automobile[i].model, automobile[i].oRead, automobile[i].manufDate, automobile[i].purchDate, automobile[i].cpct, automobile[i].f_level); 
    } 
    system("pause"); 
} 
+2

***에

printf("component:"); fgets(automobile[i].make,20,stdin); printf("model:"); fgets(automobile[i].model,20,stdin); 

하거나

printf("component:"); gets(automobile[i].make); printf("model:"); gets(automobile[i].model); 

을 변경'얻는다()'! *** 그것은 위험하고 비표준입니다. 대신'fgets()'를 사용하십시오. 그리고 디버거도 사용하십시오. fgets()를 사용할 때 –

+0

컴파일러 오류가 발생합니다. gets() 또는 fgets()를 사용하는 대신에 다른 옵션이 있습니까? 정보를 주셔서 감사합니다 The Paramagnetic Croissant –

+1

"fgets를 사용할 때 컴파일러 표시 오류"가 도움이되지 않습니다. 당신은 우리에게 ** 어떤 에러가 있는지 특별히 알려줄 필요가있다. ** 또한'fgets()'는 표준적이고 안전하기 때문에 사용해라. –

답변

0

Never use gets() as it is dangerous. 대신 fgets()을 사용하십시오. 그래서 사용하지 마십시오

printf("component:"); 
fgets(automobile[i].make,sizeof(automobile[i].make),stdin); 
printf("model:"); 
fgets(automobile[i].model,sizeof(automobile[i].model),stdin);