Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is there a way to write a removeChannel function for this TV class initialized a

ID: 3828935 • Letter: I

Question

Is there a way to write a removeChannel function for this TV class initialized as is? I need help trying to implement this function. The user would just select which channel number they'd like to delete between 1-10. How would I implement this?

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
class TV //TV class
{
private:
bool switchedOn;
int currentChannel;
string channelTitle[10];
int volume;
public:
TV()
{
switchedOn = false;
currentChannel = -1;

channelTitle[0] = "ESPN";
channelTitle[1] = "World Travel";
channelTitle[2] = "Oscar Awards";
channelTitle[3] = "Business";
channelTitle[4] = "World News";
channelTitle[5] = "Local Weather";
channelTitle[6] = "Disney Channel";
channelTitle[7] = "Game Show";
channelTitle[8] = "CNN";
channelTitle[9] = "Movie - Push";


volume = 0;
}

void switchOn()
{
switchedOn = true;
currentChannel = 1;
volume = 10;
}

void setCurrentChannel(int channel)
{
currentChannel = channel;
}

void removeChannel(int number) //This isn't working
{
string newchannelTitle[sizeof(channelTitle)/sizeof(string)-1];
int j=0;
for(int i=0;i<(sizeof(channelTitle)/sizeof(string));i++){
if(number!=(i+1)){
newchannelTitle[j++]=channelTitle[i];
}
}
}

};

Explanation / Answer

Here is your updated code with sample run . I have added main function to test the deletion. Please note that you have used static array of fixed size in your code and you want the deletion to work with that only. After deletion last element needs to be ignored as it will have the same content of second last element. Please take care of this situation in your code. I have also added one method to print all the channels to standard output for testing purpose. The code which is in bold is updated code.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class TV //TV class
{
private:
bool switchedOn;
int currentChannel;
string channelTitle[10];
int volume;
public:
TV()
{
switchedOn = false;
currentChannel = -1;

channelTitle[0] = "ESPN";
channelTitle[1] = "World Travel";
channelTitle[2] = "Oscar Awards";
channelTitle[3] = "Business";
channelTitle[4] = "World News";
channelTitle[5] = "Local Weather";
channelTitle[6] = "Disney Channel";
channelTitle[7] = "Game Show";
channelTitle[8] = "CNN";
channelTitle[9] = "Movie - Push";


volume = 0;
}
void switchOn()
{
switchedOn = true;
currentChannel = 1;
volume = 10;
}
void setCurrentChannel(int channel)
{
currentChannel = channel;
}
void removeChannel(int number)
{
   int size = sizeof(channelTitle)/sizeof(string);
for(int i=(number-1);i<size-1;i++){
   channelTitle[i] = channelTitle[i + 1];
}
  
}
  
void printChannelAfterDeletion() {
   for(int i=0;i<(sizeof(channelTitle)/sizeof(string) -1);++i){ // moving element from the deleting position to previous positions
cout<<(i+1)<<"."<<channelTitle[i]<<endl;
}
cout<<endl;
       }
       void printChannels() {
           for(int i=0;i<(sizeof(channelTitle)/sizeof(string));++i){
cout<<(i+1)<<"."<<channelTitle[i]<<endl;
}
cout<<endl;
       }

  
};

int main() {
   TV tvChannels;
   int c;
   cout<<"Enter the channel to be deleted:";
   cin>>c;
   cout<<"Initial Channels"<<endl;
   tvChannels.printChannels();
   tvChannels.removeChannel(c);
   cout<<"Channels after deletion"<<endl;
   tvChannels.printChannelAfterDeletion();
   return 0;
               }

Sample Output: -

Enter the channel to be deleted:6
Initial Channels
1.ESPN
2.World Travel
3.Oscar Awards
4.Business
5.World News
6.Local Weather
7.Disney Channel
8.Game Show
9.CNN
10.Movie - Push

Channels after deletion
1.ESPN
2.World Travel
3.Oscar Awards
4.Business
5.World News
6.Disney Channel
7.Game Show
8.CNN
9.Movie - Push


--------------------------------
Process exited after 2.398 seconds with return value 0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote