Hi, I'm new to the forum and need some help writing my first 'complicated' program.

I have trouble writing to an existing file.

I want to write to an Excel file I already created and edited exclusively for the program I wrote, but when I write the data it destroys all the editing of the file and I don't know what's wrong or if it's even possible to do what I'm trying to.

Im pretty sure all the writing to the file goes in blank spaces, so I don't know what is the problem.

I'm following the commands (the only one I know to write to files) described below:

fout.open("Character.xls");

// loads of output text

fout.close();

All my program works fine except for the writing to the Excel file, I don't get any errors.

Well, I think that's it

Hope somebody can help me soon.

Okay, I tried something from the link you posted, but I just got more confused :lol:

I'll post you what im using now:

filebuf iobuf;

	iobuf.open("Character.xls",ios_base::in | ios_base::out);

\\ loads of input text

        iobuf.close();

<< moderator edit: added [code][/code] tags >>

I'm still using the fout because the compiler tells me its necessary to input the data, but now the program wont return me anything, it leaves the file in blank, just like it was. So:
1. Do I have to create another way of calling a file output?
2. Do I need something else than #include <fstream>?

I've worked in this program four days in a row, just stopping to eat, sleep and other neccessities, and now that I've reached the end it just seems I worked for nothing. Its frustating, please help :cry:

Im kind of new to programming, only the past 2 weeks with tutorials all by myself, if you can explain something to me I'm willing to learn, just go easy ;)

Okay, I tried something from the link you posted, but I just got more confused :lol:

Yeah, me too.

I'll post you what im using now:

filebuf iobuf;

	iobuf.open("Character.xls",ios_base::in | ios_base::out);

\\ loads of input text

        iobuf.close();

<< moderator edit: added [code][/code] tags >>

:?:

I'm still using the fout because the compiler tells me its necessary to input the data, but now the program wont return me anything, it leaves the file in blank, just like it was. So:
1. Do I have to create another way of calling a file output?
2. Do I need something else than #include <fstream>?

I asked what type it was. You still haven't mentioned it. What is fout?

The page I linked to was to help explain the open modes.

Also, this

// loads of output text

doesn't tell me a whole lot about what you expect to accomplish.

I'm really sorry, I forgot your question.

Yes, fout is ofstream.

Theres is a lot of output text, so I didn't want to get anyone bored, well, if you're asking for it, here it is, my complete code.

I didn't attach the header file because is way too messy, after all, Im noob :cheesy: , but I think this holds everything you need to detect a problem.

Thank you for your time.

Attachments
#include "RPG.h"
#include <fstream>

void main()
{
	ofstream fout;

	AbilityFunc();

	Species();

	AbilityMod();

	Class();

	BuyWep1();

	bool byesno = true;

	do
	{

		char yesno;

		cout << "Do you want to buy a secondary weapon?Y/N" << endl << endl;

		cin >> yesno;

		if((yesno == 'y') || (yesno ==  'Y'))
		{
			BuyWep2();
			byesno = false;
		}

		else if((yesno == 'n') || (yesno ==  'N'))
			byesno = false;

		else 
		{
			cout << "Input valid code" << endl << endl;
			byesno = true;
		}
	
	}while(byesno == true);

	do
	{
		byesno = true;

		char yesno;

		cout << "Do you want to buy an Armor?Y/N" << endl << endl;

		cin >> yesno;

		if((yesno == 'y') || (yesno ==  'Y'))
		{
			BuyArmor();
			byesno = false;
		}

		else if((yesno == 'n') || (yesno ==  'N'))
			byesno = false;

		else
		{
			cout << "Input valid code" << endl << endl;
			byesno = true;
		}
	
	}while(byesno == true);

	filebuf iobuf;

	iobuf.open("Character.xls",ios_base::in | ios_base::out);

	fout << "\t" << Player.szName << endl;
	fout << "\t" << Player.szSpecies << endl;
	fout << "\t" << Player.szClass << endl << endl << endl << endl;

	fout << "\t" << Ability.Str << "\t" << Ability.strm << "\t\t\t" << Player.vit << "\t\t\t" << Player.woundp << endl;
	fout << "\t" << Ability.Dex << "\t" << Ability.dexm << "\t\t\t" << (10 + Player.defbon + Player.armor.armbon + Ability.dexm + Player.sizem)\
		<< "=\t" << Player.defbon << "+\t" << Player.armor.armbon << "+\t" << Ability.dexm << "+\t" << Player.sizem << "\t\t" << Player.armor.checkp << endl;
	fout << "\t" << Ability.Con << "\t" << Ability.conm << endl;
	fout << "\t" << Ability.Int << "\t" << Ability.intm << "\t\t\t" << Player.speed << "\t\t\t" << Ability.dexm << endl;
	fout << "\t" << Ability.Wis << "\t" << Ability.wism << "\t\t\t" << Player.basea << endl;
	fout << "\t" << Ability.Cha << "\t" << Ability.cham << endl << endl << endl;

	fout << "\t" << Player.fort << "=\t" << Player.forts << "+\t" << Ability.conm << "+\t" << Player.fortm << endl;
	fout << "\t" << Player.ref << "=\t" << Player.refs << "+\t" << Ability.dexm << "+\t" << Player.refm << endl;
	fout << "\t" << Player.will << "=\t" << Player.wills << "+\t" << Ability.wism << "+\t" << Player.willm << endl << endl << endl;

	fout << "\t" << Player.mab << "=\t" << Player.basea << "+\t" << Ability.strm << endl;
	fout << "\t" << Player.rab << "=\t" << Player.basea << "+\t" << Ability.dexm << endl << endl << endl << endl;

	fout << Player.wep1.szWep1 << "\t" << Player.wep1.Aroll1 << "\t" << Player.wep1.szDroll1 << "\t" << Player.wep1.szCrit1 << endl << endl;
	fout << Player.wep1.szRange1 << "\t" << Player.wep1.szWeight1 << "\t" << Player.wep1.szSize1 << "\t" << Player.wep1.szStun1 << endl << endl << endl << endl;

	fout << Player.wep2.szWep2 << "\t" << Player.wep2.Aroll2 << "\t" << Player.wep2.szDroll2 << "\t" << Player.wep2.szCrit2 << endl << endl;
	fout << Player.wep2.szRange2 << "\t" << Player.wep2.szWeight2 << "\t" << Player.wep2.szSize2 << "\t" << Player.wep2.szStun2 << endl << endl;

	fout << "\t" << Player.spoint << endl;
	fout << "\t" << Player.money << endl << endl << endl;

	fout << Player.armor.szArmor << "\t" << Player.armor.szTypea << "\t" << Player.armor.armbon << "\t" << Player.armor.maxdex << endl << endl;
	fout << Player.armor.checkp << "\t" <<  Player.armor.aspeed << "\t" << Player.armor.szWeighta << "\t" << "L/M/S" << endl << endl << endl << endl;

	fout << "\t" << Player.szExtra << endl << endl << endl << endl << endl;


	fout << "\t" << Ability.intm << endl;
	fout << "\t" << "0" << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << Ability.strm << endl;
	fout << "\t" << Ability.intm << endl;
	fout << "\t" << Ability.intm << endl;
	fout << "\t" << "0" << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << "0" << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << Ability.intm << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << "0" << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << Ability.cham << endl;
	fout << "\t" << Ability.strm << endl;
	fout << "\t" << "0" << endl << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << "0" << endl << endl << endl;
	fout << "\t" << "0" << endl;
	fout << "\t" << Ability.dexm << endl;
	fout << "\t" << Ability.intm << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << "0" << endl << endl << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << Ability.strm << endl;
	fout << "\t" << Ability.wism << endl;
	fout << "\t" << "0" << endl;

	iobuf.close();
				
}

I didn't attach the header file because is way too messy, after all, Im noob , but I think this holds everything you need to detect a problem.

Without the header, I can't compile. ;)

ofstream fout;
// ...
filebuf iobuf;
iobuf.open("Character.xls",ios_base::in | ios_base::out);

I think you want fout to be an fstream (not an ofstream) and open it in the proper mode.

fstream fout;
// ...
fout.open("Character.xls",ios_base::in | ios_base::out);

And it really seems that you file is a tab-separated-values (.tsv) file, not an .xls.

Now it writes at the top of the file, destroys all my editing done in the file and returns funny characters after all of the data was input. It looks very weird, and funny at the same time. Well, this happen after I did what you told me too in your last reply.

I'll add the header file, too bad I can't attach the Excel file, I really made it look good.

Thanks again, I really appreciate that you are responding to my messages. :D

Theres a few things you need to know about my program:

After buying the first weapon and returning to the menu of weapons you must quit or will be charged and what you already bought will be deleated, same for the second weapon.

Attachments
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

typedef struct _PLAYER{
	string szName;
	string szClass;
	string szSpecies;
	typedef struct _WEP1{
		string szWep1;
		int Aroll1;
		string szDroll1;
		string szCrit1;
		string szRange1;
		string szWeight1;
		string szType1;
		string szSize1;
		string szStun1;
	}WEP1;
	WEP1 wep1;
	typedef struct _WEP2{
		string szWep2;
		int Aroll2;
		string szDroll2;
		string szCrit2;
		string szRange2;
		string szWeight2;
		string szType2;
		string szSize2;
		string szStun2;
	}WEP2;
	WEP2 wep2;
	typedef struct _ARMOR{
		string szArmor;
		string szTypea;
		int armbon;
		int maxdex;
		int checkp;
		int aspeed;
		string szWeighta;
	}ARMOR;
	ARMOR armor;
	string szExtra;
	int money;
	int vit;
	int woundp;
	int fort;
	int ref;
	int will;
	int speed;
	int basea;
	int mab;
	int rab;
	int spoint;
	int forts;
	int wills;
	int refs;
	int fortm;
	int willm;
	int refm;
	int sizem;
	int natarm;
	int defbon;
}PLAYER;

PLAYER Player;

typedef struct _ABILITY{
	int Str;
	int Dex;
	int Con;
	int Int;
	int Wis;
	int Cha;
	int strm;
	int dexm;
	int conm;
	int intm;
	int wism;
	int cham;
}ABILITY;

ABILITY Ability;

void AbilityMod();

