-3

I am using Ubuntu terminal g++ compiler for this , its hard for me to debug

Here is my code : S.h

 class S
    {
    protected: 
        string name;
        bool space;
    public:
        S();
        S(string,bool);       
        void setName(string name);
    string getName();
};

In my S.cpp

string S::getName() 
{   
    return (name);  
}

void S::setName(string name) // I tried to change to other variable names but gave me segmentation errors
{
   name = name;
}

S::S() // default constructor
{
    name = "";
    space = true;
}

    S::S(string name, bool space) // non-default constructor
{
        name = name;
        space= space;
    }

I got another class which I am going to implement the flow In my Main.cpp

    void Main::mainmenu()
{
    //cout displays//

    cout<<"Please input your choice: ";
    cin>>choice;
    option(choice);
}
    void Main::option(int choice)
{

    static string shapename;
    static bool space;
    const int size = 100;
    static S s[size];
    static int number =0;
    static int count = 0;

    while(choice !=999)
    { 

        switch(choice)
        {
            case 1: 
            {
                cout<<"Please enter name of Shape:";
                cin>>shapename;
                s[size].setName(shapename);
                cin.clear();
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                  count++;
                mainmenu();
                cin>>number;
                option(number);

            }
            break;
            case 2:
            {

                 for(int x = 0;x<count;x++)
                {   
                    cout<<count<<endl;
                    cout<<shapename<<endl; // this is to test if the string reaches case 2 and it displays the correct input
                    cout<<s[x].getName()<<endl; // however this method prints a blank.
                }

                mainmenu();
                cin>>number;
                option(number); 
            }
            break;
            default:break;
        } // end of switch      
    } // end of while  
}

I am just running mainmenu() in my int main()

I am trying to do a switch case on my main.cpp whereby the case 1 I get user to input the name they want and I use set method from class S to get the name , and in my case 2 , I want to retrieve and print out the name the user input by using get method .

I have put static infront of my delcarations because I thought the invocations might affect the values .

How can I retrieve my get/set methods with switch case statement ? I am using Ubuntu terminal to do this its very difficult for me to see what values are being passed in.

  • you will get ah, uh, stack overflow after a number of run. think about the logic of code again – Danh Oct 31 '16 at 06:39

2 Answers2

1
 s[size].setName(shapename);

Always writes to the same spot, and it is outside the array. Pretty sure you want

 s[count].setName(shapename);

Once you have this bit of nasty fixed, you can deal with the name shadowing bug in setName without triggering a segfault.

Off topic:

All those static variables should probably be class member variables.

Recursion is not a good choice inside the while loop. both try to accomplish the same goal.

user4581301
  • 33,082
  • 7
  • 33
  • 54
1

Let's see if this can help you a little bit.

Consider this:

void S::setName(string name)
{
   name = name;
}

What you are doing here is initialize the variable name with the value of name, aka you are setting name with it's own value.

This is known as variable shadowing.

To avoid it, is best practice to prefix you private data member, such as

class S
{
protected: 
    string _name;
    bool _space;
    ...
};

so, for example, your setter will be

void S::setName(string name) 
{
   _name = name;
}

another way is to explicitly access your members using this as following

void S::setName(string name) 
{
   this->name = name;
}

It is a matter of taste if you want to prefix your data members, use this or both.

Stefano Azzalini
  • 1,027
  • 12
  • 21