wont write to variable

This is a discussion on wont write to variable within the C++ Programming forums, part of the General Programming Boards category; the code(ignore the xtra header files Code: #include <iostream> #include <string> #include <stdlib.h> #include <windows.h> #include <conio.h> #include <fstream> using ...

  1. #1
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608

    wont write to variable

    the code(ignore the xtra header files
    Code:
    #include <iostream>
    #include <string> 
    #include <stdlib.h> 
    #include <windows.h>
    #include <conio.h>
    #include <fstream>
    using namespace std;
    
    int gold, health, magic, attack, aim, defence, menuselect, ynvari, charactertype;
    string weapon, shield, armor, szWord, szLine, player, empty, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20, item21, item22, item23, item24, item25, item26, item27, item28, item29, item30;
    
    int main(int argc, char *argv[])
    {
    empty = "empty";
       	ifstream fin;
    	fin.open("Stats.sav");
    	if(fin.fail())
    	{
    		cout << "ERROR: Could not find Stats.sav!\n";
            system("pause");
            goto menu;
    	}
    
    	cout << endl;
      	fin.clear();
    
        fin >> szWord >> player;
        fin >> szWord >> charactertype;
        fin >> szWord >> health;
        fin >> szWord >> gold;
        fin >> szWord >> magic;
        fin >> szWord >> attack;
        fin >> szWord >> aim;
        fin >> szWord >> defence;
        fin >> szWord >> weapon;
        fin >> szWord >> shield;
        fin >> szWord >> armor;
        fin >> szWord >> item1;
        fin >> szWord >> item2;
        fin >> szWord >> item3;
        fin >> szWord >> item4;
        fin >> szWord >> item5;
        fin >> szWord >> item6;
        fin >> szWord >> item7;
        fin >> szWord >> item8;
        fin >> szWord >> item9;
        fin >> szWord >> item10;
        fin >> szWord >> item11;
        fin >> szWord >> item12;
        fin >> szWord >> item13;
        fin >> szWord >> item14;
        fin >> szWord >> item15;
        fin >> szWord >> item16;
        fin >> szWord >> item17;
        fin >> szWord >> item18;
        fin >> szWord >> item19;
        fin >> szWord >> item20;
        fin >> szWord >> item21;
        fin >> szWord >> item22;
        fin >> szWord >> item23;
        fin >> szWord >> item24;
        fin >> szWord >> item25;
        fin >> szWord >> item26;
        fin >> szWord >> item27;
        fin >> szWord >> item28;
        fin >> szWord >> item29;
        fin >> szWord >> item30;
    	fin.close();
    cout << player << endl;
    cout << charactertype << endl; // or any varibale besides player
       cin.get();
       return 0;
    }
    it writes to only the player variable. the stuff inside stats.sav is......

    Player: Playername
    CharacterType: 2
    Health: 100
    Gold: 75
    Magic: 9
    Attack: 13
    Aim: 13
    Defence: 11
    Weapon: empty
    Shield: empty
    Armor: empty
    Item1: dagger
    Item2: mediumsword
    Item3: bronzesmallshield
    Item4: bronzeplatewh
    Item5: hppotion
    Item6: hppotion
    Item7: hppotion
    Item8: hppotion
    Item9: hppotion
    Item10: empty
    Item11: empty
    Item12: empty
    Item13: empty
    Item14: empty
    Item15: empty
    Item16: empty
    Item17: empty
    Item18: empty
    Item19: empty
    Item20: empty
    Item21: empty
    Item22: empty
    Item23: empty
    Item24: empty
    Item25: empty
    Item26: empty
    Item27: empty
    Item28: empty
    Item29: empty
    Item30: empty
    i wrote to the file int he same program using this code, it wrote to the file ok and stuff
    Code:
    cout << "Choose your characters name." << endl ;
    cin >> player ;
    cout << "Choose what kind of character to be." << endl ;
    cout << "1. Mage 2. Barbarian 3. Warrior 4. Archer" << endl ;
    cin >> charactertype ;
    switch( charactertype )
    {
            case 1:    //Mage
            magic=60;
            attack=3;
            aim=7;
            defence=9;
            item1 = "wizardybook";
            item2 = "basicstaff";
            item3 = "robe";
            item4 = "manapotion";
                             break;
    
            case 2:     //Barbarian
            magic=9;
            attack=13;
            aim=13;
            defence=11;
            item1="dagger";
            item2="mediumsword";
            item3="bronzesmallshield";
            item4="bronzeplatewh";
                           break;
    
            case 3:     //Warrior
            magic=9;
            attack=13;
            aim=13;
            defence=11;
            item1="dagger";
            item2="mediumsword";
            item3="bronzesmallshield";
            item4="bronzeplatewh";
                           break;
    
            case 4:    //Archer
            magic=9;
            attack=12;
            aim=16;
            defence=11;
            item1="dagger";
            item2="arrows";
            item3="leathercloths";
            item4="bow";
                           break;
    
    default:
       break;
    }        // end charactertype
    gold=75;
    health=100;
    item5="hppotion";
    item6="hppotion";
    item7="hppotion";
    item8="hppotion";
    item9="hppotion";
    item10=empty;
    item11=empty;
    item12=empty;
    item13=empty;
    item14=empty;
    item15=empty;
    item16=empty;
    item17=empty;
    item18=empty;
    item19=empty;
    item20=empty;
    item21=empty;
    item22=empty;
    item23=empty;
    item24=empty;
    item25=empty;
    item26=empty;
    item27=empty;
    item28=empty;
    item29=empty;
    item30=empty;
    weapon=empty;
    shield=empty;
    armor=empty;
      // finishes off item varibles and starts writing to stats.sav
    
    ofstream fout;
        fout.open("stats.sav");
        fout << "Player: " << player << endl;
        fout << "CharacterType: " << charactertype << endl;
     	fout << "Health: " << health << endl;
    	fout << "Gold: "   << gold   << endl;
        fout << "Magic: "  << magic   << endl;
        fout << "Attack: " << attack   << endl;
        fout << "Aim: "    << aim   << endl;
        fout << "Defence: "<< defence   << endl;
        fout << "Weapon: "   << weapon << endl;
        fout << "Shield: "   << shield << endl;
        fout << "Armor: "   << armor << endl;
        fout << "Item1: " << item1 << endl;
        fout << "Item2: " << item2 << endl;
        fout << "Item3: " << item3 << endl;
        fout << "Item4: " << item4 << endl;
        fout << "Item5: " << item5 << endl;
        fout << "Item6: " << item6 << endl;
        fout << "Item7: " << item7 << endl;
        fout << "Item8: " << item8 << endl;
        fout << "Item9: " << item9 << endl;
        fout << "Item10: " << item10 << endl;
        fout << "Item11: " << item11 << endl;
        fout << "Item12: " << item12 << endl;
        fout << "Item13: " << item13 << endl;
        fout << "Item14: " << item14 << endl;
        fout << "Item15: " << item15 << endl;
        fout << "Item16: " << item16 << endl;
        fout << "Item17: " << item17 << endl;
        fout << "Item18: " << item18 << endl;
        fout << "Item19: " << item19 << endl;
        fout << "Item20: " << item20 << endl;
        fout << "Item21: " << item21 << endl;
        fout << "Item22: " << item22 << endl;
        fout << "Item23: " << item23 << endl;
        fout << "Item24: " << item24 << endl;
        fout << "Item25: " << item25 << endl;
        fout << "Item26: " << item26 << endl;
        fout << "Item27: " << item27 << endl;
        fout << "Item28: " << item28 << endl;
        fout << "Item29: " << item29 << endl;
        fout << "Item30: " << item30 << endl;
        	fout.close();
    DONT COMPLAIN ABOUT GOTO STATEMENTS I WILL BE TAKING THEM OUT LATER!!!!!!!!!!
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  2. #2
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608

    Unhappy

    and it may never....
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  3. #3
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    d u know about ifstreams? does anyone!>?!?!?!??
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  4. #4
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    usnig my own wits i have figure dout wut was wrong while it took views of 31 people to do nothing helpful.
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write a program
    By Tashfique in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2008, 11:28 AM
  2. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 12:25 PM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM

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