切换案例

时间:2016-10-21 07:42:14

标签: c++ get switch-statement set encapsulation

我有2个课程ML,每个课程都有自己的.header.cpp个文件。

在课程L中有get / set方法

    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);
}

我的班级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

我使用get / set来获取案例1中的用户输入并在案例2中将其打印出来。但是当我输入案例1中的详细信息后,我转到案例2,它什么也没显示,甚至没有{ {1}},只是空白。我也尝试将静态放在我的类型变量前面,因为我认为在调用之间可能需要做些什么。

我在输入值后立即尝试0案例1中的cout,它也显示为空白。我错过了什么?如果你觉得我写的其他代码可能会导致这种情况发生,请请求更详细的代码。这只是其中的一部分。

在我的getA()中,我只是运行int main()

2 个答案:

答案 0 :(得分:0)

forgivenprog33442 我认为没有什么是打印出来的,因为你的代码实际上从未进入案例2,因为你在你的选择方法mainMenu()之后立即调用了++count,意外地进行了递归。只需从您的选择方法中的mainMenu()变量中删除choice,然后在您的班级中将choice声明为private变量。

例如,您可以执行此操作以进行循环,而选项位于您正在处理的范围内

while(choice > 0 && choice < 3) 
{
    switch(choice)
    {
          case 1:
             while(count < SIZE)
            { 
             //loop until all number are set
             //then ask for choice
             //make sure you check if space in array 
             //or use a vector so it can resize automatically
             }
          case 2:
             //print your values
    }

}

答案 1 :(得分:0)

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

麻烦制造者行创建了一个新的mainMenu实例,另一组newL数组与原始newL中实例化的原始mainMenu无关。由于mainMenu本身会调用另一个yourChoice

建议

添加布尔模式只是为了确保您想要显示禁用输入的主菜单。这样,如果您想要输入,可以拨打mainMenu(true);,如果只需要显示,可以拨打mainMenu(false);

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); 
    }   
}
相关问题