In C++, No pointers or vectors, Create a program that will read a file and load
ID: 3733423 • Letter: I
Question
In C++, No pointers or vectors, Create a program that will read a file and load data by using structs and arrays. Also Song struct has members of type Artist and Time.
C++ structs
Artist: Name(string), CountryOfOrigin(string)
Time: Minutes(int),Seconds(int)
Song: Title(string),Artist(Artist), Length(Time)
Functions:
LoadSongDataFromFile
Loads song data from a user specified file. This function should first prompt the user to enter a filename. Next it should open the file that the user specified. After that it should read in the song data from that file into the song array parameter.
// Function prototype
void LoadSongDataFromFile(Song s[])
ShowSongData
Writes ALL data inside the song array to standard output.
// Function prototype
void ShowSongData(Song s[])
GetTotalTime
This function should iterate through the array and calculate the total time for all songs stored in the array.
Important: The Time that is returned must have seconds less than 60 or it will be marked as incorrect.
// Function prototype
Time GetTotalTime(Song s[])
Create the structs as listed above.
Create the functions as listed above.
Create an array of Song in main that has a size of 5.
You need to create a loop in main that will show a menu like the following below.
Song Program
---------------
1 - Load song data from file
2 - Show all song data
3 - Show total time
4 - Exit
Enter Choice:
Important: You must call the functions that you wrote to do what the user wants done for the given menu selection.
Song Input File Format
Title
ArtistName
CountryOfOrigin
Minutes
Seconds
…
Title
ArtistName
CountryOfOrigin
Minutes
Seconds
Song Sample Input Data File (example)
In The Air Tonight
Phil Colins
United States
3
30
Night Of Fire
Intiatal D
Europe
2
38
Gucci Gang
Gucci Gang
USA
3
20
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
using namespace std;
int size = 0;
struct Artiststruct{
string Name;
string CountryOfOrigin;
};
struct Time{
int Minutes;
int Seconds;
};
struct Song{
string Title;
Artiststruct Artist;
Time Length;
};
// C++ structs
// Artist: Name(string), CountryOfOrigin(string)
// Time: Minutes(int),Seconds(int)
// Song: Title(string),Artist(Artist), Length(Time)
// Functions:
// LoadSongDataFromFile
// Loads song data from a user specified file.
// This function should first prompt the user to enter a filename.
// Next it should open the file that the user specified.
// After that it should read in the song data from that file into the song array parameter.
// // Function prototype
// In The Air Tonight
// Phil Colins
// United States
// 3
// 30
// Title
// ArtistName
// CountryOfOrigin
// Minutes
// Seconds
void LoadSongDataFromFile(Song s[]){
cout << "Enter the filename";
string filename;
cin >>filename;
string line;
ifstream myfile (filename.c_str());
int index =0;
if (myfile.is_open() && !myfile.fail() )
{
while ( getline (myfile, line) )
{
s[index].Title = line;
getline (myfile, line);
s[index].Artist.Name = line;
getline (myfile, line);
s[index].Artist.CountryOfOrigin = line;
getline (myfile, line);
stringstream ssWordsBuf (line);
ssWordsBuf >> s[index].Length.Minutes;
getline (myfile, line);
stringstream ssWordsBuf1 (line);
ssWordsBuf1 >> s[index].Length.Seconds;
index++;
}
myfile.close();
}
size = index;
}
// ShowSongData
// Writes ALL data inside the song array to standard output.
// // Function prototype
void ShowSongData(Song s[]){
for(int i=0; i<size; i++){
cout << "Title: " << s[i].Title << " " << "Name: " << s[i].Artist.Name << " " << "Country: " << s[i].Artist.CountryOfOrigin
<< "Minutes: " << s[i].Length.Minutes << " " << "Seconds: " << s[i].Length.Seconds <<endl;
}
}
// GetTotalTime
// This function should iterate through the array and calculate the total time for all songs stored in the array.
// Important: The Time that is returned must have seconds less than 60 or it will be marked as incorrect.
// // Function prototype
Time GetTotalTime(Song s[]){
int totalMinutes =0;
int totalSeconds =0;
for(int i=0; i<size; i++){
totalMinutes += s[i].Length.Minutes;
totalSeconds += s[i].Length.Seconds;
}
totalMinutes += int(totalSeconds/60);
totalSeconds = totalSeconds%60;
struct Time totalTime;
totalTime.Minutes = totalMinutes;
totalTime.Seconds = totalSeconds;
return totalTime;
}
void showMenu(){
cout << "Select one of the folllowing options" <<endl;
cout << " 1 - Load song data from file" <<endl;
cout << "2 - Show all song data" <<endl;
cout << "3 - Show total time" <<endl;
cout << "4 - Exit" <<endl;
cout << "Enter Choice:";
}
int main(){
struct Song s[10] ;
struct Time totalTime;
int option;
do{
showMenu();
cin >> option;
if (cin.fail())
{
cout << "ERROR -- You did not enter an integer";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
continue;
}
switch(option) {
case 1 :
LoadSongDataFromFile(s);
break;
case 2 :
ShowSongData(s);
break;
case 3 :
totalTime = GetTotalTime(s);
cout << "Total Time is " << totalTime.Minutes << ":" << totalTime.Seconds <<endl;
break;
case 4 :
cout << "Exiting" <<endl;
break;
default :
cout << "Invalid option" << endl;
}
} while(option != 4);
return 0;
}