TinhornAdeniyi 0 Light Poster

ok this code has a few quirks that is annoying me
the only part that does not straight up work is the Add a show
i have a problem in increasing the size of the array.

there is also the issue of couting two zeros for the minutes of the time slot

this is my infile
4512 Supernatural Friday 10 00
2373 TDI Monday 8 00
5368 AOTS Monday 7 00
2553 xplay Tuesday 6 30

and the code....

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

using namespace std;
   
       struct Schedule //declaration of struct
    {
           int Show_ID;
           string Title;
           string Day;
           int Hour;
           double Min;
           
           };
      
    void initSchedule(Schedule tvShow[], int& maxshows);// read the file
    void menu(Schedule tvShow[], int maxshows);// User menu
    void addShow (Schedule tvShow[], int& maxshows);// 'A' from menu
    void DisplayShow (Schedule tvShow[], int maxshows);// 'D' from menu
    void ModifyShow (Schedule tvShow[], int maxshows); // 'M' from menu
     
int main()
{
    int maxshows;
    cout<<"Please enter the maximum amount of shows"<<endl;
    cin>>maxshows;
    Schedule tvShow[maxshows];
    initSchedule(tvShow,maxshows);
    menu(tvShow,maxshows);
    
    system("pause");
    return 0;
}

void initSchedule(Schedule tvShow[], int& maxshows)
{
ifstream inFile;
inFile.open("Shows.txt");
int i;
for (i=0;i<maxshows;i++)
{
inFile>>tvShow[i].Show_ID>>tvShow[i].Title>>tvShow[i].Day>>tvShow[i].Hour>>tvShow[i].Min;
}
}


void addShow (Schedule tvShow[], int& maxshows)
{
int old, amount, i=0;
cout<<"How many shows would you like to add"<<endl;
cin>>amount;
old=maxshows;
maxshows=maxshows+amount;
do
{
                         i++;
cout<<"Please enter the Show ID"<<endl;
cin>>tvShow[old+i].Show_ID;
cout<<"Please enter the title of the show"<<endl;
cin>>tvShow[old+i].Title;
cout<<"Please enter the Day the show airs"<<endl;
cin>>tvShow[old+i].Day;
cout<<"Please enter the Hour the show airs"<<endl;
cin>>tvShow[old+i].Hour;
cout<<"Please enter the Minute the show airs"<<endl;
cin>>tvShow[old+i].Min;

}
while(i<amount);
}

void DisplayShow (Schedule tvShow[], int maxshows)
{

     int i;
for (i=0;i<maxshows;i++)
{
cout<<tvShow[i].Show_ID<<" "<<tvShow[i].Title<<" "<<tvShow[i].Day<<" "<<tvShow[i].Hour<<":"<<setprecision(2)<<tvShow[i].Min<<" p.m"<<endl;
}
cout<<endl;
}
void ModifyShow (Schedule tvShow[], int maxshows)
{
     int comp;
cout<<"Please enter the Show ID of the show you are modifying"<<endl;
cin>>comp;
for(int i=0;i<maxshows;i++)
{
if(comp==tvShow[i].Show_ID)
{
cout<<tvShow[i].Show_ID<<" "<<tvShow[i].Title<<" "<<tvShow[i].Day<<" "<<tvShow[i].Hour<<":"<<setprecision(2)<<tvShow[i].Min<<" p.m"<<endl;
cout<<"Please enter the new Hour of the Show"<<endl;
cin>>tvShow[i].Hour;
cout<<endl;
cout<<"Please enter the new minute the show airs"<<endl;
cin>>tvShow[i].Min;
cout<<endl;
}
}
}
void menu (Schedule tvShow[], int maxshows)
{
char user;

do
{
cout<<"This is the user menu"<<endl;
cout<<"Enter 'A' to add a show"<<endl;
cout<<"Enter 'D' to display a list of all shows"<<endl;
cout<<"Enter 'M' to modify a show's start time"<<endl;
cout<<"Enter 'Q' to quit the menu"<<endl;
cin>>user;
switch(user)
{
case 'a':
case 'A':
addShow(tvShow,maxshows);
break;
case 'd':
case 'D':
DisplayShow(tvShow,maxshows);
break;
case 'm':
case 'M':
ModifyShow(tvShow,maxshows);
break;
case 'q':
case 'Q':
cout<<"Bye :)"<<endl;
break;
default:cout<<"This is not one of the letters, Please try again"<<endl;
}
}while(user!='Q');
}

the programs stops working after you try to input a new show title

and another question
can do while loops work if the test condition has a compound statement because

do
{
cout<<"This is the user menu"<<endl;
cout<<"Enter 'A' to add a show"<<endl;
cout<<"Enter 'D' to display a list of all shows"<<endl;
cout<<"Enter 'M' to modify a show's start time"<<endl;
cout<<"Enter 'Q' to quit the menu"<<endl;
cin>>user;
switch(user)
{
case 'a':
case 'A':
addShow(tvShow,maxshows);
break;
case 'd':
case 'D':
DisplayShow(tvShow,maxshows);
break;
case 'm':
case 'M':
ModifyShow(tvShow,maxshows);
break;
case 'q':
case 'Q':
cout<<"Bye :)"<<endl;
break;
default:cout<<"This is not one of the letters, Please try again"<<endl;
}
}while(user!='Q');

this works but

do
{
cout<<"This is the user menu"<<endl;
cout<<"Enter 'A' to add a show"<<endl;
cout<<"Enter 'D' to display a list of all shows"<<endl;
cout<<"Enter 'M' to modify a show's start time"<<endl;
cout<<"Enter 'Q' to quit the menu"<<endl;
cin>>user;
switch(user)
{
case 'a':
case 'A':
addShow(tvShow,maxshows);
break;
case 'd':
case 'D':
DisplayShow(tvShow,maxshows);
break;
case 'm':
case 'M':
ModifyShow(tvShow,maxshows);
break;
case 'q':
case 'Q':
cout<<"Bye :)"<<endl;
break;
default:cout<<"This is not one of the letters, Please try again"<<endl;
}
}while(user!='Q'||user!='q');

doesnt

Dani AI

Generated

As discovered, the crash when adding shows comes from writing past the original array. Declaring Schedule tvShow[maxshows] on the stack and then increasing maxshows does not resize that buffer — writing into tvShow[old + i] after the original allocation invokes undefined behavior and will often crash when you later read strings. Use a dynamic container (std::vector) or reallocate with new/delete; std::vector is safer and simpler. Also store minutes as an integer and format with setw/setfill to print a leading zero (using setprecision on minutes is the wrong tool). Finally, the do/while test while(user != 'Q' || user != 'q') is logically incorrect (it is always true); use && or normalize case with toupper.

Example approach (concept only — not a copy of the original full program):

#include <vector>
#include <iomanip>
#include <cctype>

// use a vector so adding shows just pushes back
std::vector<Schedule> shows;
shows.reserve(initial_capacity); // optional

// when adding:
Schedule s;
// read fields safely (use getline for multi-word titles, remember cin.ignore())
shows.push_back(s);

// print time with leading zero
std::cout << std::setw(2) << std::setfill('0') << shows[i].Min;

Use std::getline for multi-word titles and remember to cin.ignore() after numeric input. See the std::vector and IO manipulators references for details: std::vector, and the logical operator rules for the loop condition: operator logical.

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.