void AbilityMod()
{
	if(Ability.Str == 7)
		Ability.strm = -2;
	else if((Ability.Str ==  8) || (Ability.Str == 9))
		Ability.strm = -1;
	else if((Ability.Str ==  10) || (Ability.Str == 11))
		Ability.strm = 0;
	else if((Ability.Str ==  12) || (Ability.Str == 13))
		Ability.strm = 1;
	else if((Ability.Str ==  14) || (Ability.Str == 15))
		Ability.strm = 2;
	else if((Ability.Str ==  16) || (Ability.Str == 17))
		Ability.strm = 3;
	else if((Ability.Str ==  18) || (Ability.Str == 19))
		Ability.strm = 4;
	else if((Ability.Str ==  20) || (Ability.Str == 21))
		Ability.strm = 5;
	else if(Ability.Str == 22)
		Ability.strm = 6;

	if(Ability.Dex == 7)
		Ability.dexm = -2;
	else if((Ability.Dex ==  8) || (Ability.Dex == 9))
		Ability.dexm = -1;
	else if((Ability.Dex ==  10) || (Ability.Dex == 11))
		Ability.dexm = 0;
	else if((Ability.Dex ==  12) || (Ability.Dex == 13))
		Ability.dexm = 1;
	else if((Ability.Dex ==  14) || (Ability.Dex == 15))
		Ability.dexm = 2;
	else if((Ability.Dex ==  16) || (Ability.Dex == 17))
		Ability.dexm = 3;
	else if((Ability.Dex ==  18) || (Ability.Dex == 19))
		Ability.dexm = 4;
	else if((Ability.Dex ==  20) || (Ability.Dex == 21))
		Ability.dexm = 5;
	else if(Ability.Dex == 22)
		Ability.dexm = 6;


	if(Ability.Con == 7)
		Ability.conm = -2;
	else if((Ability.Con ==  8) || (Ability.Con == 9))
		Ability.conm = -1;
	else if((Ability.Con ==  10) || (Ability.Con == 11))
		Ability.conm = 0;
	else if((Ability.Con ==  12) || (Ability.Con == 13))
		Ability.conm = 1;
	else if((Ability.Con ==  14) || (Ability.Con == 15))
		Ability.conm = 2;
	else if((Ability.Con ==  16) || (Ability.Con == 17))
		Ability.conm = 3;
	else if((Ability.Con ==  18) || (Ability.Con == 19))
		Ability.conm = 4;
	else if((Ability.Con ==  20) || (Ability.Con == 21))
		Ability.conm = 5;
	else if(Ability.Con == 22)
		Ability.conm = 6;

	if(Ability.Int == 7)
		Ability.intm = -2;
	else if((Ability.Int ==  8) || (Ability.Int == 9))
		Ability.intm = -1;
	else if((Ability.Int ==  10) || (Ability.Int == 11))
		Ability.intm = 0;
	else if((Ability.Int ==  12) || (Ability.Int == 13))
		Ability.intm = 1;
	else if((Ability.Int ==  14) || (Ability.Int == 15))
		Ability.intm = 2;
	else if((Ability.Int ==  16) || (Ability.Int == 17))
		Ability.intm = 3;
	else if((Ability.Int ==  18) || (Ability.Int == 19))
		Ability.intm = 4;
	else if((Ability.Int ==  20) || (Ability.Int == 21))
		Ability.intm = 5;
	else if(Ability.Int == 22)
		Ability.intm = 6;

	if(Ability.Wis == 7)
		Ability.wism = -2;
	else if((Ability.Wis ==  8) || (Ability.Wis == 9))
		Ability.wism = -1;
	else if((Ability.Wis ==  10) || (Ability.Wis == 11))
		Ability.wism = 0;
	else if((Ability.Wis ==  12) || (Ability.Wis == 13))
		Ability.wism = 1;
	else if((Ability.Wis ==  14) || (Ability.Wis == 15))
		Ability.wism = 2;
	else if((Ability.Wis ==  16) || (Ability.Wis == 17))
		Ability.wism = 3;
	else if((Ability.Wis ==  18) || (Ability.Wis == 19))
		Ability.wism = 4;
	else if((Ability.Wis ==  20) || (Ability.Wis == 21))
		Ability.wism = 5;
	else if(Ability.Wis == 22)
		Ability.wism = 6;

	if(Ability.Cha == 7)
		Ability.cham = -2;
	else if((Ability.Cha ==  8) || (Ability.Cha == 9))
		Ability.cham = -1;
	else if((Ability.Cha ==  10) || (Ability.Cha == 11))
		Ability.cham = 0;
	else if((Ability.Cha ==  12) || (Ability.Cha == 13))
		Ability.cham = 1;
	else if((Ability.Cha ==  14) || (Ability.Cha == 15))
		Ability.cham = 2;
	else if((Ability.Cha ==  16) || (Ability.Cha == 17))
		Ability.cham = 3;
	else if((Ability.Cha ==  18) || (Ability.Cha == 19))
		Ability.cham = 4;
	else if((Ability.Cha ==  20) || (Ability.Cha == 21))
		Ability.cham = 5;
	else if(Ability.Cha == 22)
		Ability.cham = 6;
}

void AbilityFunc();

