Switch statement behavior in C++

I'm having trouble understanding my C++ switch statement.

I have to enter the accepted integer interval twice for the function to return to the switch. And then it falls straight through to case 2.

Inherited Class:

class Fugl : public DyrLuft
{
  private:
    int alder;

  public:
    Fugl() : DyrLuft()
      { }                                         
    void les()
      { 
      do
        {
        cout << "nSkriv inn fuglens alder: ";
        cin >> alder;
        if(alder < 0 || alder > 130)
          cout << "nDenne alderen virket usannsynlig, prøv igjen!n";
        } while(alder < 0 || alder > 130);
      }
};

Main:

int main()
{
  char valg = 'q';
  cout << "Hvilken dyreart ønsker du å registrere? (Q for å avslutte)"
    << "n1) - Fugl n2) - Fisk n3) - Insekt n4) - Skalldyrn"; 
  do
    {
    cin >> valg;
    switch(valg)
      {
      case '1':
        {
        Fugl fugl; fugl.les();
        } break;
      case '2':
        {
        Fisk fisk; fisk.les();
        } break;
      case '3':
        {
        Insekt insekt; insekt.les();
        } break;
      case '4':
        {
        Skalldyr skalldyr; skalldyr.les();
        } break;
      case 'Q': return 0;
      case 'q': return 0;
      default: cout << "Velg en av ovennevnte!n";
      }
    } while(valg != 'Q' || valg != 'q');
  return 0;
}

I dont know what is happening in your case, but I ran your code and it works just fine for me. Entered 1,4,Q and program exited as expected....Could be a compiler or a DyrLuft class issue(i just removed the inheritance to make it work, also lines from case 2,3,4).


You have:

case '1':
        {
        Fugl fugl; fugl.les();
        } break;

When you run this you create a Fug1 object and then you call the les() function. When you enter an appropriate age in les() the function returns. Since the break; is outside of the case block it is actually breaking the switch statement and going to the end of the loop. Then it cycles back to the top of the loop and makes you enter a selection again. If you move break inside og the case block it functions as it should. This is the changed loop:

do
    {
        cout << "Hvilken dyreart ønsker du å registrere? (Q for å avslutte)"
            << "n1) - Fugl n2) - Fisk n3) - Insekt n4) - Skalldyrn";
        cin >> valg;
        switch (valg)
        {
        case '1':
        {
                    Fugl fugl; fugl.les(); 
                    break;
        } 
        case '2':
        {
                    Fisk fisk; fisk.les();
                    break;
        }
        case '3':
        {
                    Insekt insekt; insekt.les();
                    break;
        }
        case '4':
        {
                    Skalldyr skalldyr; skalldyr.les();
                    break;
        } 
        case 'Q': return 0;
        case 'q': return 0;
        default: cout << "Velg en av ovennevnte!n";
        }
    } while (valg != 'Q' || valg != 'q');
链接地址: http://www.djcxy.com/p/84474.html

上一篇: 为什么C ++在switch语句中需要中断?

下一篇: 在C ++中切换语句行为