file user input and getting the last line

Apr 15, 2012 at 4:55pm
Im supposed to write a program that asks the user to input a a song title and stores it into a file. the other functions are to print the most recently added song and to show the list of all available songs. so:

Music index
1. add new song to file
2. print most recent song in file
3. display all songs in file
4. quit

I understand to do this i need to use switches and cases. what i need help with is number 1 and 2 in the list above. here is my current 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

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

using namespace std;
using std::string;


int main () {
    
    fstream inputfile;
    string name[4];
    int choice;
    
    do{
         cout<<"\n Music index list";
         cout<< "\n 1. Add a new song to the file";
         cout<< "\n 2. Print the most recently added song in the file";
         cout<< "\n 3. Display all songs in the file";
         cout<< "\n 4.Quit"; 
         cin>> choice;
    
    switch (choice) {
           case 1:
           inputfile.open("music.txt");
           cout<< "What is the name of the song? ";
           for (int x = 0; x <= 4; x++){
               cin>> name[x];
               inputfile << name[x];
               inputfile.close;
           break;
           
           case 2:
                inputfile.open()
                getline (inputfile, name); 
                cout << name << endl;


Am I on the right track?
Apr 15, 2012 at 5:06pm
if
using namespace std;
then no need for
using std::string;
Apr 15, 2012 at 5:13pm
First problem
string name[4];
you planned to support only four entries in the file.
So if you want to keep it that way than the last song is always going to be
name[3];
Last edited on Apr 15, 2012 at 5:14pm
Apr 15, 2012 at 7:29pm
I'm sorry i made the program more complicated than it needed to be. the file should already exist. how do i go about printing the last line in the file added (my third song)t? this is my new 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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;



int main () {
    
    fstream inputfile;
    int choice;
    inputfile.open("music.txt");
    inputfile << "my first song\n"; 
    inputfile << "my seond song\n";
    inputfile.close();
    
    
    do{
         cout<< "\n Music index list";
         cout<< "\n 1. Add songs to the file";
         cout<< "\n 2. Print the most recently added song in the file";
         cout<< "\n 3. Display all songs in the file";
         cout<< "\n 4. Quit"; 
         cin>> choice;
    
    switch (choice) {
           case 1:
          cout<< "What is the new title?";
           inputfile.open("music.txt", ios::out | ios:: app);
           inputfile<< " my third song\n";
           inputfile.close();
           break;



I dont need to know how to display all songs or quit; those i already have ready to add in as soon as im done figuring out how to get the last line from the file.
Last edited on Apr 15, 2012 at 7:34pm
Apr 20, 2012 at 2:01pm
pseudo code

variable song
infinite loop start
get first line and store in song
if end of file exit
display song


1
2
3
4
5
6
7
8
char song[81];
while(true){
  inputfile.getline(song,80);
  if(inputfile.eof()){
    break;
  }
}
cout << song;
Topic archived. No new replies allowed.