Jul 22, 2015 at 9:53am UTC
output.open("marius.txt" , fstream::app, ios::end);
Something like that
Jul 22, 2015 at 9:56am UTC
thx Axarator
If I want to write this name Axel Lastname Paul age 20
Jul 22, 2015 at 10:02am UTC
Is there a problem with it still?
Jul 22, 2015 at 10:08am UTC
I want to create a person like this
name:Alen
Lastname:Peter
Age:20
Jul 22, 2015 at 10:11am UTC
Last edited on Jul 22, 2015 at 10:12am UTC
Jul 22, 2015 at 10:13am UTC
I must worck with classes
Jul 22, 2015 at 10:16am UTC
Your code seems fine to me.
Jul 22, 2015 at 10:29am UTC
yes but I want to be predefinite name lastname age
Jul 22, 2015 at 10:42am UTC
I'm also a beginner, I am sorry I can't help.
Jul 22, 2015 at 11:01am UTC
Try compiling your broken version and see if you can see how the error messages allowed me to fic your program in a couple of minutes!
Not sure how you want to use your file. As you were trying to open with ios::end (used for seeking) I switched it to ios::ate, but ios::app might be better. See documentation on fstream::open for details.
Also, I'd prob use
ofstream output("marius.txt" , ios::ate);
rather than
1 2
ofstream output;
output.open("marius.txt" , ios::ate);
Andy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class Persoana
{
public :
Persoana(string n, string p, int v)
{
setName(n); //name <= comments were wrong way round (\\ rather than //)
setPrenume(p); //forename
setVarsta(v); //age
}
void setName(string x)
{
name = x;
}
void setPrenume(string x)
{
Prenume = x;
}
void setVarsta(int x)
{
Varsta = x;
}
string get()
{
ostringstream os;
os << "\nName:" << name << "\nPrenume:" << Prenume << "\nVarsta:" << Varsta << endl;
// <= started with "\N rather than "\nN and compiler doesn't know what \N means (just \n)
return os.str();
}
private :
string name;
string Prenume;
int Varsta;
};
int main()
{
string name;
string Prenume;
int Varsta = 0; // <= good habit to init all vars!
ofstream output;
//output.open("marius.txt", ios::end); // ios::end if for seeking!
output.open("marius.txt" , ios::ate); // ate = at end (to append)
cout << "Numele: " ;
cin >> name;
cout << "Prenumele: " ;
cin >> Prenume;
cout << "Varsta: " ;
cin >> Varsta;
cout << "Numele Prenumele si varsta \n" << endl;
cout << "\nName:" << name << "\nPrenume:" << Prenume << "\nVarsta:" << Varsta << endl;
// <= started with "\N rather than "\nN and compiler doesn't know what \N means (just \n)
output.close(); // missing () -- was just output.close
system("pause" );
return 0;
}
Last edited on Jul 22, 2015 at 11:05am UTC
Jul 22, 2015 at 11:19am UTC
this is my new code
I want to save in output file and then I want to call the output txt
Last edited on Jul 23, 2015 at 7:39am UTC
Jul 22, 2015 at 11:32am UTC
You need to read up on classes (there are 2 pages on this site).
You don't initialise member variables in the way that you have, and your set methods aren't setting anything.
Read Andy's post again.