1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <fstream>
using namespace std;
#include <cstring>
#include <cctype>
const int game_name = 35;
const int rating_of_game = 6;
const int platform = 30;
const int comments = 250;
const int summary = 400;
const int email = 35;
struct game_info
{
char name_of_game[game_name];
char game_rating[rating_of_game];
char game_platform[platform];
char game_comments[comments];
char summary_of_game[summary];
char email_address[email];
bool availability;
};
//prototypes
void readall(char prompt[], int max_size, char result[]);
void getting_game_info(game_info & game);
char display(game_info & game);
char ask_if_correct(char character);
void manage();
//bool load_games(char filename[], game_info & game, int & num);
//bool save_games(char filename[], int & num);
bool file_read(char filename[], game_info & game);
bool file_write(char filename[], game_info & game, int & num_games);
void getfilename(char filename[]);
void getfilename(char array[]);
int main(){
char filename[32];
int num_games = 0;
game_info game[20];
char character;
/*cout <<"please enter a file name including the extension, under 32 chars";
cin.get(filename, 34, '\n');
cin.ignore();*/
getfilename(filename);
//file_write(filename, *game);
file_read(filename, *game);
file_write(filename, *game, num_games);
manage();
}
void manage(){
ifstream file_in;
ofstream file_out;
int num_games;
//if(!load_games(filename, game_info & game, num_games))
//cout <<"we are starting from scratch, no games existing";
game_info game[20];
char character;
char response;
cout <<"would you like to enter a game? Enter eiher y or n";
cin >>response;
cin.ignore();
while(response == 'y')
{
for(int i = 0;i < 20 && response == 'y';++i ){
getting_game_info(game[i]);
cout <<endl<<endl <<endl;
cout <<"Would you like to add more games? Type either lowercase y or n" <<endl;
cin >> response;
cin.ignore();
}
}
}
void readall(const char prompt[], int max_size, char result[])
{
cout <<prompt << '\n';
cin.get(result, max_size, '\n');
cin.ignore(100, '\n');
}
void getting_game_info(game_info & game)
{
char character;
do {
readall("please enter name of game", game_name, game.name_of_game);
readall("please enter the rating of the game", rating_of_game, game.game_rating);
readall("please enter the game platform", platform, game.game_platform);
readall("please enter your comments about the game", comments, game.game_comments);
readall("please enter your summary of the game", summary, game.summary_of_game);
readall("please enter your email address", email, game.email_address);
cout <<"Is this information correct? Enter either y or n." << '\n';
cin >>character;
cin.ignore();
cout <<'\n';
} while((character == 'n') || (character == 'N'));
}
char display(game_info & game)
{
cout <<"The name of the game is: "<<game.name_of_game << endl;
cout << "The rating of the game is: "<<game.game_rating <<endl;
cout <<"The game platform is: "<<game.game_platform <<endl;
cout << "The game comments are: "<<game.game_comments <<endl;
cout << "Summary of the game: "<<game.summary_of_game <<endl;
cout << "Persons email address: "<<game.email_address <<endl;
}
bool file_read(char filename[], game_info & game)
{
ifstream file_in;
int num_games;
int i = num_games;
file_in.open(filename, ios::app);
if (!file_in) //not connected
{
cerr <<"problem";
return false;
}
//Connected to the file and ready to read
file_in.get(game.name_of_game, game_name,'|');
file_in.ignore(100,'|');
/*while(read && !read.eof()) //previous read is successful
{
read.get(games[num].description,SIZE_DESC,'|');
read.ignore(100,'|');
read.get(games[num].category, SIZE_CAT, '|');
read.ignore(100,'|');
read.get(games[num].rating, SIZE_RATING, '|');
read.ignore(100,'|');
read >> games[num].length;
read.ignore();
++num;
read.get(games[num].title,SIZE_TITLE,'|');
read.ignore(100,'|');
}*/
file_in.close();
return true;
}
bool file_write(char filename[], int &num_games)
{
game_info game[20];
bool success = true;
ofstream file_out;
file_out.open(filename); //rewrite over the entire file!
if (!file_out) //true -- connect; false -- problem
{
cout <<"CAN'T SAVE...\n\n";
success = false;
}
else
{
for(int i = 0; i < 20; ++i)
{
file_out << game[i].name_of_game <<"|";
}
file_out.close();
file_out.clear();
}
return success;
}
/*bool file_write(char filename[], game_info & game){
ofstream file_out;
file_out.open(filename,ios::app);
file_out<<game.name_of_game, game_name, '\n';
file_out.close();
file_out.clear();
}
*/
void getfilename(char array[])
{
readall("Please enter the name of a file limited to 31 characters: ", 31, array);
//strcat(array,".txt");
}
|