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

Write a program C++ that stores the following data about a video game in a struc

ID: 3602765 • Letter: W

Question

Write a program C++ that stores the following data about a video game in a structure: Field Name Description title string (name of game) genre enum (type of game) rank int time 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). 3. 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 (Extra Credit). 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<iostream>
#include<string>

using namespace std;

enum type { FPS, MOBA, RolePlay };

struct game{
   string title;
   type gener;
   int rank;
   double time;
};

double averageTime(game data[]){

    double sum = 0;
    for (int i = 0; i<5; i++)
        sum = sum + data[i].time;
    return sum/5;
}

double totalTime(game data[]){

    double sum = 0;
    for (int i = 0; i<5; i++)
        sum = sum + data[i].time;
    return sum;
}

void search(game data[], string str){

    double sum = 0;
    size_t found;
    for (int i = 0; i<5; i++) {
       found = data[i].title.find(str);   
       if (found != string::npos){
          cout << "Title: " << data[i].title << endl;
          cout << "Gener: " << data[i].gener << endl;
          cout << "Rank: " << data[i].rank << endl;
          cout << "Time: " <<data[i].time << endl;
       }
    }
    cout << "Not Found ";
    return;
}

void display(game data[]){

    double sum = 0;
    for (int i = 0; i<5; i++){
          cout << "Title: " << data[i].title << endl;
          cout << "Gener: " << data[i].gener << endl;
          cout << "Rank: " << data[i].rank << endl;
          cout << "Time: " <<data[i].time << endl;
       }

}

void sort(game data[]){

    game temp;
    for (int i = 0; i<5; i++){
       for (int j = i; j<5; j++){
           if (data[i].rank < data[j].rank){
              temp.title = data[i].title;
              temp.gener = data[i].gener;
              temp.rank = data[i].rank;
              temp.time = data[i].time;
              data[i].title = data[j].title;
              data[i].gener = data[j].gener;
              data[i].rank = data[j].rank;
              data[i].time = data[j].time;
              data[j].title = temp.title;
              data[j].gener = temp.gener;
              data[j].rank = temp.rank;
              data[j].time = temp.time;
           }
       }

    }

}


int main(){


   game data[5];
   int n;
   string title;

   data[0].title = "League of Legends";
   data[0].gener = MOBA;
   data[0].rank = 3;
   data[0].time = 9.5;

   data[1].title = "Team Fortress 2";
   data[1].gener = FPS;
   data[1].rank = 5;
   data[1].time = 16.75;

   data[2].title = "Hearth Stone";
   data[2].gener = RolePlay;
   data[2].rank = 2;
   data[2].time = 4.25;

   data[3].title = "Halo";
   data[3].gener = FPS;
   data[3].rank = 1;
   data[3].time = 0.75;

   data[4].title = "DOTA 2";
   data[4].gener = MOBA;
   data[4].rank = 2;
   data[4].time = 6.75;

   sort(data);

   cout << "1.Print all games ";
   cout << "2.Search a game ";
   cout << "3.Print time for all games ";
   cout << "4.Print average rank ";

   cout << " Enter your choice :";
   cin >> n;

   switch (n) {
       case 1 : display(data);
                break;
       case 2 : cout << "Enter the title :";
                cin >> title;
                search(data,title);
                break;
       case 3 : cout << "Total Time : " << totalTime(data) << endl;
                break;
       case 4 : cout << "Average Time : " << averageTime(data) << endl;
                break;
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote