Thread: Loading Data

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    4
    Rep Power
    0

    Loading Data


    Loading data
    I have the program output the data that i want to a .txt file. However, I cannot get the program to load(correctly). You see, it saves the data, and then if i wanted to load the program, it loads the users data, but then runs the program from the start, and resets the data. How can i get it to load from the point in the game where they are?

    This is the code for my program...



    // Game file: rpg.cpp

    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include "apstring.h"
    #include "enemy.h"
    #include <fstream.h>
    #include <assert.h>
    #include <stdio.h>

    double response;
    int a, x, data, data1;
    int y = 2;
    int x1, x2, x3, x4, x5;
    apstring name, weapon;


    // Declare Player Stats

    int health = 25;
    int health2 = 25;
    int speed = 18;
    int agility = 19;
    int strength = 15;
    int intelligence = 17;
    int defense = 23;
    int dexterity = 20;
    int skill = 17;
    int EXP = 1500;
    int level = 5;

    int main()

    {
    ifstream infile;
    ofstream outfile;


    cout << "________________________________________" << endl;
    cout << "[ ]" << endl;
    cout << "[ Things you should know ]" << endl;
    cout << "----------------------------------------" << endl;
    cout << endl;
    cout << endl;
    cout << "If you read a line of text, and the program haults, it is waiting for a response from you" << endl;
    cout << "that says it is ok to advance in the game. We will set this response to any # key." << endl;
    cout << "Hitting a character, will cause the game to advance past places where your response" << endl;
    cout << "is NECESSARY." << endl;
    cout << endl;
    /*cout << "Do you wish to load a game? (Y/N)" << endl;
    / cin >> response;

    if (response == 'Y' || 'y')
    {
    ifstream infile;
    infile.open("savefile.txt");
    assert (! infile.fail());
    infile >> name >> level >> EXP >> strength >> skill >> speed >> agility >> defense >> dexterity >> intelligence >> weapon;
    cout << endl;
    infile.close();
    assert(! infile.fail());
    }
    else
    { */
    cout << "Before you embark on your adventure, what is your name?" << endl;
    getline(cin, name);
    cout << endl;
    cout << endl;

    // Declare the players stats

    cout << setw(40) << "YOUR CURRENT STATS" << setw(60) << endl;
    cout << setw(40) << "__________________" << setw(60) << endl;
    cout << "Health" << setw(34) << health << endl;
    cout << "Speed" << setw(35) << speed << endl;
    cout << "Agility" << setw(33) << agility << endl;
    cout << "Strength" << setw(32) << strength << endl;
    cout << "Intelligence" << setw(28) << intelligence << endl;
    cout << "Defense" << setw(33) << defense << endl;
    cout << "Dexterity" << setw(31) << dexterity << endl;
    cout << "Skill" << setw(35) << skill << endl;
    cout << endl;
    cout << endl;
    cout << "What is going on?? How can this happen to me?" << endl;
    cin >> a;
    cout << "Could the prophecy be coming true?" << endl;
    cin >> a;
    cout << "Maybe Darien will have some answers." << endl;
    cin >> a;
    cout << "But I promised my mom I would pick up some milk from the store." << endl;
    cin >> a;
    cout << "What should I do?" << endl;
    cout << " ______________________________________ \n"
    << "|{--Go to Darien's--} (Enter [ 1 ]) |\n"
    << "|{--Pick up Groceries--} (Enter[ 2 ])|\n"
    << " -------------------------------------- \n";
    cin >> x;
    cout << endl;
    if (x < y)
    {
    cout << "The groceries can wait until later. I have to figure out what is going on!" << endl;
    }
    else
    {
    cout << "Darien will have to wait. I have to keep the promise to my mom." << endl;
    }
    cout << endl;
    cout << "-- Having made his decision " << name << " began his journey. --" << endl;
    cout << endl;
    if (x == 1)
    {
    cout << "-- " << name << " approached Darien's house. As he was reaching the peak of the" << endl;
    cout << "hill, he stopped to think. --" << endl;
    }
    else
    {
    cout << "-- " << name << " drew closer to the grocery store..." << endl;
    cout << "Suddenly 2 Grops attacked " << name << "!!!--" << endl;
    cout << "___________________________" << endl;
    }
    cout << "-- " << name << " entered the fight!! --" << endl;
    do
    {

    void beginFight( int x1, int x2, int x3, int x4, int x5, int data, int data1);
    {
    time_t seconds;

    time(&seconds);
    srand((unsigned int) seconds);
    data1 = rand() % (gropst - defense - agility + gropi - dexterity + gropsk);
    cout << endl;
    cout << "2 Grops attack " << name << ", 1 hits and deals " << data1 << " damage!" << endl;
    cin >> a;
    health2 = health2 - data1;
    if (health <= 0)
    {
    cout << "You are dead..." << endl;
    void battleEnd();
    {
    cout << "The grop attacked you with amazing force, knocking you to the ground." << endl;
    cout << "You try to defend yourself, but the grop proves to strong." << endl;
    cout << "With it's powerful tentacles, the grop slowly squeezes the life out of you." << endl;
    }
    return 0;
    }
    else
    {
    cout << "Your health is currently " << health2 << "." << endl;
    }
    data = rand() % (strength - gropd - gropa + intelligence - gropde + skill);
    groph = groph - data;
    cout << "You attack 2 grops and deal " << data << " damage to both of them." << endl;
    if (groph <= 0)
    {
    cout << "Congratulations, you have defeated the grops!" << endl;
    cout << "\a The grops stare in awe as you bring your mighty " << 1 << " down to bear on them." << endl;
    cout << "You have gained " << gropex << " experience points." << endl;
    EXP = EXP + gropex;

    cout << "Your total exp is " << EXP << "." << endl;
    }
    if (EXP >= 2000)
    {
    void levelUp(int health, int health2, int speed, int agility, int strength, int intelligence, int defense, int dexterity, int skill, int EXP, int level);
    {
    cout << setw(45) << "LEVEL UP!!!" << endl;
    cout << "---------------------------------------------------------------------------" << endl;
    cout << "You have ahceived new Statistics!!!" << endl;
    health = health + 4;
    health2 = health + 4;
    speed = speed + 1;
    agility = agility + 2;
    strength = strength + 2;
    intelligence = intelligence + 1;
    defense = defense + 2;
    dexterity = dexterity + 1;
    skill = skill + 3;
    EXP = EXP + 0;
    level = level + 1;
    cout << "Level" << setw(35) << level << endl;
    cout << "===" << endl;
    cout << "Health" << setw(34) << health << endl;
    cout << "Speed" << setw(35) << speed << endl;
    cout << "Agility" << setw(33) << agility << endl;
    cout << "Strength" << setw(32) << strength << endl;
    cout << "Intelligence" << setw(28) << intelligence << endl;
    cout << "Defense" << setw(33) << defense << endl;
    cout << "Dexterity" << setw(31) << dexterity << endl;
    cout << "Skill" << setw(35) << skill << endl;
    }
    }
    else;
    }
    }while (groph > 0);

    cout << "Do you wish to save your game at this time?" << endl;
    cout << "1=yes 2=no" << endl;
    cin >> response;
    if (response == 1)
    {

    ofstream outfile;
    outfile.open("savefile.txt");
    assert(! outfile.fail());
    outfile <<
    "Name: " << name << endl << "Health: " << health << endl << "Strength: " << strength << endl << "Speed: " << speed << endl << "Skill: " << skill << endl << "Dexterity: " << dexterity << endl << "Intelligence: " << intelligence << endl << "Defense: " << defense << endl << "Experience: " << EXP << endl << "Level : " << level <<
    endl;

    outfile.close();
    assert(! outfile.fail());

    cout << "Saving game data." << endl;
    cout << "a\a\a\a\a\a" << endl;
    cout << "Save succesful!" << endl;
    }
    else
    {
    cout << endl;
    }

    if (x == 1)
    {
    cout << name << " approached Darien's house, and knocked on the door." << endl;
    cout << endl;
    cout << "DARIEN|| Hey " << name << ", whats up?" << endl;
    cout << name << "|| Not much. I really have to ask you something though." << endl;
    cout << endl;
    cout << "-- " << name << " paused for a short while. Unsure of how to approach the sensitive" << endl << " subject. --" << endl;
    }
    else
    {
    cout << "-- " << name << " entered the store, and began walking up and down" << endl << " the aisles looking for the milk. --" << endl;
    cout << "STORE OWNER|| Hello, what can I do for you?" << endl;
    cout << name << "|| Can you tell me where the milk aisle is please?" << endl;
    cin >> a;
    cout << "STORE OWNER|| Sure, it's right down aisle 4, at the end." << endl;
    cout << name << "|| Thanks a bunch." << endl;
    cout << "STORE OWNER|| You're that kid, um " << name << " right?" << endl;
    cout << name << "|| Yes, why?" << endl;
    cin >> a;
    cout << "STORE OWNER|| Well, there has been some talk that you may be in grave danger." << endl;
    cout << "STORE OWNER|| Good day to you..." << endl;
    cout << "-- The store owner then walked away, not giving " << name << " a chance to ask about what" << endl << "he said." << endl;
    }
    cin >> a;
    return 0;
    }
  2. #2
  3. No Profile Picture
    YaBN (Yet another BSD Newbie)
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Posts
    74
    Rep Power
    14
    Code:
    if (response == 'Y' || 'y')
    should be
    Code:
    if (response == 'Y' || response == 'y')
    -MattWil

IMN logo majestic logo threadwatch logo seochat tools logo