What would be the best way of doing this?.....
I have a text file containing a list of music songs that shows the artist, song title & song duration.
So I'm writing a program that opens the text file & reads the duration of each song, adds each song length together to get the total duration of all the songs & prints out the total duration aswell as how many total songs there are in the file.
eg of the txt file layout:
Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
The Angels,Take A Long Line,180,
How do you open a text file in C++?
What function do you use to store a piece of text from the text file as an array/string.
My idea is to:
- open the file music.txt
- use cin.ignore to ignore text but to read/store numbers/integers
- when the function finds a number it stores it
- once all the song durations have been stored, I add them together to get the total playlist duration
- use cin.ignore again to read how many lines of text there are in the file (how many lines of text = how many songs are in the playlist)
Is there a better way of doing this than what I am thinking of doing below?
My coding so far - although I haven't started on what I am trying to do above because I am in very new territory
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string command;
cout << "Command: ";
cin >> command;
if ( command == "quit")
return 0;
else if ( command == "info" || command == "Info")
cout << "Info";
else if ( command == "playlist " || command == "Playlist")
cout << "Playlist";
else cout << "Illegal command: " << command;
return 0;
}