Hey.

I'm currently working on a project where I'm suppose to read album titles including song names from a file into a 2d array. When I input the cd number, only that record should show up in the console.

E.g. Cd number: 1

Album title

Song 1
Song 2
Song 3
Song 4
Song 5
etc.

This is what I have done so far and I have no clue of where to go from tere. Please bare with me, just started working with c++. I'm using dev-c++.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string music[5][20];
int songnr = 0;
  ifstream song1 ("song.txt");
  if (song1.is_open())
  {
    while (! song1.eof() )
    {
          for (int x = 0; x < 5; x++){
          for (int y = 0; y < 20; y++) {
              cout<<"Song nr: ";
              cin>>songnr;
              if (songnr = 1){
      getline (song1,music[x][y]);
      cout << musik[x][y] << endl;

    }

}
}
}
    song1.close();
  }

  else cout << "Can't open"; 
  
  cin.get();
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 2 Replies

To successfully read a file you need to know exactly how it is organized. If the file is organized with the Album Title on the first line and song titles one per line thereafter like this:

Green Chili's Greatest Hits
How Hot
Dreamy
7 Days and 7 Nights

the using getline() would be appropriate to read the file line by line. The input could be stored in an array of strings like this:

string songs[20];

But the only reason to do so would be if you are trying to learn how to use arrays or if you are planning to use the information after you print it to the screen.

Don't use the return value of eof() to control how many times the loop runs. Use an input statement instead:

string input;
while(getline >> input)

The = operator is the assignment operator, whereas the == opertor is the equals operator. Within an if() you want the == not the =.

You need to increment songnr each time through the loop. If songnr equals zero then input is the album title. If not, then input is a song name.

Hmm, I see.

The data in the file is organized as following:
album song1 song2 song3 song 4 song 5
Album2 song1 song2 song3 song4 song5

Example:
Absolution Apocalypse Please Time Is Running Out Sing For Absolution Stockholm Syndrome
Black Holes And Revelations Take A Bow Starlight Supermassive Black Hole

There should be 5 albums and 20 songs for each album. Each record (album + songs) should be assigned to a number and when prompted only that whole record should show up.
E.g.
int song no;
cout<< "Song no: ";
cin>> song no:;
if song no == 1 Then
Absolution Apocalypse Please Time Is Running Out Sing For Absolution Stockholm Syndrome

I would like to set it up as a single column list in the console, but if difficult or not possible, that would be alright too, as long as the printing works.
E.g.

Absolution
Apocalypse Please
Time Is Running Out Sing
For Absolution
Stockholm Syndrome

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.