0

It works when I try to compile it but the first time i register a car it skips 'entering the model' and then if I choose to continue and register a new car it works just fine. I don't know why it does or what cause that. If I run it without a menu (loop) works fine. Please help!

#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;
 }
Kevin Punkt
  • 113
  • 1
  • 1
  • 7

0 Answers0