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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//function prototypes
void saveCd();
void displayCds();
int main()
{
int menuOption = 0;
do //begin loop
{
//display menu and get option
cout << endl;
cout << "1 Enter CD information" << endl;
cout << "2 Display CD information" << endl;
cout << "3 End the program" << endl;
cout << "Enter menu option: ";
cin >> menuOption;
cin.ignore(100, '\n');
cout << endl;
//call appropriate function
//or display error message
if (menuOption ==1)
saveCd();
else
if (menuOption ==2)
displayCds();
//end if
//end if
} while (menuOption != 3);
return 0;
} //end of main
//*****function definitions*****
void saveCd()
{
//writes records to a sequential access file
string cdName = "";
string artistName = "";
//create file object and open the file
ofstream outFile;
outFile.open("cds.txt", ios::app);
//determine whether the file is opened
if (outFile.is_open())
{
//get the CD name
cout << "CD name (-1 to stop): ";
getline(cin, cdName);
while (cdName != "-1")
{
//get the artist's name
cout << "Artists name: ";
getline (cin, artistName);
//write the record
outFile << cdName << '#'
<< artistName << endl;
//get another CD name
cout << "CD name (-1 to stop): ";
getline(cin, cdName);
} //end while
//close the file
outFile.close();
}
else
cout << "File could not be opened." << endl;
//end if
} //end of saveCd function
void displayCds()
{
//displays the records stored in the cds.txt file
string cdName = "";
string artistName = "";
//create file object and open the file
ifstream inFile;
inFile.open("cds.txt", ios::in);
//determine whether the file was opened
if (inFile.is_open())
{
//read a record
getline(inFile, cdName, '#');
getline(inFile, artistName);
while (!inFile.eof())
{
//display the record
cout << cdName << ", " <<
artistName << endl;
//read another record
getline(inFile, cdName, '#');
getline(inFile, artistName);
} //end while
//close the file
inFile.close();
}
else
cout << "File could not be opened." << endl;
//end if
} //end of displayCds function
|