void AbilityFunc()
{
	int try1=0, try2=0, try3=0, try4=0, try5=0, try6=0, try7=0, try8=0, try9=0, try10=0, try11=0, try12=0, try13=0, try14=0, try15=0, try16=0, try17=0, try18=0;
	int ab1=0, ab2=0, ab3=0, ab4=0, ab5=0, ab6=0;
	string szAb1, szAb2, szAb3, szAb4, szAb5, szAb6;

	srand(GetTickCount());

	do
	{
		try1 = rand()%7;
	}while(try1 <= 2);

	do
	{
		try2 = rand()%7;
	}while(try2 <= 2);

	do
	{
		try3 = rand()%7;
	}while(try3 <= 2);

	do
	{
		try4 = rand()%7;
	}while(try4 <= 2);

	do
	{
		try5 = rand()%7;
	}while(try5 <= 2);

	do
	{
		try6 = rand()%7;
	}while(try6 <= 2);

	do
	{
		try7 = rand()%7;
	}while(try7 <= 2);

	do
	{
		try8 = rand()%7;
	}while(try8 <= 2);

	do
	{
		try9 = rand()%7;
	}while(try9 <= 2);

	do
	{
		try10 = rand()%7;
	}while(try10 <= 2);

	do
	{
		try11 = rand()%7;
	}while(try11 <= 2);

	do
	{
		try12 = rand()%7;
	}while(try12 <= 2);

	do
	{
		try13 = rand()%7;
	}while(try13 <= 2);

	do
	{
		try14 = rand()%7;
	}while(try14 <= 2);

	do
	{
		try15 = rand()%7;
	}while(try15 <= 2);

	do
	{
		try16 = rand()%7;
	}while(try16 <= 2);

	do
	{
		try17 = rand()%7;
	}while(try17 <= 2);

	do
	{
		try18 = rand()%7;
	}while(try18 <= 2);

	ab1 = try1 + try2 + try3;
	ab2 = try4 + try5 + try6;
	ab3 = try7 + try8 + try9;
	ab4 = try10 + try11 + try12;
	ab5 = try13 + try14 + try15;
	ab6 = try16 + try17 + try18;

	cout << "Ability Codes(capslock needed): STR, DEX, CON, INT, WIS and CHA" << endl << endl;

	cout << "You have the following scores" << endl << endl;

	cout << "Score1 " << ab1 << endl;
	cout << "Score2 " << ab2 << endl;
	cout << "Score3 " << ab3 << endl;
	cout << "Score4 " << ab4 << endl;
	cout << "Score5 " << ab5 << endl;
	cout << "Score6 " << ab6 << endl;

	cout << endl << endl;

	cout << "Do not use spaces in the following inputs: " << endl << endl;

	do
	{
		ab1 = try1 + try2 + try3;

		cout << "Enter which ability you want for score1: " << ab1 << endl << endl;

		cin >> szAb1;

		cout << endl;

		if(szAb1 == "STR")
			Ability.Str = ab1;

		else if(szAb1 == "DEX")
			Ability.Dex = ab1;

		else if(szAb1 == "CON")
			Ability.Con = ab1;

		else if(szAb1 == "INT")
			Ability.Int = ab1;

		else if(szAb1 == "WIS")
			Ability.Wis = ab1;

		else if(szAb1 == "CHA")
			Ability.Cha = ab1;

		else
		{
			ab1 = 0;
			cout << "Input a valid code" << endl << endl;
		}
	}while(ab1 == 0);

	do
	{
		ab2 = try4 + try5 + try6;

		cout << "Enter which ability you want for score2: " << ab2 << endl << endl;

		cin >> szAb2;

		cout << endl;

		if(szAb2 == "STR")
		{
			if(Ability.Str == ab1)
			{
				cout << "You already input STR" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Str != ab1)
				Ability.Str = ab2;
		}

		else if(szAb2 == "DEX")
		{
			if(Ability.Dex == ab1)
			{
				cout << "You already input DEX" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Dex != ab1)
				Ability.Dex = ab2;
		}

		else if(szAb2 == "CON")
		{
			if(Ability.Con == ab1)
			{
				cout << "You already input CON" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Con != ab1)
				Ability.Con = ab2;
		}

		else if(szAb2 == "INT")
		{
			if(Ability.Int == ab1)
			{
				cout << "You already input INT" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Int != ab1)
				Ability.Int = ab2;
		}

		else if(szAb2 == "WIS")
		{
			if(Ability.Wis == ab1)
			{
				cout << "You already input WIS" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Wis != ab1)
				Ability.Wis = ab2;
		}

		else if(szAb2 == "CHA")
		{
			if(Ability.Cha == ab1)
			{
				cout << "You already input CHA" << endl << endl;
				ab2 = 0;
			}
			else if(Ability.Cha != ab1)
				Ability.Cha = ab2;
		}

		else
		{
			ab2 = 0;
			cout << "Input a valid code" << endl << endl;
		}
	}while(ab2 == 0);

	do
	{
		ab3 = try7 + try8 + try9;

		cout << "Enter which ability you want for score3: " << ab3 << endl << endl;

		cin >> szAb3;

		cout << endl;

		if(szAb3 == "STR")
		{
			if((Ability.Str == ab1) || (Ability.Str == ab2))
			{
				cout << "You already input STR" << endl << endl;
				ab3 = 0;
			}
			else if((Ability.Str != ab1) || (Ability.Str != ab2))
				Ability.Str = ab3;
		}

		else if(szAb3 == "DEX")
		{
			if((Ability.Dex == ab1) || (Ability.Dex == ab2))
			{
				cout << "You already input DEX" << endl << endl;
				ab3 = 0;
			}
			else if((Ability.Dex != ab1) || (Ability.Dex != ab2))
				Ability.Dex = ab3;
		}

		else if(szAb3 == "CON")
		{
			if((Ability.Con == ab1) || (Ability.Con == ab2))
			{
				cout << "You already input CON" << endl << endl;
				ab3 = 0;
			}
			else if((Ability.Con != ab1) || (Ability.Con != ab2))
				Ability.Con = ab3;
		}

		else if(szAb3 == "INT")
		{
			if((Ability.Int == ab1) || (Ability.Int == ab2))
			{
				cout << "You already input INT" << endl << endl;
				ab3 = 0;
			}
			else if((Ability.Int != ab1) || (Ability.Int != ab2))
				Ability.Int = ab3;
		}

		else i

Code in a header...! You have much to learn young grasshopper.

Sorry I haven't been that great of a help thus far. But have you considered outputting your data to a bland old .tsv file and then use Excel's wonderful import abilities to spruce things up on that end?

Thanks again,

I have some more doubts:

What is a .tsv file?
I did it but no file appears in the directory. "Character.tsv"
And how do i get excel to import it and paste it where i want to?

I tell you, now that I'm remembering, I only have a week programming, any suggestions will be happily accepted. :mrgreen:

I'll just have my program save an Excel file and then copy everything to the formatted one, still a bit of trouble, but it's the best I could do. Thanks for your help, I really appreciated it.

(If you happen to find a way, please post it)

>What is a .tsv file?

A tab separated values file, like a .csv is a comma separated values file. A text file with field delimiters.

>And how do i get excel to import it and paste it where i want to?

Something like this.

[IMG]http://img277.echo.cx/img277/5011/excel11cp.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/2338/excel20hk.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/239/excel35gt.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/5059/excel44zc.th.jpg[/IMG] [IMG]http://img277.echo.cx/img277/4274/excel54ym.th.jpg[/IMG]
Before you plop it in, you can set up the properties.
[IMG]http://img277.echo.cx/img277/7555/excel64ub.jpg[/IMG] [IMG]http://img277.echo.cx/img277/7793/excel71wx.th.jpg[/IMG]
Then you can update the file from your source code and see the updates via Excel doing a lot of the dirty work.
[IMG]http://img277.echo.cx/img277/2043/excel80ac.th.jpg[/IMG]

This article has been dead for over six months. Start a new discussion instead.