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

Sorry for the spam, I can\'t seem to get this working. The user must first delet

ID: 3829531 • Letter: S

Question

Sorry for the spam, I can't seem to get this working. The user must first delete a channel before adding one, this tv can only hold a maximum of 10 channels. I am able to delete a channel for the moment fine, but once I add one I start getting some duplicating errors when trying to print the final channel list. How might I go about fixing this? I believe the error in logic is in the printChannelAfterAddition function because thats what's providing the duplicate channel titles.

#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 removeChannel(int number)
{
int size = sizeof(channelTitle)/sizeof(string);
for(int i=(number-1);i<size-1;i++){
channelTitle[i] = channelTitle[i + 1];
}

}
  
void addChannel (int number, string name)
{
int size = sizeof(channelTitle)/sizeof(string);
int i = number-1;
channelTitle[ i ] = name;
for (int j = (number+1);i<=size; i++){
channelTitle[j] = channelTitle[j-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 printChannelAfterAddition() { //Something has to be off about this function
for(int i=0; i<(sizeof(channelTitle)/sizeof(string) ); ++i){
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(int argc, char** argv) {

//Creating TV class object
TV tv;
int choice, choice2, choice3, choice5, choice6;
string choice4;
int end = 1;
tv.switchOn();
  
while(end == 1){

cin >> choice;
  
switch(choice){

case 8: //Not Proper
cout<<"What channel number are you replacing? 1-10"<<endl;
cin>>choice5;
cout<<"What is the name of the channel you'd like to add?"<<endl;
cin>>choice4;
tv.printChannelAfterDeletion();
tv.addChannel(choice5, choice4);
cout<<"Channel Added"<<endl;
tv.printChannelAfterAddition();
break;

case 9:
cout<<"What is the channel number you're trying to remove?";
cin>>choice6;
cout<<"Initial Channels"<<endl;
tv.printChannels();
tv.removeChannel(choice6);
cout<<"Channel Removed"<<endl;
tv.printChannelAfterDeletion();
break;

}

}

return 0;

};

Explanation / Answer

c++ 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 removeChannel(int number)
{
//int size = sizeof(channelTitle)/sizeof(string);
// for(int i=(number-1);i<size-1;i++){
channelTitle[number-1] = "";
// }
}
  
void addChannel (int number, string name)
{
int size = sizeof(channelTitle)/sizeof(string);
int i = number-1;
channelTitle[ i ] = name;
for (int j = (number+1);i<=size; i++){
channelTitle[j] = channelTitle[j-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 printChannelAfterAddition() { //Something has to be off about this function
for(int i=0; i<(sizeof(channelTitle)/sizeof(string) ); ++i){
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;
}
void switchOn(){
switchedOn = true;
}
};
int main(int argc, char** argv) {
//Creating TV class object
TV tv;
int choice, choice2, choice3, choice5, choice6;
string choice4;
int end = 1;
tv.switchOn();
  
while(end == 1){
cin >> choice;
  
switch(choice){
case 8: //Not Proper
cout<<"What channel number are you replacing? 1-10"<<endl;
cin>>choice5;
cout<<"What is the name of the channel you'd like to add?"<<endl;
cin>>choice4;
tv.printChannelAfterDeletion();
tv.addChannel(choice5, choice4);
cout<<"Channel Added"<<endl;
tv.printChannelAfterAddition();
break;
case 9:
cout<<"What is the channel number you're trying to remove?";
cin>>choice6;
cout<<"Initial Channels"<<endl;
tv.printChannels();
tv.removeChannel(choice6);
cout<<"Channel Removed"<<endl;
tv.printChannelAfterDeletion();
break;
}
}
return 0;
}

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