Archived

This topic is now archived and is closed to further replies.

ifstream problem

This topic is 5624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

the code(ignore the xtra header files
#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......
quote:
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
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@yahoo.com || AIM: Klinerr1 Website:[url]http://vc.flashstyle.org[/url] Wired were the eyes of a horse of a jet pilot when the smile flew right over the bay....

Share this post


Link to post
Share on other sites
i was trying to use ifstream to copy stuff form a txt file into varibales of my program. but i figured it all out (it gave me headaches also)

Email: Klinerr1@yahoo.com || AIM: Klinerr1
Website:[url]http://vc.flashstyle.org[/url]
Wired were the eyes of a horse of a jet pilot when the smile flew right over the bay....

Share this post


Link to post
Share on other sites
This doesn''t have much to do with your question, but here''s some advice.

I hope that your book stresses that global variables are bad programming practice, unless they''re const. Try getting in the habbit of keeping variables local.

Share this post


Link to post
Share on other sites
OMG! What the hell is this?!?

quote:
Original post by Alimonster
For the love of God, use arrays!


Ha! Even a better idea: use std::vector...

AND - use the source tags, encapsulated by ] and [

Regards,
Crispy

Share this post


Link to post
Share on other sites
quote:
Original post by Arkainium
This doesn''t have much to do with your question, but here''s some advice.

I hope that your book stresses that global variables are bad programming practice, unless they''re const. Try getting in the habbit of keeping variables local.


Give him a break, by the looks of it i''ld say hes only just started C++. Its not although hes going on a >10k lines project any time soon, when global variables will actually become a headache. Besides, global variables can sometimes be useful. cin and cout anyone?

Share this post


Link to post
Share on other sites