I have this an assignment which uses this code, but I need it to work right to get started on it. Everything works as expected except the removeFollowers function. It does end up removing the correct follower, however when printing the array again it messes up and repeats existing followers. I know the problem is with the logic of the removeFollowers function, but I can't seem to get it to work right.
Here's the header file, the main, the current output and the expected output:
-------TwitterProject.h-------------
#ifndef TWITTERPROJECT_H_INCLUDED
#define TWITTERPROJECT_H_INCLUDED
#include<iostream>
using namespace std;
template <class T>
class Twitter {
private:
string name;
T followers[5];
int numFollowers;
public:
Twitter(string x);
void addFollower(T follower); //takes in follower to add
bool removeFollower(T follower); //takes follower to remove
void printFollowers(); //doesn't take any parameters
};
template <class T>
Twitter<T>::Twitter(string x) { //constructor
name = x;
numFollowers = 0; //starts out with zero followers
}
template <class T>
void Twitter<T>::addFollower(T follower) {
if (numFollowers < 5) {
followers[numFollowers] = follower;
numFollowers++;
}
else{
cout<<"Error, this user already has maximum followers" << endl; }
}
template <class T>
bool Twitter<T>::removeFollower(T follower) {
for(int i=0;i<numFollowers;i++){
if(followers[i] == follower){
for(int j = i + 1; j < numFollowers; j++){
followers[j - 1] = followers[j];
numFollowers--;
return true;
}
return false;
}
}
cout << endl;
}
template <class T>
void Twitter<T>::printFollowers() {
cout << "Followers for " << name << ": " << endl;
for(int i = 0; i < numFollowers; i++)
cout << followers[i] << endl;}
#endif // TWITTERPROJECT_H_INCLUDED
---------main.cpp--------------
#include <iostream>
#include "TwitterProject.h"
using namespace std;
struct Profile {
string userName;
int age;
string state;
};
ostream& operator << (ostream & output, const Profile & p) {
output << p.userName;
return output;
} //overloading the insertion operator <<
bool operator == (const Profile &p1, const Profile &p2)
{
return p1.userName == p2.userName;
} //overloading the == operator for the removeFollower method
int main()
{
Twitter<string> e1("Sara");
Twitter<Profile> e2("Ty");
//adding followers of type string to user Sara (also string)
cout << "Adding followers to Sara" << endl;
e1.addFollower("Rapunzel");
e1.addFollower("Flynn");
e1.addFollower("Cinderella");
e1.addFollower("Prince Charming");
//printing Sara's followers
e1.printFollowers();
cout << endl;
//creating profiles of type username
Profile p1 = {"Rue", 18, "Michigan"};
Profile p2 = {"Jules", 22, "California"};
Profile p3 = {"Fez", 30, "Alaska"};
Profile p4 = {"Maddie", 21, "Colorado"};
Profile p5 = {"Cassie", 24, "Maine"};
//adding profile followers to Ty user (profile)
cout << "Adding follower profiles to Sara" << endl;
e2.addFollower(p1);
e2.addFollower(p2);
e2.addFollower(p3);
e2.addFollower(p4);
e2.addFollower(p5);
//printing Ty's followers
e2.printFollowers();
cout << endl;
//demonstrating removing followers from both users
cout << "Removing follower Flynn from Sara" << endl;
e1.removeFollower("Flynn");
cout << "Removing follower Prince Charming from Sara" << endl;
e1.removeFollower("Prince Charming");
e1.printFollowers();
cout << endl;
cout << "Removing Jule's profile from Ty" << endl;
e2.removeFollower(p2);
e2.printFollowers();
cout << endl;
return 0;
}
--------current output---------
Adding followers to Sara
Followers for Sara:
Rapunzel
Flynn
Cinderella
Prince Charming
Adding follower profiles to Sara
Followers for Ty:
Rue
Jules
Fez
Maddie
Cassie
Removing follower Flynn from Sara
Removing follower Prince Charming from Sara
Followers for Sara:
Rapunzel
Cinderella
Cinderella
Removing Jule's profile from Ty
Followers for Ty:
Rue
Fez
Fez
Maddie
------expected output------------
Adding followers to Sara
Followers for Sara:
Rapunzel
Flynn
Cinderella
Prince Charming
Adding follower profiles to Sara
Followers for Ty:
Rue
Jules
Fez
Maddie
Cassie
Removing follower Flynn from Sara
Removing follower Prince Charming from Sara
Followers for Sara:
Rapunzel
Cinderella
Removing Jule's profile from Ty
Followers for Ty:
Rue
Fez
Maddie
Cassie
Any help, hints, or leads are greatly appreciated. Thank you so much :)