代码之家  ›  专栏  ›  技术社区  ›  forgivenprog33442

C++获取/设置:在开关情况下未获取值

  •  0
  • forgivenprog33442  · 技术社区  · 8 年前

    我有两节课 M L 每个都有自己的 .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,它不会显示任何内容,甚至不会显示 0 也就是说,只是一片空白。我还尝试将静态放在类型变量前面,因为我认为这可能与调用之间有关。

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

    在我 int main() 我只是跑 mainMenu();

    2 回复  |  直到 8 年前
        1
  •  0
  •   Community c0D3l0g1c    4 年前
    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
    

    该麻烦制造者行创建了一个主菜单的新实例,因此 newL 数组,该数组与原始数组没有关系 纽尔 在原始文件中实例化 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); 
        }   
    }
    
        2
  •  0
  •   Irving    8 年前

    宽恕后代33442 我认为没有输出任何内容,因为您的代码在调用 mainMenu() 就在你 ++count 在你的选择方法中,进行递归是偶然的。删除 主菜单() 从你 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
        }
    
    }