-
Save to File
Can anyone tell me what's wrong with this ? Thanx... I'm just trying to get a save-game function, but I can't get it to work...
Code:
#include <iostream.h>
#include <fstream.h>
char name[50];
int age[50];
char location[50];
char birthplace[50];
char filename[50];
char xit;
ifstream infile(filename);
ofstream outfile(filename);
int main()
{
cout << "Name ?" << endl << endl;
cin >> name[50];
cout << "\n\n\nAge ?" << endl << endl;
cin >> age[50];
cout << "\n\n\nLocation ?\n\n";
cin >> location[50];
cout << "\n\n\nBirthplace ?\n\n";
cin >> birthplace[50];
cout << "\n\n\nSave info where ?\n\n";
cin >> filename[50];
infile << name[50];
infile << "\n";
infile << age[50];
infile << "\n";
infile << location[50];
infile << "\n";
infile << birthplace[50];
cin >> xit;
return 0;
}
-
i believe "infile <<" should be "infile>> "
-
OOPS !
This is why I hate computers sometimes... too stupid to understand human error... maybe it's just me :D
And NOW I get it running, I type in name, and I get a fatal error...
Anyone ( again ) ? Thanx a lot for your support guys...
-
Code:
cout << "Name ?" << endl << endl;
cin >> name[50];
cout << "\n\n\nAge ?" << endl << endl;
cin >> age[50];
cout << "\n\n\nLocation ?\n\n";
cin >> location[50];
cout << "\n\n\nBirthplace ?\n\n";
cin >> birthplace[50];
cout << "\n\n\nSave info where ?\n\n";
cin >> filename[50];
infile << name[50];
infile << "\n";
infile << age[50];
infile << "\n";
infile << location[50];
infile << "\n";
infile << birthplace[50];
Remove the "[50]" off of everything there. (but leave it on your variable declarations) You need to read in an entire string, but by passing the 50th element to the stream, you're actually reading into the memory occupied by the null terminator... and any memory after that.
-
cin >> name[50];
Get rid of the brackets. eg:
cin >> name;
infile >> name;
-
24 C:\My Documents\delete\Untitled1.cpp
initializing non-const `bool &' with `int *' will use a temporary
217 C:\PROGRAMMING\DEV-CPP\include\g++-3\iostream.h
in passing argument 1 of `istream::operator >>(bool &)'
Hum... Us newbies need help... thanx for living up to it !
-
okay, change int age[50] to char age[50], it will compile
-
Sorry guys, still not. It compiles, the thing runs fine until it has to create the file, where I get another fatal error.
-
Exactly.
int age[50]; /* an array of 50 birthdays! */
char age[50]; /* reserve 50 chars to store a number in character format */
-
Post the entire program then.
-
#include <iostream.h>
#include <fstream.h>
char name[50];
char age[50];
char location[50];
char birthplace[50];
char filename[50];
char xit;
ifstream infile(filename);
ofstream outfile(filename);
int main()
{
cout << "Name ?" << endl << endl;
cin >> name;
cout << "\n\n\nAge ?" << endl << endl;
cin >> age;
cout << "\n\n\nLocation ?\n\n";
cin >> location;
cout << "\n\n\nBirthplace ?\n\n";
cin >> birthplace;
cout << "\n\n\nSave info where ?\n\n";
cin >> filename;
infile >> name;
infile >> "\n";
infile >> age;
infile >> "\n";
infile >> location;
infile >> "\n";
infile >> birthplace;
cin >> xit;
return 0;
}
-
Re: Save to File
Code:
#include <iostream.h>
#include <fstream.h>
char name[50];
int age;
char location[50];
char birthplace[50];
char filename[50];
char xit;
ofstream outfile;
int main()
{
cout << "Name ?" << endl << endl;
cin >> name;
cout << "\n\n\nAge ?" << endl << endl;
cin >> age;
cout << "\n\n\nLocation ?\n\n";
cin >> location;
cout << "\n\n\nBirthplace ?\n\n";
cin >> birthplace;
cout << "\n\n\nSave info where ?\n\n";
cin >> filename;
outfile.open(filename);
outfile<< name;
outfile<< '\n';
outfile<< age;
outfile<< '\n';
outfile<< location;
outfile<< '\n';
outfile<< birthplace;
cin >> xit;
return 0;
}
You'll probably want to use the outfile for saving data.
Also, you open your files by passing the uninitilized "filename" variable, then later you ask which file you'd like to save to. You should place the initilization (opening) of the outfile between your calls to cin and cout and file manipulation. Above is the revised code.
-
Code:
#include <iostream.h>
#include <fstream.h>
char name[50];
char age[50];
char location[50];
char birthplace[50];
char filename[50];
char xit;
int main()
{
cout << "Name ?" << endl << endl;
cin >> name;
cout << "\n\n\nAge ?" << endl << endl;
cin >> age;
cout << "\n\n\nLocation ?\n\n";
cin >> location;
cout << "\n\n\nBirthplace ?\n\n";
cin >> birthplace;
cout << "\n\n\nSave info where ?\n\n";
cin >> filename;
ofstream outfile(filename);
outfile >> name;
outfile >> "\n";
outfile >> age;
outfile >> "\n";
outfile >> location;
outfile >> "\n";
outfile >> birthplace;
cin >> xit;
return 0;
}
-
Guys, sorry, still not working, either of them...
While I'm here, could I ask how to declare a char with unlimited length ?
-
Code:
outfile >> name;
outfile >> "\n";
outfile >> age;
outfile >> "\n";
outfile >> location;
outfile >> "\n";
outfile >> birthplace;
Wrong operator. Use << for ofstream, not >>.
There's no such thing as a char with unlimited length... nor is there a practical use for one. Oh, and try the code from my last post, I had copied the wrong code... it's fixed now.