Storing Words in .txt and reading them

I would like to store the titles of a CD and then read them. I have started a program but not sure how to display or make sure it is storing it in the .txt file.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    char names[5][100];
    
    for (int i = 0; i < 5; i++)
    {
        cout << "Title of DVD, Date created, Producer:\n";
        cin.getline(names[i], 99);;
    }

    
    ifstream inFile;
    ofstream outFile;
    char response;
    inFile.open("CD.txt");
    
    if (!inFile.fail()) {
        cout << "A file by the name CD.txt exists.\n"
        << "Do you want to continue and overwrite it\n"
        << " with the new data (y or n): ";
        cin >> response;
        if (tolower(response) == 'n') {
            cout << "The existing file will not be overwritten." << endl;
            exit(1);
        }
    }
    outFile.open("CD.txt");
    if (inFile.fail()) {
        cout << "\nThe file was not successfully opened" << endl;
        exit(1);
    }
    
    cout << "The file has been succesfully opened for writing." << endl;
    
    
    ofstream myfile ("CD.txt");
    if (myfile.is_open())
    {
        myfile << char names [5][100] << endl;
        myfile.close();
    }
    else cout << "Unable to open file";
    
    
    
    return 0;
}
line 9 - You should use a list or a vector instead, using a char array is not the worst idea, but you are using it improperly. For what you need, a vector or a list will function similarly and you wont notice a difference. They have common functions for what you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Must have
#include <string> //use this instead of char array


//One or the other
#include <vector>
or
#include <list>

int main()
{
//One or the other
  std::vector<string> cds;
    //OR
  std::list<string> cds;


//Must haves
  std::string input;
  std::fstream inFile;
  std::ofstream outFile;


line 11 to 15 - you are getting the user input but this approach may work better.

1
2
3
4
5
6
7
  while(true) //
  {
    std::cin >> input; //This gets the whole string
    if( input == "done") //Type "done" to signal that you are done inputting cd's
      break;
    cds.push_back(input);  This pushes the input into the list or vector
  }



Now maybe you just want to see all your input, to debug or just verify it is there

1
2
3
4
for( int i = 0; i < cds.size(); ++i)
{
  std::cout << cds[i] << std::endl;
}



Try that. Your file I/O still needs work, but hopefully this gets you going in the proper direction

:)
Last edited on
Ok I kind of tweaked it a bit however i am unable to use a space in the title of the CD. I know i need to use get line () but am not sure exactly how. The error is in 17-24

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
/* This program stores and lists the titles of cds that are
 input by a user. */

#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

int main ()

{
    vector<string> CD;
    string input;
    
    while(true)
    {
        cout << "What is the title, artist, and realease date of the CD?\n (Type done when finshed listing)" << endl;
        getline(cin, CD);
        if( input == "done") //Type "done" to signal that you are done inputting cd's
            break;
        CD.push_back(input);
    }
    
    
    ifstream inFile;
    ofstream outFile;
    char response;
    inFile.open("CD.txt");
    
    if (!inFile.fail()) {
        cout << "A file by the name CD.txt exists.\n"
        << "Do you want to continue and overwrite it\n"
        << " with the new data (y or n): ";
        cin >> response;
        if (tolower(response) == 'n') {
            cout << "The existing file will not be overwritten." << endl;
            exit(1);
        }
    }
    outFile.open("CD.txt");
    if (inFile.fail()) {
        cout << "\nThe file was not successfully opened" << endl;
        exit(1);
    }
    
    cout << "The file has been succesfully opened for writing." << endl;
    
    
    ofstream myfile ("CD.txt");
    if (myfile.is_open())
    {
        myfile << &CD << endl;
        myfile.close();
    }
    else cout << "Unable to open file";
    
    
    for( int i = 0; i < CD.size(); ++i)
    {
       cout << CD[i] << endl;
    }
    
    
    return 0;
    
}
getline(cin, CD);
should be
getline(cin, input);
Topic archived. No new replies allowed.