Console Text RPG- File I/O and Inventory

This is a discussion on Console Text RPG- File I/O and Inventory within the Game Programming forums, part of the General Programming Boards category; I am attempting to make a console rpg to get some practice before I move any farther in the book ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Console Text RPG- File I/O and Inventory

    I am attempting to make a console rpg to get some practice before I move any farther in the book I have. I want to know how I can store variables in files and also how I could use an array to store items without using a switch or quite a few if then statements. I've thought about using a class for items.
    I dont know how I could use multiple variables in one file because this doesn't seem to work.


    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    
    void main()
    {
      int x = 5;
      int y = 6;	
      ofstream a_file("c:\jonsexample.txt");
      a_file<<x;
      a_file<<y;
      a_file.close();
      
      y=0;
      x=0; //to change x and y value
      
      ifstream b_file("c:\jonsexample.txt");
      b_file>>x;
      b_file>>y;
      cout<<x<<endl;
      cout<<y<<endl;
      b_file.close();
               
    }
    So my questions are these.

    1. What can I do to write a function for an inventory screen and output the item name without using if-then statements or a switch statement (maybe I have to use these) by possibly using an array?

    2. How can I write a load/save function by writing an array to a file and then loading the array for character information?

    Thank You for your comments.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    73
    One maybe not-so-helpful suggestion I have for you is this: Use code that is meets the current standard.

    Behold:

    1.
    #include <iostream.h>

    should be

    #include <iostream>

    2.
    #include <fstream.h>

    should be

    #include <fstream>

    3.
    Place the following before main:

    using namespace std;

    4. Declare main as type int (main can never be void)

    If you are using a compiler that does not support the current C++ standard, I highly suggest you obtain a newer version. Happy coding! ^_^

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    and for more helpful information:

    first off,

    ofstream a_file("c:\jonsexample.txt");

    needs to be

    ofstream a_file("c:\\jonsexample.txt");

    because the backslash is used to signify escape sequences. you need two backslashes within a string to make an actual backslash.

    secondly, using the fstream, the >> operator retrieves tokens, which means it reads in characters until it hits whitespace. what you need to do is output a space or a newline after each variable, so that when you read them in you will only get one variable at a time. thats why, when you read it in your way, you get

    56
    0

    because its reading in the entire token for the first variable and then it doesnt have anything else to read so the second variable stays a 0.
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    195
    Originally posted by Zoalord
    One maybe not-so-helpful suggestion I have for you is this: Use code that is meets the current standard.

    Behold:

    1.
    #include <iostream.h>

    should be

    #include <iostream>

    2.
    #include <fstream.h>

    should be

    #include <fstream>

    3.
    Place the following before main:

    using namespace std;

    4. Declare main as type int (main can never be void)

    If you are using a compiler that does not support the current C++ standard, I highly suggest you obtain a newer version. Happy coding! ^_^

    What advantages does the namespace std thing have over #include <iostream.h>? Im just wondering

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    So it should look like this?


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      int x = 5;
      int y = 6;	
      ofstream a_file("c:\\jonsexample.txt");
      a_file<<x<<endl;
      a_file<<y<<endl;
      a_file.close();
      
      y=0;
      x=0; //to change x and y value
      
      ifstream b_file("c:\\jonsexample.txt");
      b_file>>x;
      b_file>>y;
      cout<<x<<endl;
      cout<<y<<endl;
      b_file.close();
               
      return 0;
    }
    I compiled an ran that code, and it seems to work. By the way I'm using Visual C++ 6. Does anyone have an idea on how to do the item inventory?

    Thank you for your comments.
    Last edited by Jontay; 11-08-2003 at 06:25 PM.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    73
    Indeed! It looks like you have that standard form down. I'm sorry that I didn't post more helpful suggestions. Lack of standard code was the first thing I noticed. =P

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I have another question. How do I delete everything in a file so I can write to it again?

  8. #8
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    the default mode (i belive) for opening a text file with the ofstream is to erase the file if its already there or create a new one if its not.

    so essentially you are already deleting everything in the file.

    there are other modes that you can get information about by looking through the help about ifstream and ofstream, such as appending to the end of the file, opening in binary mode instead of text mode, etc...
    I came up with a cool phrase to put down here, but i forgot it...

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I think I'm jsut going to do the item system with a bunch of if thens and then if I come across anything better I'll change it.
    I have one final question about my compiler.

    Is it possible to make a number the lines in the main screen of MSVC++? If so, How?

    Thank you.

  10. #10
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    im using VC++.NET, so if youre using 6.0 im not sure if its the same, but this is how i do it:

    go to tools->options then click on the "text editor" folder, and then the "C/C++" folder. you should have some check boxes on the right, and the third one from the bottom is "line numbers"

    again, thats .NET, its probably a little different in 6.0.
    I came up with a cool phrase to put down here, but i forgot it...

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I dont see it there but thanks for trying to help.

  12. #12
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    i found it by going to the help and typing in "line numbers"... you may want to try that
    I came up with a cool phrase to put down here, but i forgot it...

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    17
    I tried that, but it only talks about line numbers in the .obj file not in the compiler.

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I'm not sure of your setup but normally I make inventory items have a room number of -1 or 9999. If you dont want to do this simply change the room number of the item to 0 and then add it to the inventory array or linked list.

  15. #15
    Registered User
    Join Date
    May 2002
    Posts
    31
    4. Declare main as type int (main can never be void)

    Hmm thats odd, see i remember... no that couldnt be right. Yeah actually it is! I compiled a program where main was declared void and it worked! OMG!

    Yeah... main SHOULD be declared int(or char or Hotdog or whatever you fancy besides void) but that does not mean that you can't do it.

    And if you wanna make some suggestion about changing the main function... id go with:

    int main(int argc, char *argv[])

    That way you could like... uhh.. get command line input and good stuff! Yeah! Way to make that int main() actually worthwhile!

    Stumbled across this just now
    Last edited by SinAmerica; 11-22-2003 at 06:35 PM.
    I never lie... except for right now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  2. text rpg help
    By xxwerdxx in forum Game Programming
    Replies: 1
    Last Post: 11-26-2005, 08:16 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Reading a line from a text file Please help
    By Blizzarddog in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 01:35 PM
  5. Check out My Text Rpg Game
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 11:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21