Hi, I'm fairly new to C++.
My project states that I must write a program that will allow the user to view a list of TV shows read from an input file. each show should consist of four data members: show ID, Title, day of show, and time(pm). ID serves as primary key.

input file:
123 CSI Thursday 9
127 Survivor Thursday 8
124 Office Friday 10
122 AmazingRace Wednesday 7
125 Football Sunday 1

The program must read in the file and store it in an "Array of struct", and loop through the following MENU until the user quits.

'A' - ADD a show
'D' - DISPLAY list of shows
'M' - MODIFY a show's start time
'Q' - Quit

*Note - Modify will display a list of shows and ID's the user will pick one and then it will allow the user to edit the start time.

I understand structs, how to display the information, i/o, and looping but I am having trouble adding a show and modifying the shows

Here is my attempt:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct showType
{
   string id[9];
   string name[9];
   string day[9];
   string time[9];

};   
showType show;    

fstream infile;
int i = 0;
char select;

void Menu ( char select);
void getShows ( showType& show, int& i);
void addShows ( showType& show, int& i);
void modifyShows ( showType& show, int& i);
void displayShows ( showType& show, int& i);

int main()
{
    infile.open("Shows.txt");
    getShows(show,i);
    Menu(select);
    
    //displayShows(show,i);

    
    
    displayShows(show,i);
    
    system("pause");
    return 0;
}

void Menu ( char select)
{
     bool inc = true;
     
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" Menu"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" 'A' - ADD a show"<<endl;
     cout <<" 'D' - DISPLAY a list of all shows"<<endl;
     cout <<" 'M' - MODIFY a show's start time"<<endl;
     cout <<" 'Q' - QUIT"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl; 
     cin >> select;
     
     while (inc == true)
     {
         
         switch(select)
         {
           case 'a': select = 'A';
           case 'A': inc = false;
                break;
           case 'd': select = 'D';
           case 'D': inc = false;
                break;
           case 'm': select = 'M';
           case 'M': inc = false;
                break;
           case 'q': select = 'Q';
           case 'Q': inc = false;
                break;
           default: inc = true;
                    cout <<" INVALID INPUT, please select another option."<<endl;
                    cin >> select;
                    break;
         }
     
     }

     switch (select)
     {
           case 'A': addShows(show, i);
                break;
           case 'M': modifyShows(show, i);
                break;
           case 'D': displayShows(show, i);
                break;
     }           
          
}

void getShows ( showType& show, int& i)
{
     for (i; i < 9; i++)
         infile >> show.id[i] >> show.name[i] >> show.day[i] >> show.time[i];
}

void addShows ( showType& show, int& i)
{
     cout <<" Please enter the show record in the following format"<<endl;
     cout <<" ex: 123 CSI Thursday 9"<<endl<<" ";
          cin >> show.id[1] >> show.name[1] >> show.day[1] >> show.time[1];
}

void modifyShows (showType& show, int& i)
{
     
     
}  

void displayShows ( showType& show, int& i)
{
     i=0;
     cout <<left<<setw(8)<<" Show ID"<<right<<setw(12)<<" Show Name "<<setw(10)<<" Weekday ";
     cout <<left<<setw(13)<<" Show Time "<<endl<<endl;
     for (i; i < 5; i++)
     {
         cout <<right<<setw(6)<<setw(3)<<"   "<< show.id[i] <<right<<setw(14)<< show.name[i];
         cout <<setw(10)<< show.day[i] <<setw(6)<< show.time[i]<<endl;
         
     }
}

Recommended Answers

All 3 Replies

From what you have there, show is a one item, not an array, meaning that every time you add a show, you're overwriting the data previously placed there.

Are you stuck using an array of structs, cause a vector would be much easier. vector <showType> list; will declare it. This is what getShows would look like if you used a vector to store all the shows.

void getShows ( showType& show, int& i)
{
     for (i; i < 9; i++)
         infile >> show.id[i] >> show.name[i] >> show.day[i] >> show.time[i];
         list.push(show);
}

Also, the getShows line in main should be before Menu that way people can see all the shows already without adding any.

Put Menu in a loop that breaks when a person inputs Q otherwise they will only be able to enter one option before the program closes.

You can simplify the menu function by using toupper to force all the letters to be uppercase and thus eliminate the need of the first switch. Here's the revised code.

