Thread: Inputing Character Data

  1. #1
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12

    Inputing Character Data

    I have an RPG game and I want to be able to load a character's stats and map position into the game from a text file.

    Paladin

    HP: 4500
    MP: 800

    Strength: 200
    Vitality: 300
    Agility: 100
    Magic: 80

    Defense = 600
    Damage = 400

    Magic Damage: 160

    Stat Points: 5

    Location: 5, 5

    Any help is appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what's your question?

    My question is why are you attempting something as complicated as writing an RPG when you haven't mastered the basics of parsing a text file for information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I've done something similar recently.
    This is how I did it...

    1. Read each line as a string into a vector.

    This is easy. This can be done using getline(cin,string), then vector.push_back().

    2. Read the first word of each line.

    This isn't difficult either. Just check if the first word is something you recognise.

    Code:
    stringstream ss( line );  // include <sstream>
    string firstWord ;
    if ( (ss >> firstWord) ) {
      // see if word is something you recognise
    }
    else {
      // there are no words on this line; ignore
    }
    3. If the first word is something you recognise, read the rest of the line into values.

    Code:
    if ( firstWord == "HP" ) {
      int hp ;
      if ( ss >> hp ) {
        // we got the health points
      }
      else {
        // error
      }
    My suggestion is not to use =, : or any other characters to delimit strings. Just use spaces.

    Code:
    HP  400
    MP 300
    Stat Points 5
    Location 2 2
    Hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM