-
How to save a game
K well srry this is not in the games section i thought this had something to do with file streaming. K well i was wondering for my school project i need to make tycoon game and is there a way to creat a notepad or anything else that will save my game info into and then you can load it up.. I checked the tuts and could not find anything that helps..
Thanks guys
-
OOO and srry another question.. Umm is there a way to make like 999999999999 not crash my program. So something that will not allow user to inpute above int variable?
-
For your second post,
if(integer > 9999)
cout<<"This number is too high, enter lower num: ";
cin>>tycoonnumber
-
If you know how file i/o works, then saving is quite simple. Just output the necessary game variables into the file and seperate by spaces. Then to load the game, read the variables in accordingly. Just make sure you don't miss any information, otherwise you have bugs in your game. For example if you had a game where a guy gives you an item the first time you talk to him, and you didn't save that you talked to him already, then he would give you the item every time you loaded!
Anyway, just read up on File I/O, and you should figure out what to do. If you need more detailed information, though, just ask.
-
umm i think that will still make it crash.. any input like "fhaskgh" or "999999999" will crash my program. for the "hgjhgdg" i figured a way to fix it. which is
Code:
void int_trap( int& input)// error trap
{
do
{
if ( !cin.good() )//type
{
cout << "Invalid Input" << endl;
cout << "Please try again" << endl;
cin.clear ();
cin.ignore( 128, '\n' );
}
cin >> input;
}while ( !cin.good() );
};
But is there something similar to this for like numbers that intregers can't hold and that creat infinant loop thansk. If you do not get what i am saying i will say again, kool
-
umm i think that will still make it crash.. any input like "fhaskgh" or "999999999" will crash my program. for the "hgjhgdg" i figured a way to fix it. which is
Code:
void int_trap( int& input)// error trap
{
do
{
if ( !cin.good() )//type
{
cout << "Invalid Input" << endl;
cout << "Please try again" << endl;
cin.clear ();
cin.ignore( 128, '\n' );
}
cin >> input;
}while ( !cin.good() );
};
But is there something similar to this for like numbers that intregers can't hold and that creat infinant loop thansk. If you do not get what i am saying i will say again, kool
-
Grr never mind i am real not smart because i do not know how to use arrays and going into advanced crap all i know is as far as function cause i read it and it looks tough lol. um i guess i have to wait for teacher to teach ahhahaha. thanks
-
Cin is very volitile and there isn't much you can do to prevent the stream from failing. The best I can think of is reading every input into a string of characters, then checking that input to see if it's proper for an integer, then reading it into the integer. There might be some functions out there that checks input before it places it, but "cin" adds input into the stream as soon as the keyboard is pressed.
Just rest assured knowing that once you get passed all this basic programming, iostream is history for you.
-
here's a nice safe way to get input:
Code:
#include <iostream> //for console I/O
#include <cstdlib> //for atoi()
#include <string> //for strings
int main()
{
int num; //holds the number
std::string line; //holds the user input
std::cout<<"Enter some stuff (Type 'exit' to exit the program)\n";
for(;;)
{
std::cout<<"> ";
getline(std::cin,line,'\n'); //take in input as a string
if(line=="exit") //if the command to exit is given
{
break; //follow it
}
num=atoi(line.c_str()); //if not, convert the string to an int
std::cout<<num<<std::endl; //and do something with it
}
return 0;
}
notice that doesn't do anything to validate that input... so if they enter their name, it's going to return 0, and if they enter 99999999999999999999999999, it's going to return 2147483647. Just look at it this way: It's better than watching your program launch itself into an infinite loop.