file text problem!!! Solution needed!!!

Sep 6, 2013 at 12:18pm
once i enter the second name... but the file only contain one name which is the last one i have entered... i want to save every name i have entered...

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
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main ()
{
	int option;
	string name,line;	
	
	do {
	cin>>option;
	cin.clear();
    	cin.ignore();
		
		if (option == 1)
		{
			cout<<"Enter name:";
			getline (cin,name);

			ofstream myfile ("example2.txt");
			if (myfile.is_open ())
			{
				myfile<<"Name"<<endl;
				myfile<<name<<endl;
		                myfile.close();}
		}
		
		else if (option == 2)
		{
			ifstream myfile ("example2.txt");
			if (myfile.is_open())
			{
				getline (myfile,line);
				cout<<line<<endl;
				myfile.close();}
		}
		else if (option == 3)
		{
			exit(EXIT_FAILURE);
		}
	}while (option != 3);

return 0;
}
Sep 6, 2013 at 1:01pm
Each time the user enters a new name, you write over the old file. This is because you are creating the file with the same again and again. Rather declare the ofstream myfile ("example2.txt") outside the do while loop and that should work.
Sep 6, 2013 at 2:37pm
Well, there is two ways that are relatively easy that you can do to avoid this.

Option 1 would be to look here: http://www.cplusplus.com/reference/fstream/ofstream/open/

basically using something like this will keep you from overwriting the file.
1
2
std::ofstream myfile;
myfile.open("mytextfile.txt", std::ofstream::out | std::ofstream::app);


The second option is, if you already have knowledge of arrays, is just to add all the names to an array and at the end just loop through the array outputting the information to the file but that would also require you to restructure your program.

Edit:: Also your option number two is only going to read the first line in the file, not sure if that was the intent or not.
Last edited on Sep 6, 2013 at 3:02pm
Sep 8, 2013 at 12:30pm
BardaTheHobo...
can you give me a brief example of array based on my code?
Sep 8, 2013 at 9:04pm
Info on arrays: http://www.cplusplus.com/doc/tutorial/arrays/

If I were you I would make a new option for getting information while making option 1 for saving, or the other way around, just keep them separate, like I said it would require you to restructure your program a bit.

anyways for putting info into an array using your current system of if statements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string foo[25];
int i = 0; //Used to access elements in the array, remember arrays start at 0
if (option == 0 )
{
    if (i < 25) //Used to keep from going over array size
    {
        cout << "Enter Info: ";
        cin >> foo[i];
         i++; //increment i by one so the next time it will store info in the next element
    }
    else
     {
        cout << "No More room" << endl;
     }
}


Then for saving to a file you can use a for loop to iterate through the array storing all of the info into the text file at once
ex:
1
2
3
4
5
6
int size_of_array = sizeof(foo) / sizeof (foo[0]); //get the size of the array for the for loop
//you can just use the i from earlier in this program, just showing how to get the size for future reference
for (int j = 0; j < size_of_array; j++)  // or j < i if you prefer in this program
{
  file >> foo[j];
}

More info on for loops if you need: http://www.cplusplus.com/doc/tutorial/control/
Last edited on Sep 8, 2013 at 9:10pm
Topic archived. No new replies allowed.