- #1
tebes
- 39
- 0
Homework Statement
How do I retreat the last entered data from .txt file ?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void menu(int *);
void option(fstream &, int *);
int main()
{
int a, p;
while(a== 1||2||3||4||5 )
{
menu(&a);
fstream myfile;
myfile.open("music.txt" , ios::app | ios::out);
myfile.close();
p=a;
option(myfile, &p);
}
system("pause");
return 0;
}
void menu(int *e)
{
cout<<"\n\nMusic index list \n"
<<"---------------- \n"
<<"1. Add new song title to index list file \n"
<<"2. Print most recently added song title in index list file \n"
<<"3. Print all the items from the index list file \n"
<<"4. Delete all of the items on the index list file \n"
<<"5. Quit \n\n";
cin>>*e;
cout<<endl<<endl;
}
void option(fstream &myfile, int *p)
{
char ch;
string line;
int pass;
switch (*p)
{
case 1:
{
myfile.open("music.txt", ios::app | ios::out);
cout<<"Please end the title with a period . \n";
cout<<"What is the new title ?\n ";
cin.get(ch);
while (ch != '.')
{
myfile.put(ch);
cin.get(ch);
}
myfile.put(ch);
myfile.close();
break;
}
case 2:
{
myfile.open("music.txt", ios::out); //Need to print the last song title from the file
myfile.close();
break;
}
case 3:
{
cout<<"My music: \n";
myfile.open("music.txt");
if (myfile.is_open())
{
while (myfile.good())
{
getline (myfile, line);
cout<<line<<endl;
}
myfile.close();
}
else cout<<"Unable to open file.\n";
break;
}
case 4:
{
cout<<"Warning, confirm to delete.\n"
<<"Please press 1. \n";
cin>>pass;
if(pass==1)
{
myfile.open("music.txt", ios::out);
cout<<"Data deleting...\n";
myfile.close();
}
else
cout<<"Try again.\n";
break;
}
case 5:
{
cout<<"warning, program to be closed.\n";
exit(0);
}
}
}
Last edited by a moderator: