First, you define several methods with return types, and when you implement them, you have no return statements.
CODE
string SongDatabase::GetGenre(string Genre)
{
genre = Genre;
}
Second, while it's not a functional issue, these methods have names containing "Get", but they appear to be mutators rather than accessors. Should these be renamed using "Set"?
Third, when defining non-POD types as parameters, prefer passing by pointer or reference and especially pointer-to-const or reference-to-const.
Fourth, your main doesn't have a return statement.
Once I corrected these issues, it seems to work fine.
CODE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class SongDatabase
{
public:
void SetArtist(const string &Artist);
void SetSong(const string &Song);
void SetGenre(const string &Genre);
void SetPrice(float Price);
void SetCount(int PurchaseCount);
void purchaseSong();
private:
string artist;
string song;
string genre;
float price;
int purchaseCount;
};
void SongDatabase::SetArtist(const string &Artist)
{
artist = Artist;
}
void SongDatabase::SetSong(const string &Song)
{
song = Song;
}
void SongDatabase::SetGenre(const string &Genre)
{
genre = Genre;
}
void SongDatabase::SetPrice(float Price)
{
price = Price;
}
void SongDatabase::SetCount(int PurchaseCount)
{
purchaseCount = PurchaseCount;
}
int main()
{
int max = 200;
SongDatabase Song[max];
ifstream infile;
ofstream outfile;
string songArray[5];
// Stray semicolon?
//;
const char* str_price;
const char* str_count;
float price;
int count;
infile.open("exe/songlist.txt");
// Added test to verify that the file opened successfully.
if (!infile) {
cerr << "Failed to open file\n";
return -1;
} // if (!infile)
int a = 0;
while(!infile.eof())
{
// Prefer pre-increment to post-increment.
for (int i = 0; i < 5; ++i)
{
getline(infile, songArray[i], ',');
}
str_price = songArray[3].c_str();
str_count = songArray[4].c_str();
price = atof(str_price);
count = atoi(str_count);
Song[a].SetArtist(songArray[0]);
Song[a].SetSong(songArray[1]);
Song[a].SetGenre(songArray[2]);
Song[a].SetPrice(price);
Song[a].SetCount(count);
// Prefer pre-increment to post-increment.
++a;
}
infile.close();
return 0;
}
Finally, in C you must declare all local variables in a function prior to the first code statement. Since in C++, this is no longer necessary, it is generally considered good practice to define local variables as near as practical to their first use.
CODE
int main()
{
int max = 200;
SongDatabase Song[max];
ifstream infile;
infile.open("exe/songlist.txt");
// Added test to verify that the file opened successfully.
if (!infile) {
cerr << "Failed to open file\n";
return -1;
} // if (!infile)
int a = 0;
while(!infile.eof())
{
string songArray[5];
for (int i = 0; i < 5; ++i)
{
getline(infile, songArray[i], ',');
}
const char *str_price = songArray[3].c_str();
const char *str_count = songArray[4].c_str();
float price = atof(str_price);
int count = atoi(str_count);
Song[a].SetArtist(songArray[0]);
Song[a].SetSong(songArray[1]);
Song[a].SetGenre(songArray[2]);
Song[a].SetPrice(price);
Song[a].SetCount(count);
// Prefer pre-increment to post-increment.
++a;
}
infile.close();
return 0;
}