Question: Hello! I am still having trouble with my code. When I run it, it goes straight to the default statement about being invalid and this
Hello!
I am still having trouble with my code. When I run it, it goes straight to the default statement about being invalid and this goes for any selection I make. I structured this the same way my professor did and when I ran his code it seemed to work find. Please help.
As always, all help is appreciated
Solution:
#include
typedef struct{ int year; char make[10]; char model[10]; char color[10]; char condition[10]; double price; }AUTO; int getChoice(); void addAuto(AUTO inventory[], int *esize); void deleteAuto(AUTO inventory[], int *esize); void displayInventory(AUTO inventory[], int esize);
int main(){ char choice; int esize = 0; AUTO inventory[SIZE]; do{ choice = getChoice(); switch(choice){ case '1': addAuto(inventory, &esize); break; case '2': deleteAuto(inventory, &esize); break; case '3': displayInventory(inventory, esize); break; case '4': printf("Exiting the program now, thank you for using my program. "); PAUSE; break; default: printf("Invalid selection, please select one of the options above. "); PAUSE; break; } } while (choice != 4); return 0; } int getChoice(){ int results; printf("Please make a selection: "); char menu[100]="1. Add a Auto 2. Delete Auto 3. Display Inventory 4. Exit "; printf("%s", menu); scanf("%i", &results); return results; } void addAuto(AUTO inventory[], int *esize){ if(*esize == SIZE){ printf("No additions allowed, inventory is full."); PAUSE; return; } printf(" Entering the data: "); printf("Year: "); scanf("%d", &inventory[*esize].year); printf("Make: "); scanf("%s", inventory[*esize].make); printf("Model: "); scanf("%s", inventory[*esize].model); printf("Color: "); scanf("%s", inventory[*esize].color); printf("Condition: "); scanf("%s", inventory[*esize].condition); printf("Price: "); scanf("%lf", &inventory[*esize].price); *esize = *esize + 1; } void deleteAuto(AUTO inventory[], int *esize){ int index; int i, j; if(*esize == 0){ printf("No autos in inventory! "); return; }
//display inventory printf("List of autos in inventory: "); for(i = 0; i < *esize; i++){ printf("%d. %s %s %s %s %f ", i+1, inventory[i].year, inventory[i].make, inventory[i].model, inventory[i].color, inventory[i].price); }
//ask user to enter index to delete printf("Please enter the index of the auto you want to delete: "); scanf("%d", &index); index--; //index is 1-indexed, but our array is 0-indexed
//check if index is valid if(index < 0 || index > *esize-1){ printf("Invalid index! "); return; }
//delete auto for(j = index; j < *esize-1; j++){ inventory[j] = inventory[j+1]; }
//reduce size *esize = *esize-1;
printf("Auto deleted successfully! "); } void displayInventory(AUTO inventory[], int esize){ int i; printf("List of autos in inventory: "); for(i = 0; i < esize; i++){ printf("%d. %d %s %s %s %s %f ", i+1, inventory[i].year, inventory[i].make, inventory[i].model, inventory[i].color, inventory[i].condition, inventory[i].price); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
