Stuffing info into a 2D array

Hello,

I am writing a music library program (like I-Tunes) in which I have to input a exe file with commas into a 2D array. After getting rid of the commas, I should be able to list the genres, songs and artists as outputs with a simple switch statement and some functions.
The only problem is, I have no idea on how to get rid of commas within the file and stuff it into a 2D array. Please help !!
Could you explain more and post some of the code you have ?
I do not have any code so far but here are the requirements:

Your assignment is to create a program that will read a txt file of music and allow the user to see what He/She might have in their list of music.
Hard code your file path for development purpose but ask the user if they want to enter a path or use the default file. The program should read the categories of the listings by reading the first line of the file.

For Example:
Please select from the following list
--------------------------------------------------------
1) Artist
2) Song
3) Genre
4) Exit
If the user selects the Artist, list all the artist only once and allow the user to select the artist
Example:
Artist
----------------------------------------
1) Van Halen
2) Miles Davis
3) Barry Wordsworth & BBC Concert Orchestra

If the user selects 1, show all the Van Halen in the list.
Van Halen Rock
-------------------------------------
The Best Of Van Halen, Vol. I
Van Halen III
Van Halen

If the user selects Genre, show the entire genre List
Please Select a Genre
--------------------------------------
1) Pop
2) Rock
3) Jazz
4) Country
5) Movie Soundtrack
6) ……..

When the user selects a Genre the songs and artist with that Genre will then be listed
Jazz
----------------------------------------------------------------------
Take 5 Buddy Miles
Worlds Aaron Goldberg
Quiet Songs Aisha Duo

At any time in a menu ‘M’ should send the user back to the main menu.
And in the Main menu 4 or ‘E’ to exit
I have written a small program for you to demonstrate how to read a line from the file, separated with comma's into a vector.

I hope you would carry on from here. this is jut for a demo and may not work entirely.
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>


void maketoken(std::string& line, std::vector<std::string>& tokens)
{
	while(line.length())
	{
		int pos = line.find(',');
		if(pos == std::string::npos)
		{
			//this may be the last token
			tokens.push_back(line);
			break;
		}
		tokens.push_back(line.substr(0, pos));
		line.erase(0, pos + 1);
	}
}

int main()
{
	std::ifstream ifs("testfile.txt");
	if(!ifs.good())
		return 0;

	std::string line;
	std::vector<std::string> tokens;

	while(!ifs.eof())
	{
		std::getline(ifs, line);
		maketoken(line, tokens);
		//do something with tokens.

		//after using them, clear the vector for next iteration
	}

        ifs.close();

	return 0;
}


Last edited on
I cannot use vectors,pointers or tokens, any way to just use 2D arrays ?? ??
Last edited on
Do the same, use char* version of getline. this would give you a line in a 1D array.
Then take out substrings (seperated by comma), you can do find using the 'C' api's and then put them in a 2D array.

you can declare a 2D array like this"

char **ppline; or may be like this
char *pline[255];
assuming that you have at max 255 lines.
Thanks I will try to work on it. I will comment again if needed.
Topic archived. No new replies allowed.