switch no default output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CPP freshman
    New Member
    • May 2012
    • 15

    switch no default output

    I tried this example:
    Code:
    void testSwitch(){
      for(int i=1;i<8;i++){
        cout<< i: <<i<< is ;
        switch(i){
          case 1: cout<<1; break;
          case 5: cout<<5; break;
          case 6: cout<<6; break;
          case 4: cout<<4; break;
    defaut: cout<<0; break;
        }
        cout<<endl;
      }
    }
    and the output is
    Code:
     i: 1 is 1
     i: 2 is 
     i: 3 is 
     i: 4 is 4
     i: 5 is 5
     i: 6 is 6
     i: 7 is
    Why no 0s for 2, 3, and 7?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    For the same reason that 1 is not displayed as 01.

    You will need to set the field width and the zero-fill for the cout ostream to see a zero.

    Comment

    • CPP freshman
      New Member
      • May 2012
      • 15

      #3
      I changed it to:
      Code:
      void testSwitch(){
        for(int i=1;i<8;i++){
          cout<< i: <<i<< is ;
          switch(i){
            case 1: cout<<1; break;
            case 4: cout<<4; break;
            case 5: cout<<5; break;
            case 6: cout<<6; break;
      defaut: cout<<[B]10[/B]; break;
          }
          cout<<endl;
        }
      }
      but the output is the same.

      Comment

      • taschetto
        New Member
        • May 2012
        • 1

        #4
        Because you've mistyped the default keyword.

        Comment

        Working...