Instructions
Requirements and Specifications
Source Code
#include
#include
// Structure that stores information about model houses
struct Game {
char name[100];
int minutes;
int points;
};
// identifiers
struct Game getLongestGame(struct Game games[1000], int n_games);
struct Game getHigherScoreGame(struct Game games[1000], int n_games);
int main() {
// char array to store file names
char file_name[100];
// array fo struct to store games
struct Game games[1000];
// Ask for file name
std::cout << "Enter the name of the file you wish to analyze: ";
std::cin.getline(file_name, 100, '\n');
// variable to count the number of games read
int n_games = 0;
// read file
FILE* myFile;
myFile = fopen(file_name, "r");
// check that file opened correctly
if(myFile == NULL) {
printf("File %s could not be opened.", file_name);
return 1;
}
char name[100];
char line[100];
int minutes, points;
bool isInt = false;
int line_id = 0;
while(fscanf(myFile, "%s %i %i", &name, &minutes, &points) == 3)
{
// Create game
struct Game game;
memcpy(&game.name, &name, 100);
game.minutes = minutes;
game.points = points;
games[n_games] = game;
n_games++;
}
fclose(myFile);
// get longest game
struct Game longest_game = getLongestGame(games, n_games);
// Get game with higher score
struct Game higher_score_game = getHigherScoreGame(games, n_games);
std::cout << "Player with the highest minutes is: " << longest_game.name << std::endl;
std::cout << "Player with the highest point total is: " << higher_score_game.name << std::endl;
std::cout << std::endl << std::endl;
system("pause");
return 0;
}
struct Game getLongestGame(struct Game games[1000], int n_games)
{
// get game with the longest duration
Game ret_game = games[0];
for(int i = 1; i < n_games; i++)
{
if(games[i].minutes > ret_game.minutes) {
ret_game = games[i];
}
}
return ret_game;
}
struct Game getHigherScoreGame(struct Game games[1000], int n_games)
{
// get game with the higher score
Game ret_game = games[0];
for(int i = 1; i < n_games; i++)
{
if(games[i].points > ret_game.points) {
ret_game = games[i];
}
}
return ret_game;
}