Home > Enterprise >  I need help trying to figure out the logic for removing elements in an Array
I need help trying to figure out the logic for removing elements in an Array

Time:02-22

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 :)

CodePudding user response:

I don't really understand your logic in your removefollowers function, but what I think you're trying do, or somewhere along those lines is: find follower to remove, shift array to left one space from after that index, and decrement number of followers?

I think you can probably take an easier approach to this, by just swapping your found element with the last element of array, and decrementing number of followers.

So your function should be kind of like this maybe?

template <class T>
bool Twitter<T>::removeFollower(T follower) {

    
    for(int i=0;i<numFollowers;i  ){
        if(followers[i] == follower){       
            T tmp = followers[i];
            followers[i] = followers[numFollowers-1];
            followers[numFollowers-1] = tmp;

            --numFollowers;
            return true;
        }
    }
    return false;
}

Personally, I don't recommend using arrays for this type of thing. Vectors are a much better option for removing and adding elements.

P.S. haven't tested this so, sorry if there are typos...

CodePudding user response:

Within the inner loop of the function removeFollower

for(int j = i   1; j < numFollowers; j  ){

        followers[j - 1] = followers[j];
        numFollowers--;
        return true;
}

there is an exit from the function as soon as one element was copied to the preceding element of the array. As a result the same element in fact is duplicated provided that it is not the last element of the array.

The function can be declared (I changed the type of the function parameter) and defined the following way

template <class T>
bool Twitter<T>::removeFollower( const T &follower ) 
{
    int i = 0;

    while ( not ( i == numFollowers ) && not ( followers[i] == follower ) ) i  ;

    bool success = i != numFollowers;

    if ( success )
    {
        while (   i != numFollowers )
        {
            followers[i - 1] = followers[i];
        }
        --numFollowers;
    }
 
    return success;
}

The function guarantees that after its execution the order of followers will not be changed that looks naturally.

  • Related