Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Video Games (with File I/O Write a program that stores the following data about

ID: 3594825 • Letter: V

Question

Video Games (with File I/O Write a program that stores the following data about a video game in a structure: Field Name title genre rank time Description string (name of game) enum (type of game) int float/double This program will NOT need an array of structures. Instead, store the data in a file named games.dat". Your program will use a single structure variable to read/write a record as needed. Your program should have a menu that allows the user to perform the tasks shown below. 1. Add a game. 2. Print the information for all games, in table form (does not need to be sorted). Calculate and display the total time spent playing all video games. 4. Calculate and print the average rank for all games. 5. Edit the data Your program should allow the user to make selections until they choose a 6th option "Exit Program" which will end the program. Your program should be modular. Use a separate function for each option stated above. Your output should be well-organized, neat, and easy to read.

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

enum gameType {A,B};/*change according to your game*/

struct game

{

char title[30];

enum gameType genre;

int rank;

float time;

};

void addGame(FILE *fp, struct game *value)

{

fwrite(value, sizeof(struct game), 1, fp);

}

void printAll(FILE *fp)

{

struct game temp;

while(fread(&temp, sizeof(struct game), 1, fp))

{

printf("Title = %s Genre = %d Time spent = %f Rank = %d ", temp.title, temp.genre, temp.rank, temp.time);

}

}

float totalTime(FILE *fp)

{

struct game temp;

float total = 0;

while(fread(&temp, sizeof(struct game), 1, fp))

{

total += temp.time;

}

return total;

}

int averageRank(FILE *fp)

{

struct game temp;

int sum = 0,count = 0;

while(fread(&temp, sizeof(struct game), 1, fp))

{

sum += temp.rank;

count++;

}

return sum/count;

}

void edit(FILE *fp, char tit[30])

{

struct game temp;

fpos_t position;

while(fread(&temp, sizeof(struct game), 1, fp))

{

if(!strcmp(temp.title, tit))

{

fgetpos(fp, &position);

printf("Current values of this game Title = %s Genre = %d Time spent = %f Rank = %d ", temp.title, temp.genre, temp.rank, temp.time);

printf("New values (Note: retype all values irrespective of change) ");

scanf("%s%d%d%f", &temp.title, &temp.genre, &temp.rank, &temp.time);

const fpos_t pos1 = position-sizeof(struct game);

fsetpos(fp, &pos1);

fwrite(&temp, sizeof(struct game), 1, fp);

}

}

}

int main()

{

FILE *fp;

int choice;

struct game temp;

char tempTitle[30];

do

{

printf("1. Add a game 2. Print all games 3. Total time spent on all games 4. Average rank in all games 5. Edit a game 6. Exit ");

scanf("%d",&choice);

fp=fopen("games.dat", "w+");

switch(choice)

{

case 1:

printf("Enter Title Type Rank Time ");

scanf("%s%d%d%f", &temp.title, &temp.genre, &temp.rank, &temp.time);

addGame(fp, &temp);

break;

case 2:

printAll(fp);

break;

case 3:

printf("Total time spent is: %f ", totalTime(fp));

break;

case 4:

printf("Average rank is: %d ", averageRank(fp));

break;

case 5:

printf("Which game to edit? ");

scanf("%s", tempTitle);

edit(fp,tempTitle);

break;

fclose(fp);

}

}while(choice!=6);

fclose(fp);

return 0;

}

/*there may be some situations which I have not handled due to time constraint, but basic functionalities are working fine on my machine, so please comment if you want something more*/