Heloo

I have a problem I want to write multiple person
and I want to save in output file
and then I want to take from output what I have write


this is my code

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
68
69
70
71
72
73
74
75
76
77
  #include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

class Persoana
{
public:
	Persoana(string n, string p, int v)
	{
		setName(n);           \\name
		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 << "\Name:" << name << "\nPrenume:" << Prenume << "\nVarsta:" << Varsta << endl;
		return os.str();
	}


private:

	string name;
	string Prenume;
	int Varsta;
};






int main()
{
	
	
	string name;
	string Prenume;
	int Varsta;
	ofstream output;
	output.open("marius.txt", ios::end);

	

	cout << "Numele: ";
	cin >> name;
	cout << "Prenumele: ";
	cin >> Prenume;
	cout << "Varsta: ";
	cin >> Varsta;
	cout << "Numele Prenumele si varsta \n" << endl;
	cout << "\Name:" << name << "\nPrenume:" << Prenume << "\nVarsta:" << Varsta << endl;



	output.close;
	system("pause");
	return 0;
}
output.open("marius.txt", fstream::app, ios::end);

Something like that
You probably want to take a look at:

http://www.cplusplus.com/doc/tutorial/files/
thx Axarator
If I want to write this name Axel Lastname Paul age 20
Is there a problem with it still?
I want to create a person like this

name:Alen
Lastname:Peter
Age:20

Check this pastebin (Some things are broken but my name thing works) https://pastebin.com/DjtwLZ1E
Last edited on
I must worck with classes
Your code seems fine to me.
yes but I want to be predefinite name lastname age
I'm also a beginner, I am sorry I can't help.
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
this is my new code
I want to save in output file and then I want to call the output txt

Last edited on
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.
Topic archived. No new replies allowed.