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;
}

Your work doesn't impressed me:

>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.
Did you bothered to open your text book or open a search engine and search it?
RTFM and STFW

>Is there a better way of doing this than what I am thinking of doing below?
Your code is actually doing nothing. Anyone who knows how to print the screen using cout and use a if statement can write that code. Your code doesn't involve even the header file need for File IO. This shows that you have not devoted enough time at your lectures/Book/internet.

Another advice. Please read http://www.catb.org/~esr/faqs/smart-questions.html to get spontaneous replies to your queries.
BTW, for the answer : click here

Ahem :-O

I said that this is all new territory for me, how can I be more clear. you dont learn if you dont ask questions.

And as also said, I have not attempted to write the parts I am asking about because I wanted to hear a better way of doing it before I went & wrote the thing to find out there is a better way.

But thank you for the link

So far this is what I have although the open file is not working & I am strugling to store the information i need in arrays. Any advice? :)

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

void read_file(int temp_array);

int info(int temp_array[], int a[][n]);

int playlist();


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;
}

void read_file(int temp_array)
{
	ifstream infile ("music.txt");

	for ( int i = 0 ; i < num_of_students; i++)
	{
		infile >>  temp_array[q].fname;
		infile >> temp_array[q].lname;
		infile >> temp_array[q].IDNum;
		infile >> temp_array[q].DOB;
		infile >> temp_array[q].GPA;
	}

	infile.close();
}

int info(int temp_array[], int a[][n])
{
	int n = 10, sum = 0, i;

	for (i = 0; i < n; i++)
		sum = sum + a[i];

	cout << "sum = " << sum << endl;
}

int playlist()
{

}

>how can I be more clear. you dont learn if you dont ask questions.
You learn more when you read a book and then ask questions.

>I have not attempted to write the parts I am asking about because I wanted to
>hear a better way of doing it before I went & wrote the thing to find out there is
>a better way.
So you want short-cuts. To be a good programmer, you need to practice. Until you go wrong and dig the problem yourself, you won't able to be a good programmer.

Smooth Roads don't make winners

Applies the most to programming.

>But thank you for the link
But I am afraid you did not read it properly( this is reflected in your last post)

>So far this is what I have although the open file is not working & I am strugling
>to store the information i need in arrays. Any advice?
Yes, an advice. There is no point in hitting and trying when you don't know the concept behind what you are doing. My advice is: buy a decent book about programming (the best is Accelerated C++) or read some free books/tutorial online (the best book would be Thinking In C++ by Bruce Eckel which is both free and fantastic) and then try out. Had you read the books before, you wouldn't be asking such questions because they teach you all this in a boo/tutorial.
Why I am saying is because the quality of your code you posted is very bad. You are not even sure about the program flow.
(I can sense that may be you were a programmer in some other language and now has migrated to c++)
Anyways, my advice is STOP, and READ A BOOK.

You problem is very trivial.

In case, after sidds post you are still reading these forums instead of books, here's a piece of my mind.

Your original idea isnt half-bad. Though, instead of reading the file twice, I suggest you just read it through once and save all the needed data in array(s). Then manipulate it.

In "your" read_file function, converting the file into a stream should work. The rest looks like a copy-pasta from another thread... Try to think what kind of data, how much data and from where you are saving it from.

Looks like you're also doing the COMP115 assignment, or something similar.

Didn't fully read thread, but one of your targets for the code was to find numbers in the line, however if you've looked through music.txt, you'll realise there's a few songs/artists which have numbers in them (at the bottom, 10cc, if you count the 10, you're kinda stuffed)

As for the people recommending us to read a book, we've been using C++ programming: From problem analysis to program design by D.S. Malik.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.