How can I save things ona text file without overwriting them (Code included)?

Hi there,

I am writing a program which requests the name and the age of the user, along with the name of the city they would like to destroy. I want to get around the problem of the values being over written whenever I run the program. I want to save "city" in a different file but I want it to be a list.

I want to store city in an array, which will then be listed on another text file.

Here is the 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
#include <iostream>
#include <fstream>
using namespace std;
void orderRequest(string name, int age, string city);
void saveData(string city);

int main()
{
    string name;
    int age;
    string city;
    cout << "Please enter your name and your age below \n\n";
    getline(cin, name);
    cout << "\n" << endl;
    cin >> age;
    cout << "\n" << endl;
    cout << "Thank you, now please enter the name of the city you would like to destroy" << endl;
    cin >> city;
    
    if(city == "london")
    {
        cout << "Go to hell!!!" << endl;
    }
    else{cout << "It's soon to be a nuclear wasteland" << endl;}
    
    orderRequest(name,age,city);
    saveData(city);
    return 0;
}

void orderRequest(string name, int age, string city)
{
    ofstream bombcityinfo("ForNSA.txt");
    bombcityinfo << name << "\n" << age << "\n" << city << endl;
    bombcityinfo.close();
}

void saveData(string city)
{
    ifstream takeData("LIST.txt");
    string cityName[1];
    ofstream cityList("LISTC.txt");
    
    int i;
    for(i = 0; i <= 1; i++)
    {
        cityName[i] = {city};
        cityList << city << "\n " << endl;
    }
    cityList.close();
    takeData.close();
}


Hopefully you can see what I am trying to do. Thanks in advance for the help.
closed account (jwkNwA7f)
 
ofstream file ("anything.txt", ios::app);


ios::app puts the text at the end of the file.

Hope this helped!
Topic archived. No new replies allowed.