In: Computer Science
#include
#include
#include
#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;
}
void regCars(Car reg[], int *pNrOfCars)
{
char again[WORDLENGTH] = "yes", model[WORDLENGTH];
int year, milage;
while (strcmp(again, "yes") == 0)
{
printf("Ange model:");
scanf("%s%*c", model);
printf("Ange year:");
scanf("%d%*c", &year);
printf("Ange milage:");
scanf("%d%*c", &milage);
reg[*pNrOfCars] = createCar(model, year, milage);
(*pNrOfCars)++;
printf("Do you rigister another car? enter yes or no:\n");
scanf("%s%*c", again);
}
}
void printRegister(Car reg[], int nrOfCars)
{
int i = 0;
for (i = 0; i < nrOfCars; i++)
{
printf("%d. Car: %s, Year model: %d, Mil: %d\n", i + 1, reg[i].model, reg[i].year, reg[i].milage);
}
}
int main()
{
Car carRegister[MAX];
int nrOfCars = 0;
char r[100];
printf("register a car\n");
regCars(carRegister, &nrOfCars);
printf("Do you want to print the cars details or quit? entr print or quit:\n");
scanf("%s", r);
if (strcmp(r, "print") == 0)
printRegister(carRegister, nrOfCars);
else
exit(1);
return 0;
}
Add a menu item that is "Increase miles" where you should be able to select a car by entering its location in the register and then can increase the number of miles it has traveled. So to increase the fiat's miles above, enter 3.
Edit: this question is a following question to a previous one which was:
In question number one we should Add the function that prints
all cars in the register in our car register program. The printout
should look as follows:
1. Car: Volvo, Year model: 2011, Mil: 3000
2. Car: Saab, Year model: 2000, Mil: 5000
Car: Fiat, Model year: 1999, Mil: 40000
Then in a separate question we should Change the program so that it
starts with a menu according to
below. Depending on what you choose, the correct function is
called. Then the user gets another chance to choose until he
chooses to quit.
• register cars
• Print all cars
• Exit
#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;
void printCar(Car c){
printf("Bil: %s, Arsmodell: %d, mil:
%d\n",c.model,c.year,c.milage);
}
Car createCar(char model[],int year, int milage){
Car c;
strcpy(c.model,model);
c.year=year;
c.milage=milage;
return c;
}
void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "ja", model[WORDLENGTH];
int year,milage;
while(strcmp(again,"ja")==0){
printf("Ange model:");
scanf("%s%*c",model);
printf("Ange year:");
scanf("%d%*c",&year);
printf("Ange milage:");
scanf("%d%*c",&milage);
reg[*pNrOfCars]=createCar(model,year,milage);
(*pNrOfCars)++;
printf("Vill du fortsatta? (ja/nej)");
scanf("%s%*c",again); }
}
void printRegister(Car reg[], int nrOfCars){}
int main(){
Car carRegister[MAX];
int nrOfCars=0;
regCars(carRegister,&nrOfCars);
printRegister(carRegister, nrOfCars);
return 0;
}
the answer was:
#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; } void regCars(Car reg[],int *pNrOfCars){ char again[WORDLENGTH] = "yes", model[WORDLENGTH]; int year,milage; while(strcmp(again,"yes")==0){ printf("Ange model:"); scanf("%s%*c",model); printf("Ange year:"); scanf("%d%*c",&year); printf("Ange milage:"); scanf("%d%*c",&milage); reg[*pNrOfCars]=createCar(model,year,milage); (*pNrOfCars)++; printf("Do you rigister another car? enter yes or no:\n"); scanf("%s%*c",again); } } void printRegister(Car reg[], int nrOfCars){ int i=0; for(i=0;i<nrOfCars;i++) { printf("%d. Car: %s, Year model: %d, Mil: %d\n",i+1,reg[i].model,reg[i].year,reg[i].milage); } } int main(){ Car carRegister[MAX]; int nrOfCars=0; char r[100]; printf("register a car\n"); regCars(carRegister,&nrOfCars); printf("Do you want to print the cars details or quit? entr print or quit:\n"); scanf("%s",r); if(strcmp(r,"print")==0) printRegister(carRegister, nrOfCars); else exit(1); return 0; }
Updated program with an 'Increase miles' option is below:
#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;
}
void regCars(Car reg[],int *pNrOfCars){
char again[WORDLENGTH] = "yes", model[WORDLENGTH];
int year,milage;
while(strcmp(again,"yes")==0){
printf("Ange model:");
scanf("%s%*c",model);
printf("Ange year:");
scanf("%d%*c",&year);
printf("Ange milage:");
scanf("%d%*c",&milage);
reg[*pNrOfCars]=createCar(model,year,milage);
(*pNrOfCars)++;
printf("Do you rigister another car? enter yes or no:\n");
scanf("%s%*c",again);
}
}
void printRegister(Car reg[], int nrOfCars){
int i=0;
for(i=0;i<nrOfCars;i++)
printf("%d. Car: %s, Year model: %d, Mil:
%d\n",i+1,reg[i].model,reg[i].year,reg[i].milage);
}
void increaseMiles (Car reg[], int nrOfCars) {
int carno, newmiles;
printf ("Enter car no to increase miles:");
scanf ("%d%*c", &carno);
printf ("Enter new no of miles for this car:");
scanf ("%d%*c", &newmiles);
if ((carno > 0) && (carno <= nrOfCars) )
reg[carno-1].milage = newmiles;
}
int main(){
Car carRegister[MAX];
int nrOfCars=0;
char r[100];
printf("register a car\n");
regCars(carRegister,&nrOfCars);
while (1== 1) {
printf("Do you want to print cars details, increase miles or quit?
enter print, miles or quit:\n");
scanf("%s",r);
if(strcmp(r,"print")==0)
printRegister(carRegister, nrOfCars);
else if(strcmp(r,"miles")==0)
increaseMiles (carRegister, nrOfCars);
else
break;
}
return 0;
}