Followers 0

# c++ quiz project (beginner)

## 6 posts in this topic

ok so I have been on other forums, googled, read on most c++ websites and I can't understand how to get my code to save the data to a file which can be selected to loaded once in the game.

I have two ofstreams and ifstreams, one to save and load the highscore of the game the other supposed to save the progress and load it when you feel like it. The thing is I can't get the progress or game to save the data I believe I save it in wrong format and so on but I don't know how to fix that at the moment so I would be really gratefull if someone could help me out here.

I know some of the basics but not all still things I need to get explained but the major problem right now is to get it to save in any format except .txt (was thinking to save it as a .exe first but that didn't work out well so now I'm thinking of something like .dat or .dll or such)

Once I get a hang of how to use return properly i will use that and "call" on the things I need to main and not write it all in there but for time being I have done that so it looks messy.

Am using switch-case and in those i have if-else with new switch-cases etc to make the game flow at the moment

ooh by the way is there a way to remove this post or move it to the beginner c++ section in the for beginner? seems like i cant find the delete button for my topic sry wrong section :(

Edited by TrickJay
0

0

##### Share on other sites
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
struct highscore{
int svar;
};

struct game{
int fraga;
int val;
};

int main(){
ofstream game;
ofstream highscore;
bool fail = true;
int lost, fraga[10], svar = 0; // these are for the Start option
char val;

game.open("game.txt", ios::app);
if (!game){
cout << "file could not open";
exit (1);
}

do{
cout << " Menu" << endl;
cout << " 1. Start" << endl;
cout << " 2. Load" << endl;
cout << " 3. Credits" << endl;
cin >> val;
switch(val){

case '1':

highscore.open("Highscore.txt", ios::app);
if (!highscore){
cout << "file could not open";
exit (1);
}
cout << "WELCOME TO THE CISCO QUIZ  GOOD LUCK" << endl;
cout << "how many layers are there in the OSI model?" << endl;
cout << "1. 4" << endl;
cout << "2. 5" << endl;
cout << "3. 7" << endl;
cin >> fraga;
switch(fraga){
case 1:

cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 2:

cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 3:
svar = svar + 5;
break;
default:
cout << "error" << endl;
}

if(svar == 5){
cout << "what is the full name of OSPF?" << endl;
cout << "1. Open Shortest Path First" << endl;
cout << "2. Online Shooting Pro Fishing" << endl;
cout << "3. On Short Path First" << endl;
cin >> fraga;
switch(fraga){

case 1:
svar = svar + 5;
break;

case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
}
}
if(svar == 10){
cout << "What does TTL mean?" << endl;
cout << "1. Twin Twitched Life" << endl;
cout << "2. Two Time Lives" << endl;
cout << "3. Time To Live" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"<< endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 3:
svar = svar + 10;
break;
}
}
if(svar == 20){
cout << "What is the first command you enter on a Cisco router?" << endl;
cout << " 1. enable"<< endl;
cout << " 2. disable" << endl;
cout << " 3. enter" << endl;
cin >> fraga;

switch(fraga){
case 1:
svar = svar + 5;
break;

case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
}
}
if(svar == 25){
cout << "which protocol is the most prefered?" << endl;
cout << " 1. RIP" << endl;
cout << " 2. EGRP" << endl;
cout << " 3. EIGRP" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;

case 3:
svar = svar + 15;
cout << "You have completed the game press 1 to exit or if you wish to do it again or check highscores press 2" << endl;
cin >> lost;
if (lost != 1 ){
fail = false;
}
break;
}
}
highscore <<"Score: " << svar << '\n';
highscore.close();
game << fraga << val;
game.close();
cin.ignore(10,'\n');

cin.get();

case '2':
cout << "type 1 to load game or type 2 to load highscores" << endl;
{
{
cout << line2 << endl;
}
cout << "do you wish to return to the menu press 1 else press any number" << endl;
cin >> tillbaka;
if (tillbaka != 1){
fail = false;
}
game.close();
}
}
{
{
cout << line << endl;
}
cout << "do you wish to return to the menu press 1 else press any number" << endl;
cin >> tillbaka;
if (tillbaka != 1){
fail = false;
}

highscore.close();

}
}
cin.ignore(10,'\n');

cin.get();
break;

}
}while(fail);

}



so here's the code open to all suggestions

NOTE: have also tried to save the game.txt as a .dat didnt help much tho :S

Edited by TrickJay
0

##### Share on other sites

ok so I have been on other forums, googled, read on most c++ websites and I can't understand how to get my code to save the data to a file which can be selected to loaded once in the game.

Well what is it you're trying to accomplish? There's nothing really mystical about saving and loading files, using streams as an example: you basically just insert or grab variables to a stream to write their value to the file or read the next value into the variable.

I have two ofstreams and ifstreams, one to save and load the highscore of the game the other supposed to save the progress and load it when you feel like it. The thing is I can't get the progress or game to save the data I believe I save it in wrong format and so on but I don't know how to fix that at the moment so I would be really gratefull if someone could help me out here.

I looked over your code a bit and I'm quite frankly not sure what it is you're trying to -do- from what I can tell it just opens a file, reads everything out of it and prints it to the console. That's not really going to change program flow at all.

I know some of the basics but not all still things I need to get explained but the major problem right now is to get it to save in any format except .txt (was thinking to save it as a .exe first but that didn't work out well so now I'm thinking of something like .dat or .dll or such)

Extension is just a naming scheme, used to let programs "assume" what kind of data a file contains, or for the operating system to use(i.e. .exe files are executable code.) All files are binary but most people differentiate them as text or binary, text is just binary encoded to be read back as text(useful for opening the file to edit it easily, or being able to read it into strings and other business.)

Unless your game relies on being performant or you have some good reason to use straight binary writing/reading, text files would likely suit your purposes fine.

The extension doesn't really "matter" that much if you know which file you're loading, since it's just a label, but you really shouldn't label files with common extensions like .exe or .dll if they aren't in fact either of those file types.

Once I get a hang of how to use return properly i will use that and "call" on the things I need to main and not write it all in there but for time being I have done that so it looks messy.

Your code is very hard to follow honestly, you probably should read up on functions and scope and all that basic stuff before you worry about reading and writing files. Code with good flow is much easier to understand and to correct then a giant set of switches.

That said when it comes down to it I just don't even understand what your goal is, what are you trying to save and load? Edited by Satharis
0

##### Share on other sites

ok so i've done some major changes in my code and i hope this one is better i have loads of comments so that might make it difficult to read but here it is:

http://ideone.com/mKKxuy

Edited by TrickJay
0

##### Share on other sites

ok so i've done some major changes in my code and i hope this one is better i have loads of comments so that might make it difficult to read but here it is:

http://ideone.com/mKKxuy

Ignoring the rest of the format of your code, to me it would be the most simple to either have a "score" global variable or to pass said variable around to your question functions, and just increase it when they get an answer right.

You really should only be loading the highscore at the start of the program and saving it when they complete the quiz, no?
0

##### Share on other sites

no i want it to update the score all the time im working on that and once you fail/complete the game the score gets saved so it would be a list obv. i will post an update of my code once i've worked a bit more on it i should be fully done in around 4-5 hours hopefully less ^^ and im planning on making it to a return value from each question so that one return points; or such will get "updated" after each question

ok so i dont understand how to make the return points into main i have tried a little which can be seen here:

http://ideone.com/dk2IYx

anyone mind explaining to me how to get the return points into main so i only need to save into .txt once and in main only instead of everywhere?

Edited by TrickJay
0

## Create an account

Register a new account