Hello, I have a program that records inventory for a video game store (stores ti
ID: 3581258 • Letter: H
Question
Hello,
I have a program that records inventory for a video game store (stores title, quantity, and genre) and is supposed to display the results at the end of the program. However, my results are printing out very disorganized, and I am having trouble getting the information to be in an organized layout. If someone could help me fix the code up so that the results print in a nice, listed format that would be spectacular. Also, everything else is fine and does not need to be touched, it is literally the part at the end after the (====) that needs to be organized. Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum GENRE {Action,RPG,Simulation,Strategy,Sports};
struct gameinfo{
char name[100];
int quantity;
enum GENRE sam;
};
void get_game_info(struct gameinfo *current);
void display_game_info(struct gameinfo gamesinfo[], int n);
int main()
{
printf("How many gamess are there in inventory? ");
int n;
scanf("%d",&n);
struct gameinfo gamesinfo [n];
int k=0;
while(k<n)
{
get_game_info(&gamesinfo[k]);
k++;
}
display_game_info(gamesinfo,n);
return 0;
}
void get_game_info(struct gameinfo *current)
{
printf(" Title of game (maximum of 15 characters): ");
fflush(stdin);
fgets(current->name,100,stdin);
int length;
for(length=0; current->name[length]!='' ; length++)
{
}
if(length>15)
{
printf("Title will be truncated ");
}
printf("Number Sold: ");
scanf("%d",¤t->quantity);
printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
int genrevalue;
scanf("%d",&genrevalue);
while(genrevalue > sizeof(current->sam))
{
printf("Genre not recognized, please reenter ");
printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
scanf("%d",&genrevalue);
}
current->sam=genrevalue;
}
void display_game_info(struct gameinfo gamesinfo[], int n)
{
int i;
printf(" Title ");
printf("Quantity Sold ");
printf(" Genre ");
printf("===== ");
printf("============= ");
printf("===== ");
for(i=0;i<n;i++)
{
printf("%.15s ",gamesinfo[i].name);
printf("%d ",gamesinfo[i].quantity);
switch(gamesinfo[i].sam)
{
case 0:
printf("Action");
break;
case 1:
printf("RPG");
break;
case 2:
printf("Simulation");
break;
case 3:
printf("Strategy");
break;
case 4:
printf("Sports");
break;
}
printf(" ");
}
}
Explanation / Answer
//check the full listing. Modifications are commented
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum GENRE {Action,RPG,Simulation,Strategy,Sports};
struct gameinfo{
char name[100];
int quantity;
enum GENRE sam;
};
void get_game_info(struct gameinfo *current);
void display_game_info(struct gameinfo gamesinfo[], int n);
int main()
{
printf("How many gamess are there in inventory? ");
int n;
scanf("%d",&n);
struct gameinfo gamesinfo[2];
int k=0;
while(k<n)
{
get_game_info(&gamesinfo[k]);
k++;
}
display_game_info(gamesinfo,n);
return 0;
}
void get_game_info(struct gameinfo *current)
{
printf(" Title of game (maximum of 15 characters): ");
fflush(stdin);
fgets(current->name,100,stdin);
int length;
// this part is modified to remove newline
for(length=0; current->name[length]!=' ' ; length++)
{
}
current->name[length]=' '; // this is necessary
if(length>15)
{
printf("Title will be truncated ");
}
printf("Number Sold: ");
scanf("%d",¤t->quantity);
printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
int genrevalue;
scanf("%d",&genrevalue);
while(genrevalue > sizeof(current->sam))
{
printf("Genre not recognized, please reenter ");
printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
scanf("%d",&genrevalue);
}
current->sam=genrevalue;
}
void display_game_info(struct gameinfo gamesinfo[], int n)
{
int i;
printf(" Title ");
printf("Quantity Sold ");
printf(" Genre ");
printf("===== ");
printf("============= ");
printf("===== ");
// adjusted tabs and spaces
for(i=0;i<n;i++)
{
printf("%.15s ",gamesinfo[i].name);
printf("%d ",gamesinfo[i].quantity);
switch(gamesinfo[i].sam)
{
case 0:
printf("Action");
break;
case 1:
printf("RPG");
break;
case 2:
printf("Simulation");
break;
case 3:
printf("Strategy");
break;
case 4:
printf("Sports");
break;
}
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.