File I/O assistance needed

I'm starting a little text-based RPG project and need help with FileI/O. i can get the code to write the characters name to the file (WrathData.txt) but cannot get the program to read it from that file when i restart the application. please help.

the BOLD text is the problem area.
here's the code:




#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <windows.h>
#include <cstdlib>
#include <string>
using namespace std;

string charname;//getting character's name
string newgame;//new game y/n holder
string gamedata;//for file io
int x;//for file io



int main()
{
/*Main Intro Screen*/
cout << "================================== Wrath ==================================" << endl;
cout << endl;
cout << "Welcome to Wrath, the text based RPG game." << endl;
cout << endl;
cout << "================================== Wrath ==================================" << endl;
system("PAUSE");
system("cls");
/*Bottom of Intro Screen*/

/*New Game y/n*/
cout << "Are you starting a new game? (y/n)";
cin >> newgame;

/*New game Yes*/
if(newgame == "y"){
/*Get data from file*/

/*Get Character Name*/
cout << "What is your name? ";
cin >> charname;
system("cls");
cout << "Welcome " << charname <<", to the temple of The Gods\n";
Sleep(1000);
cout << "Prepare to feel the wrath!\n";
Sleep(2000);
system("cls");
/*End Get Character Name*/

ofstream WrathData;
WrathData.open("WrathData.txt");
WrathData << charname << endl;
WrathData.close();


/*Intro*/
cout << "So, " << charname <<",\n";
Sleep(1500);
cout << "you are a demigod, A god/human mix breed.\n";
Sleep(1500);
cout << "capable of utilizing marvelous powers.\n";
Sleep(1500);
cout << "but, before you can use these powers your father god must accept you\n";
Sleep(2000);
cout << "as his son...\n";
Sleep(3000);
system("cls");
/*End Intro*/

}//End new game Yes



if(newgame == "n"){
char str[15];
ifstream WrathData("WrathData.txt");
while(!WrathData.eof()){
WrathData.getline(str,15);
str == charname;
}//while wrath dataeof

WrathData.close();//closes file
}//End new game No









/*Main Menu*/
cout << "___________________________________________________________________________\n";
cout << " _ _ _ _ \n";
cout << "( ) _ ( ) ( )_( ) \n";
cout << "| | ( ) | | _ __ _ _ | ,_) |__\n";
cout << "| | | | | |( '__)'_` )| | | _ `\n";
cout << "| (_/ \\_) || | ( (_| || |_| | | |\n";
cout << "`\\___x___/'(_) `\\__,_)`\\__)_) (_)\n";
cout << "___________________________________________________________________________\n";
cout << "Welcome back " << charname << ","<< endl;
cout << "Type the number cooresponding with the option you wish to choose.\n";
cout << "1 - Campaign\n";
cout << "2 - Blacksmith's Shop\n";
cout << "3 - The Good Ol' Tavern\n";
cout << "___________________________________________________________________________\n";
/*End Main Menu*/







system("PAUSE");
}
Obligatory link: http://cplusplus.com/forum/articles/28558/

Anyway why are you using a char array to save the player name? Why not just save it as a string:

1
2
3
4
5
6
7
8
9
10
string charname;

// when saving
ofstream file("whatever");
file << charname << '\n';  // put a new line after the name


// when loading
ifstream file("whatever");
getline(file,charname);
Topic archived. No new replies allowed.