#include #include #include #include "provided.h" using std::ifstream; /*************************************************************************** * * This provided code uses C-style strings. * Later...

C++ Homework


#include #include #include #include "provided.h" using std::ifstream; /*************************************************************************** * * This provided code uses C-style strings. * Later this semester the students will be learning and using C++ Strings * instead. * It is important to know about C-style strings because they are used * in a lot of code. Unless you have a specific, compelling reason to use C-style * strings, once you learn C++ Strings, use std::string (defined in the header) instead. * std::string is easier, safer, and more flexible. * *************************************************************************** */ /* Function readGameTitles * fileName: c-style String, i.e., array of chars ended by ‘\0 * ngames: number of games cousins are bringing to the house * gameTitles: 2d-array where row i stores the title for game i * Return value: -1 if error opening the file, otherwise 0. */ int readGameTitles (char fileName[], int ngames, char gameTitles[MAX_NB_GAMES][MAX_TITLE_SIZE]) { ifstream fin; fin.open(fileName); if (!fin) { // error in opening the file return -1; } for (int i = 0; i < ngames;="" i++)="" {="" strcpy(gametitles[i],="" "unknown="" title");="" }="" int="" gameid;="" char="" title[max_title_size];="" while="" (!fin.eof())="" {="" while="" not="" at="" the="" end="" of="" the="" file="" fin="">> gameid; if (fin.good()) { // no error reading integer game // read title as the rest of the line if (gameid >= 0 && gameid < max_nb_games)="" {="" valid="" game="" consume="" space;="" input="" is="" guaranteed="" to="" have="" only="" space="" before="" title="" char="" space;="" fin="">> std::noskipws >> space; fin.getline(gameTitles[gameid], MAX_TITLE_SIZE); } else { // ignoring invalid gameid, just consume title fin.getline(title, MAX_TITLE_SIZE); } } else { // read the rest of the line and ignore it fin.clear(); fin.getline(title, MAX_TITLE_SIZE); } } return 0; } /* Function printGamesPlayedInVacation * start: first day of the vacation (1 <= start="">< 365)="" *="" duration:="" number="" of="" vacation="" days="" *="" plan:="" array="" with="" 366="" positions="" with="" the="" sequence="" of="" games="" *="" to="" be="" played="" over="" the="" year.="" in="" other="" words,="" *="" plan[k]="" indicates="" the="" game="" planned="" for="" day="" k="" (1=""><= k=""><= 365)="" *="" gametitles:="" array="" with="" game="" titles="" *="" ngames:="" number="" of="" games="" *="" return="" value:="" none.="" */="" void="" printgamesplayedinvacation(int="" start,="" int="" duration,="" int="" plan[],="" char="" gametitles[max_nb_games][max_title_size],="" int="" ngames)="" {="" bool="" title_included[max_nb_games];="" for="" (int="" i="0;" i="">< ngames;="" i++)="" {="" title_included[i]="false;" }="" int="" gameid;="" for="" (int="" i="start;" i="">< start="" +="" duration="" &&="" i=""><= 365;="" i++)="" {="" gameid="plan[i];" if="" (!title_included[gameid])="" {="" std::cout="">< gametitles[gameid]="">< std::endl; title_included[gameid] = true; } } } const int max_nb_games = 200; const int max_title_size = 128; int readgametitles (char filename[], int ngames, char gametitles[max_nb_games][max_title_size]); void printgamesplayedinvacation(int start, int duration, int plan[], char gametitles[max_nb_games][max_title_size], int ngames); std::endl;="" title_included[gameid]="true;" }="" }="" }="" const="" int="" max_nb_games="200;" const="" int="" max_title_size="128;" int="" readgametitles="" (char="" filename[],="" int="" ngames,="" char="" gametitles[max_nb_games][max_title_size]);="" void="" printgamesplayedinvacation(int="" start,="" int="" duration,="" int="" plan[],="" char="" gametitles[max_nb_games][max_title_size],="" int="">
Oct 07, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here