Hey everyone, I need help with my program it is an ipod simulator that goes like this:

/* FUNCTION - void initaPod
 Initialize all the slots to blank and 0 size memory*/
void initaPod ();

/* FUNCTION - int addSong
 attempts to add a new song to the aPod
returns a 0 if successful
returns -1 if not enough memory to add the song
 returns -2 for any other error (such as a blank title or artist)*/
int addSong (string newTitle, string newArtist, int size);

/* FUNCTION - int removeSong
attempts to remove a song from the aPod
returns 0 if successful
returns -1 if nothing is removed */   
int removeSong (string title);

/* FUNCTION - void clearMemory
clears all the songs from memory 
*/void clearMemory();

/* FUNCTION - int getRemainingMemory
* returns the amount of memory left in your aPod that is available for use
*/ int getRemainingMemory();

/* FUNCTION - void showSongList   
    prints the current list of songs in order from first to last to standard output
    format - slot #, Title, Artist, size in MB (one song per line)
    print "Empty" for any slots that do not contain a song*/
    void showSongList ();

/* FUNCTION - void shuffle
   shuffles the songs into random order
will do nothing if there are less than two songs in the current list
*/
void shuffle ();

The problem is with my "showSongList" function because I have an input file that reads the Title(string), Artist(string), and Size(# of mb song is worth, int) and then displays the contents to the screen. I read the contents of the file into an array of structs then print it to the screen. I think the problem is when I try to call the function in "int main" or something about the arguments I try to pass to the array. Can someone help me? This is the code so far:

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

using namespace std;

struct Songs{
     string title;
     string artist;
     int size;
     };
     
void showSongList(Songs title[],int &Num_Songs);

int main(){
    
const int Num_Songs = 8;
const int Memory_Size = 25;
string title;
string artist;
int size;


showSongList();


system("pause");
return 0;
} void showSongList(Songs title[],int &Num_Songs) {
   ifstream inFile;
   inFile.open("aPod.txt");
  
   string title;
   string artist;
   int size;
   
   inFile >> title;
   while (inFile) {
      inFile >> artist;
      inFile >> size;
      
cout << title << artist << size <<endl;

      title[Num_Songs].title = title;
      title[Num_Songs].artist = artist;
      title[Num_Songs].size = size;
      
      Num_Songs++;
      
      inFile >> title;
   }      
        
}

Recommended Answers

All 5 Replies

Well, have you ever compile this source? If so, did you ever read compiler diagnostic messages?

You declare showSongList as a function with two parameters but you are trying to call it without arguments. Where is a definition of an array of Songs? Why you declare TWO different title entities in the function showSongList - the 1st as an array parameter and the 2nd as a local variable of string type?

Thanks for the suggestion ArkM about the declaring two "title" entries in the program. Now, I am trying to figure out how to call the function "showSongList". This is the code:

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

using namespace std;

struct Songs{
     string title;
     string artist;
     int size;
     };
     
void showSongList(Songs titles[],int &Num_Songs);

int main(){
    
const int Num_Songs = 8;
const int Memory_Size = 25;
string title;
string artist;
int size;


showSongList(Songs titles[], int &Num_Songs);


system("pause");
return 0;
}

 void showSongList(Songs titles[],int &Num_Songs) {
   ifstream inFile;
   inFile.open("aPod.txt");
  
   string title;
   string artist;
   int size;
   
   inFile >> title;
   while (inFile) {
      inFile >> artist;
      inFile >> size;
      
cout << title << artist << size <<endl;

      titles[Num_Songs].title = title;
      titles[Num_Songs].artist = artist;
      titles[Num_Songs].size = size;
      
      Num_Songs++;
      
      inFile >> title;
   }      
        
}

These are the errors I get when I try to call "showSongList" function:

error: expected primary-expression before "titles"
error: expected primary-expression before "int"

You must pass real (defined) objects as function arguments, that's a function call:

showSongList(titles,Num_Songs);

A function call is not the same thing as a function declaration. Alas, you ignored one of my question in the previous post, You don't declare array named titles (or what else) in the main function. The construct showSongList(titles,Num_Songs) means: call the function showSongList and pass as the 1st argument of this call the array of Songs named titles and as the 2nd argument a reference to the int variable named Num_Songs. It's impossible: no such array in the main function or in the source text before...

The next error (you will get this diagnostic message after call correction): showSongList wanted a reference to int as the 2nd parameter. You can't pass a reference to const int Num_Songs because it's possible to get const reference to int for a const variable (const means read-only). The function modifies the 2nd argument value via this reference parameter - that's why the language forbids const var to non-const reference conversion.

Yet another defect: suppose you declare an array of Songs in the main, for example:

int main()
{
    ....
    int songcnt; 
    Songs album[100];
    ...
    showSongList(album,songcnt);
    ...

What happens if you have 101 (or more) songs info in the file aPod.txt? Right, you will get "array overflow" - the program overwrite memory allocated for the array album. Pass max number of array elements as the next parameter or use std::vector instead of a plain array...

I think I understand what you are saying ArkM. So, should I create an array called "titles" or pass another variable to the function call?

It does not matter what's a name of that array. A function call binds any proper type argument to the correspondent parameter. Try to understand this mechanics.
If you know std::vector standard library containers, better rewrite your code with std::vector<Songs> (instead of Songs[] array).

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.