Thread: How to save a game

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    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

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    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?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    For your second post,

    if(integer > 9999)
    cout<<"This number is too high, enter lower num: ";
    cin>>tycoonnumber

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    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

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    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

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    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

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM