Ok, I have been busting tail trying to get this simple project done. Lesson learned is do not take a break from course work during the summer, my lack of fundamentals at this moment is tear me apart. I am determined to finish this project ahead of time to finish the rest of my other course work.
The current project broken down shortly
Using previous teachings of arrays, structs, reference parameters, pointers we are to construct a program doing the following
Using a array of structs, we have a file "song.dat" and the text inside of the file looks like such
Title
Artist
Genre
Track Time
This will count as one item we can not have more than 10 items
upon start up we must read from a "song.dat" file and then provide the user with a choice of options but Ill start off with what I got
I created a function -load_file which should read from song.dat and then when later called clear the current array and read from a file of the users choice
Then I will print out the array
This is the code I have...tis very messy/poor programming habits...sorry
My main problems, it seems to read from "song.dat" at execution
and when I load the next file i THINK the old array clears but it just prints out 0 0 0 0
Edit: I added i = 0; after the file opens in the load function and read correctly from the next file
But I am sure there is more efficent ways than mine
In song.dat there are 4 items the next file of my choice there is 3
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 10;
struct music{
string title, artist, genre;
int iTrackTime;
};
int load_file(char* file, music song[MAX], int i);
void print_song(music song[MAX], int i);
int main()
{
int choice;
int j;
int i;
music song[MAX];
char new_file[256];
i = load_file("song.dat", song, i); //Read from song.dat file upon execution of program
do{
cout<<endl;
cout<<"Load a new file of songs - 1\n";
cout<<"Print current array of songs - 2\n";
cout<<"Enter a number for a task: ";
cin>>choice;
cout<<endl;
switch(choice){
case 1:
cout<<"Enter the new file of songs: ";
cin>>new_file;
load_file(new_file, song, i);
break;
case 2:
print_song(song, i);
default:
break;
}
}while(choice != 10);
system("Pause");
return 0;
}
int load_file(char* file, music song[MAX], int i)
{
string dummy;
ifstream fin;
if(i !=0){
for(int j=0; j<i; j++){
song[j].title.clear();
song[j].artist.clear();
song[j].genre.clear();
song[j].iTrackTime = 0;
}
}
fin.open(file);
while(!fin.eof() && i<MAX){
getline(fin,song[i].title);
getline(fin,song[i].artist);
getline(fin,song[i].genre);
getline(fin,dummy); //Read the track time as a string so it grabs the newline
song[i].iTrackTime = atoi(dummy.c_str());//Now conver the string into a integer
i++;//A count of items in file
}
fin.close();
return(i);
}
void print_song(music song[MAX], int i)
{
for(int j=0; j<i; j++)
{
cout<<song[j].title
<<song[j].artist
<<song[j].genre
<<song[j].iTrackTime<<endl;
}
}
Also when I change the order the variable i and j the program crashes
Before
int main()
{
int choice;
int j;
int i;
music song[MAX];
After
int main()
{
int choice;
int i;
int j;
music song[MAX];
The program crashes...
-_-'