Thread: File IO - Pulling numbers out of nowhere.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    83

    File IO - Pulling numbers out of nowhere.

    Alright - My SAVE LOAD is working great - except for one number. That number is the RACE and CLASS number. It tells the game what RACE the player is, and what CLASS the player is. It is the index of what member of the array of pointers containing all of the available RACEs/CLASSes. The CLASS number is 0, or 1. Here's my fileio.cpp file...
    Code:
    #include"player.h"
    #include"main.h"
    #include<fstream.h>
    #include"baseStats.h"
    #include"item.h"
    #include"magic.h"
    
    int getNextInt(ifstream& loadFile);
    
    int saveGame(char* fileName)
    {
    	ofstream saveFile(fileName, ios::out);
    	if(!saveFile)	
    	{
    		cout << "Error opening file for saving!";
    		return 0;
    	}
    
    	saveFile << "#Player Name\n" << player1.szName;
    	saveFile << "\n#Speed\n" << player1.speed;
    	saveFile << "\n#Attack\n" << player1.attack;
    	saveFile << "\n#Wisdom\n" << player1.wisdom;
    	saveFile << "\n#Defense\n" << player1.defense;
    	saveFile << "\n#CurHP\n" << player1.curHp;
    	saveFile << "\n#MaxHP\n" << player1.maxHp;
    	saveFile << "\n#CurMp\n" << player1.curMp;
    	saveFile << "\n#MaxMp\n" << player1.maxMp;
    
    	saveFile << "\n#Total EXP\n" << player1.totalexp;
    	saveFile << "\n#Level\n" << player1.level;
    	saveFile << "\n#EXP\n" << player1.exp;
    	saveFile << "\n#EXP TNL\n" << player1.tnl;
    
    	saveFile << "\n#No Items\n" << player1.noItems;
    	saveFile << "\n#No Spells\n" << player1.noSpells;
    
    	saveFile << "\n#Gold\n" << player1.gold;
    
    	saveFile << "\n#curX\n" << curX;
    	saveFile << "\n#curY\n" << curY;
    
    	saveFile << "\n#Current Area\n" << player1.curArea;
    	saveFile << "\n#Inventory";
    	for(int i =0;i < itemMax;i++)
    	{
    		if(player1.inventory[i])
    			saveFile << '\n' << player1.inventory[i]->idNo ;
    	}
    	saveFile << "\n#Spells";
    	for(i =0;i < spellMax;i++)
    	{
    		if(player1.spellInv[i])
    			saveFile  << '\n'<< player1.spellInv[i]->idNo;
    	}
    	
    	saveFile << "\n#Current Weapon\n" << player1.curWeapon->idNo;
    	saveFile << "\n#Current Armor\n" << player1.curArmor->idNo;
    
    	saveFile << "\n#Race\n" << player1.pRace->number;
    	saveFile << "\n#Class\n" << player1.pClass->number;
    
    	saveFile.close();
    	return 1;
    }
    
    int loadGame(char* fileName)
    {
    	for(int q =0;q<spellMax;q++)
    		player1.spellInv[q] = 0;
    	for(q =0;q<itemMax;q++)
    		player1.inventory[q] = 0;
    
    	ifstream loadFile(fileName, ios::in);
    
    	if(!loadFile)
    	{
    		cout << "Error opening file for loading,";
    		return 0;
    	}
    
    	char ch;
    	int i = 0;
    	while(!loadFile.eof())
    	{
    		loadFile.get(ch);
    		if(ch == '#')
    		{
    			loadFile.ignore(1000,'\n');
    			continue;
    		}
    		if(ch == '\0')
    			break;
    		if(ch == '\n')
    			break;
    		player1.szName[i] = ch;
    		i++;
    
    	}
    
    	player1.speed = getNextInt(loadFile);
    	player1.attack = getNextInt(loadFile);
    	player1.wisdom = getNextInt(loadFile);
    	player1.defense = getNextInt(loadFile);
    	player1.curHp = getNextInt(loadFile);
    	player1.maxHp = getNextInt(loadFile);
    	player1.curMp = getNextInt(loadFile);
    	player1.maxMp = getNextInt(loadFile);
    	player1.totalexp = getNextInt(loadFile);
    	player1.level = getNextInt(loadFile);
    	player1.exp = getNextInt(loadFile);
    	player1.tnl = getNextInt(loadFile);
    	player1.noItems = getNextInt(loadFile);
    	player1.noSpells = getNextInt(loadFile);
    	player1.gold = getNextInt(loadFile);
    	curX = getNextInt(loadFile);
    	curY = getNextInt(loadFile);
    	player1.curArea = getNextInt(loadFile);
    	int nNoPounds = 0;
    	for(int k =0;k<itemMax;k++)
    	{
    		if(loadFile.peek() == '#')
    			nNoPounds++;
    		if(nNoPounds == 2)
    			break;
    		player1.inventory[k] = ITEM.allItems[getNextInt(loadFile)];
    	}
    	nNoPounds = 0;
    	for(k =0;k<spellMax;k++)
    	{
    		if(loadFile.peek() == '#')
    			nNoPounds++;
    		if(nNoPounds == 2)
    			break;
    		player1.spellInv[k] = SPELL.allSpells[getNextInt(loadFile)];
    	}
    
    	player1.curWeapon = ITEM.allItems[getNextInt(loadFile)];
    	player1.curArmor = ITEM.allItems[getNextInt(loadFile)];
    
    	player1.pRace = RACE.allRaces[getNextInt(loadFile)];
    	player1.pClass = CLASS.allClasses[getNextInt(loadFile)];
    
    		
    			
    	return 1;
    }
    
    int getNextInt(ifstream& loadFile)
    {
    	int value = 0;
    	unsigned char ch = 0;
    	while(!loadFile.eof())
    	{
    		loadFile.get(ch);
    		if(ch == '#')
    		{
    			loadFile.ignore(1000,'\n');
    			continue;
    		}
    		if(ch == '\n')
    			break;
    		value = 10*value + (ch - 48);
    	}
    	return value;
    }
    The problem is, when it gets the class integer, it will get 1, or 0, like it should. Next it will pull out some bogus charecter like 315 etc. I have no idea where is come from. When it multiplies the 1 and 318 together, we get a totally weird value that crashes the program when CLASSes are used. The entire source is <a href=http://wetfire.angelcities.com/source.zip> here</a>. Thanks

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    The problem is with RACE now - now class. It's pulling out a 255 "y" from the file. It's not there, its a newline charecter. what the heck?

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Okay - I fixed it; I added a check - if its a bogus value, break. But I dont know why it's pulling number's out of it's pant's. Any ideas?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're using old headers.
    <iostream> instead of <iostream.h>
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    which should i use?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In your case, <fstream>.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    It's not working for me - I'm using VC6

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    iostream instead of iostream.h and fstream instead of fstream.h, etc are prefered because they are the new standards. listing the .h file extension in the #include directive is no longer the standard method, though many compilers in current use (maybe not for current sale or current downloading, but in current use) aren't up to date with the current standards. If you use the standard header style (without the .h) then you need to take into account a namespace as well. The simplist way, though not the prefered, is to add the line

    using namespace std;

    after the listing of header files for the program/file. Alternatives to using this line are distributed abundantly through the board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM