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.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace 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.
//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
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
/* This program stores and lists the titles of cds that are
input by a user. */
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
usingnamespace 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;
}