void Menu ( char select)
{
     bool inc = true;
     
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" Menu"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" 'A' - ADD a show"<<endl;
     cout <<" 'D' - DISPLAY a list of all shows"<<endl;
     cout <<" 'M' - MODIFY a show's start time"<<endl;
     cout <<" 'Q' - QUIT"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl; 
     cin >> select;

        switch(toupper(select))
         {
           case 'A': addShows(show, i);
                break;
           case 'M': modifyShows(show, i);
                break;
           case 'D': displayShows(show, i);
                break;
           case 'Q': break;
           default: inc = true;
                    cout <<" INVALID INPUT, please select another option."<<endl;
         }
}

There is a while(cout) cout << pm; at the bottom of the file that is going to cause issues. Just get rid of the while part an it will run fine.

That's all I can see wrong with your code at the moment. Hope this helps.

Thank you Demon gal I appreciate the quick response and help. I have implemented a few of your suggestions and they were very helpful. Yes, I am stuck using an array of struct (but thanks for the heads up on vectors). I also modified the struct to an array but now i receive errors about "too few arguments".
I am still confused about how to modify the certain elements after the user selects an ID.

attempt:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct showType
{
   string id;
   string name;
   string day;
   string time;

};   
showType show[5];    

fstream infile;
int i = 0;
char select;
string d;

void Menu ( char select);
void getShows ( showType show[], int& i);
void addShows ( showType show[], int& i);
void modifyShows ( showType show[], int& i, string d);
void displayShows ( showType show[], int& i);

int main()
{
    infile.open("Shows.txt");
    
    getShows(show,i);
    
    while (select != 'Q')
    {
          Menu(select);
    }
    

    displayShows(show,i);
    
    system("pause");
    return 0;
}

void Menu ( char select)
{
     bool inc = true;
     
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" Menu"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
     cout <<" 'A' - ADD a show"<<endl;
     cout <<" 'D' - DISPLAY a list of all shows"<<endl;
     cout <<" 'M' - MODIFY a show's start time"<<endl;
     cout <<" 'Q' - QUIT"<<endl;
     cout <<" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl; 
     cin >> select;
     
     while (inc == true)
     {
      
           switch(toupper(select))
           {
            case 'A': addShows(show, i);
                 break;
            case 'M': modifyShows(show, i);
                 break;
            case 'D': displayShows(show, i);
                 break;
                 case 'Q': break;
                      default: inc = true;
                               cout <<" INVALID INPUT, please select another option."<<endl;
            }
      }
          
}

void getShows ( showType show[], int& i)
{
     for (i; i < 5; i++)
         infile >> show.id[i] >> show.name[i] >> show.day[i] >> show.time[i];
}

void addShows ( showType show, int& i)
{
     i++;
     cout <<" Please enter the show record in the following format"<<endl;
     cout <<" ex: 123 CSI Thursday 9"<<endl<<" ";
          cin >> show.id[i] >> show.name[i] >> show.day[i] >> show.time[i];
}

void modifyShows (showType show[], int& i, string d)
{
     cout <<" Please enter the ID of the show you wish to modify ";
          cin >> d;
     //if (d == show.id[i])
        cin >> show.id[i] >> show.name[i] >> show.day[i] >> show.time[i];
     
}  

void displayShows ( showType show[], int& i)
{
     i=0;
     cout <<left<<setw(8)<<" Show ID"<<right<<setw(12)<<" Show Name "<<setw(10)<<" Weekday ";
     cout <<left<<setw(13)<<" Show Time "<<endl<<endl;
     for (i; i < 5; i++)
     {
         cout <<right<<setw(6)<<setw(3)<<"   "<< show.id[i] <<right<<setw(14)<< show.name[i];
         cout <<setw(10)<< show.day[i] <<setw(6)<< show.time[i]<<endl;
         
     }
     cout <<endl<<endl;
}

I would first make your array size 100 and get a counter to keep track of the number of shows that have been entered. It is difficult to modify the size of an array after the program is running (vectors don't have that issues, hence why I suggested them).

Anyway, to modify a certain show, I'd ask for the id of the show they'd want to modify, then for which piece they'd like to modify, then what the new value is. Then you just walk down the array with a for loop.

for(int i = 0; i < counter; i++) // counter is the total number of shows already in the array
    { if(show[i].id== id)
        { if(/*the user wanted to modify name*/) show[i].name= val;
          ....
        }
     }

You'd have to fill in the other piece but that's the general idea.

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.