Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Forum » x-Forum » Question

Begineer, Open a file of songs Must ask the user for a fil

Must ask the user for a filename
Assume the file is in the local directory (i.e. no drive letters or folders)
Read the file into an array (same array as Create a new list of songs)
Must tell the user if the filename does not exist OR if the file has been successfully read
Return to the main menu after telling the user what happened

I started the code 

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;


struct individualSong                          //using struct class to define information in vector/array, MUST BE AT TOP OF PROGRAM

{
    string songName;
    string songArtist;
    int rating;

};


void addSong(vector<individualSong>& endofList, string songName, string songArtist, int rating)   // function to keep songs/information in order

{

    individualSong songTemp;

    songTemp.songName = songName; songTemp.songArtist = songArtist, songTemp.rating = rating;

    endofList.push_back(songTemp);

    vector<individualSong> songList;

}

void printFunction(vector<individualSong> songList)
{

    if (songList.size() > 0)
    {
        cout << "Song List" << endl;

        for (int printCounter = 0; printCounter < songList.size(); printCounter++)
        {
            cout << "Name: " << songList[printCounter].songName << " Artist: " << songList[printCounter].songArtist << " Rating: " << songList[printCounter].rating << endl;
        }
    }
    else
        cout << "Need to create list 1st" << endl;
}
void createplayList(vector<individualSong>& songList)
{
    songList.clear();  //clears old list to make new list


    string songName, songArtist;
    int songCounter, songNumber, rating;

    songCounter = 1, songNumber = 0, rating = 0;         //setting starting values for int variables


    cout << "Enter number of songs you will be listing : ";    //prompt to define end of loop

    cin >> songNumber;
    if (songNumber < 1)                                             //validation for number of songs
    {
        while (songNumber < 1)
        {

            cout << "You must list at least 1 song" << endl;
            cout << "Enter number of songs you will be listing : " << endl;          //error if not able to add songs
            cin >> songNumber;
        }
    }
    else;


    do
    {



        cin.ignore();                                                              //I know cin.ignore can be dangerous but it was a simple solution to the program skiping the first prompt after first entry using both ints and strings


        cout << "Enter the name of song " << songCounter << " : ";                  //Promt for song name, songCounter used as guide to position on the list

        getline(cin, songName);                                                                 //input for song name

        cout << "Enter the artist of song " << songCounter << " : ";

        getline(cin, songArtist);                                                           //input for artist

        cout << "Rating (1-10) : ";                     //Promt for song rating, songCounter used as guide to position on the list

        cin >> rating;                                                      //input for rating

        if (rating < 1 || rating >10)                           //validation for rating, must be between 1-10
        {
            while (rating < 1 || rating >10)                            //loop if rating not valid
            {

                cout << "Ratings must be from 1-10." << endl;
                cout << "Rating (1-10) : ";
                cin >> rating;

            }

        }
        else;

        addSong(songList, songName, songArtist, rating);       //calling addSong function
        cout << songName << " has been added to the list" << endl << endl;  //verifying song has been added, not necessary just for user interface
        songCounter++;




    } while (songNumber + 1 > songCounter);
}


void Openfile(vector<individualSong>& endofList,string songList, string songName, string songArtist)
{
    int file;
    char choice;

    

    cout << "Would u like to open a file of songs? ";
    cin >> choice;

    switch (choice)
    {
    case 'y':

        break;
    case 'Y':

        break;
    case 'n':

        int main();

    case 'N':

        int main();

    default:
        cout << "ERROR" << endl;
        exit(0);

        break;
    }

    cout << "File name: ";
    cin >> file;

    ifstream inputfile;

    inputfile.open("songlist.txt");

   

        for (int printCounter = 0; printCounter < songList.size(); printCounter++)
        {
            inputfile >> songName[printCounter];          
            inputfile >> songArtist[printCounter];
           // inputfile >> rating[printCounter];
        }

        inputfile.close();

        cout << "successfully read" << endl;



        int main();


        /// <summary>
        /// /////////////////
        /// </summary>
        /// <param name="songList"></param>
       









    //short loop = 0;
    //string line;

    //ifstream myfile("songList.txt");

    //if (myfile.is_open())
   // {
   //     while (!myfile.eof())
   //     {
   //         getline(myfile, line);
            //songList[loop] = line;
   //         cout << songList[loop] << endl;
   //         loop++;
   //         cout << "successfully read";
   //     }
   //     myfile.close();
  //  }
  //  else cout << "can't open the file";




}



int main()
{
    
    using namespace std;


    vector<individualSong> songList; // linked list of songList
    string songName, songArtist;
    int menuOption;

    do
    {
        using namespace std;

        cout << "Press 1 to create new list, 2 to print and 3 to exit " << endl;

        cin >> menuOption;
        if (menuOption == 1)                      //Create new list

        {
            createplayList(songList);
        }




        else if (menuOption == 2) // print the songList
        {
            printFunction(songList);                        // calling print list function
        }


    } while (menuOption != 3);




    



    return 0;                       //exit program
}

Posted by kixzin 2020-11-10 22:03:39

No Ans Posted

Please login to comment

Log In


Ask New Question