当我尝试编译它时它起作用但是我第一次注册汽车它会跳过“进入模型”然后如果我选择继续并注册一辆新车它就可以了。我不知道为什么会这样或者是什么原因。如果我没有菜单(循环)运行它工作正常。请帮忙!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORDLENGTH 30
#define MAX 100
struct car{
char model[WORDLENGTH];
int year;
int milage;
};
typedef struct car Car;
Car createCar(char model[],int year, int milage){
Car c;
strcpy(c.model,model);
c.year=year;
c.milage=milage;
return c;
}
//REGISTER THE CARS FUNCTION
void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "ja", model[WORDLENGTH], tmp[WORDLENGTH];
int year,milage;
while(strcmp(again,"ja")==0){
printf("Enter model:");
gets(model);
printf("Enter year:");
gets(tmp);
year=atoi(tmp);
printf("Enter milage:");
gets(tmp);
milage=atoi(tmp);
reg[*pNrOfCars]=createCar(model,year,milage); (*pNrOfCars)++;
printf("Continue? (ja/nej)");
gets(again);
}
}
int main(){
Car carRegister[MAX];
int nrOfCars=0;
//HERE IS THE MENU:::::::::
char choice;
printf("\nMeny\n1 register\n2 exit\n");
scanf(" %c", &choice);
int run=1;
while(run){
if(choice=='1') regCars(carRegister,&nrOfCars);
if(choice=='2') run=0;
}
return 0;
}