Search code examples
c++getswitch-statementsetencapsulation

C++ get/set not getting value in switch case


I have 2 classes M and L each with its own .header and .cpp files.

In class L there are get/set methods

    class L // this is in L.h file
{
      private:
    int A;
    float B;

    public:
        A();

        A(int,float);

        void setA(int A);
        int getA();

        void setB(int B); 
        int getNoOfEarthLikePlanets();

};

//L.cpp//

L::L() // default constructor
{
    A = 0;
    B = 0;  
}   

L::L(int aA,float bB) non-default constructor
{   
    A = aA;
    B = bB; 
}

void L::setA(int aA) // set A
{
    A = aA;
}

int L::getA() // get A
{
    return(A);
}

void L::setB(float bB) //set B
{

    B = bB;
}

float L::geB() // get B
{   
    return(B);
}

My class M:

#include "L.h"
void M::mainMenu()
{
    int choice = 0;
    cout<<" 1) enter your A and B:"endl;
    cout<<" 2) Display your An B :"endl;
    cin>>choice;

    yourChoice(choice);


}
void M::yourChoice(int choice)
{   
    const int size = 50;
     int cinA;
     int cinB;
    static int count = 0;
    L newL[size];

while(choice != 999)          
    {
        switch(choice)
        {            
            case 1:
            {                          
                 while(count<SIZE)
                 {                
                 cout<<"What is A : ";
                 cin>>cinA;
                 newL[count].setA(cinA);
                 cout<<"What is B: ";
                 cin>>cinB;
                 newL[count].setA(cinA);
                 ++count;  
                  //******i tried to cout my newL[count].getA(); it displays a blank too*********
                 mainMenu();      
                 cin>>choice;
                 break;    
                 };
        //Even if i bring newL
            } // end of case 1
        break;
       case 2:
        {
           for(int x = 0;x<size;x++)
                   {                          
           cout<<newL[x].getA()<<endl; //Prints a blank 
                   }       
        }//end of case 2
         }//end of switch
   }//end of while

I am using get/set to get the user input in case 1 and printing it out in case 2. However when after I input the details in case 1, I go to case 2, it displays nothing, not even a 0, just blank. I have also tried putting static in front of my type variables because I thought it might be something to do between invocations.

And I also tried to cout the getA() in case 1 right after the I input the values, it displays blank too. What am I missing out? Pls request for more detail code if u feel other code I wrote might cause this to happen. This is only part of it.

In my int main() I just run mainMenu();


Solution

  • while(choice != 999)          
        {
            switch(choice)
            {            
                case 1:
                {                          
                     while(count<SIZE)
                     {                
                     cout<<"What is A : ";
                     cin>>cinA;
                     newL[count].setA(cinA);
                     cout<<"What is B: ";
                     cin>>cinB;
                     newL[count].setA(cinA);
                     ++count;  
                      //******i tried to cout my newL[count].getA(); it displays a blank too*********
                     YOUR TROUBLE MAKER>>>>>mainMenu();      
                     cin>>choice;
                     break;    
                     };
            //Even if i bring newL
                } // end of case 1
            break;
           case 2:
            {
               for(int x = 0;x<size;x++)
                       {                          
               cout<<newL[x].getA()<<endl; //Prints a blank 
                       }       
            }//end of case 2
             }//end of switch
       }//end of while
    

    That trouble maker line creates a new instance of mainMenu therefore, another set of newL array which has no relations to your original newL instantiated in the original mainMenu. Since mainMenu itself calls another yourChoice.

    Recommendation

    The addition of a Boolean mode is just to make sure you want a display of the main menu where the input is disabled. This way you can call mainMenu(true); if you want an input and mainMenu(false); if you only want a display

    void M::mainMenu(boolean mode)
    {
        cout<<" 1) enter your A and B:"endl;
        cout<<" 2) Display your An B :"endl;
        
        if(mode)
        {
            int choice = 0;
            cin >> choice;
            yourChoice(choice); 
        }   
    }