• + 1 comment

    Hello What is wrong in my code ? I have 2 uses cases which fail (7 + 29 and 6 + 35) while the output seems correct to me...

    string num2word(int n) {    
        string s;
        int t = n/10;
        int u = n%10;
        
        switch (t) {
            case 0:
                if (u == 0) return "zero";
                break;
            case 1: 
                switch (u) {
                    case 0: s = "ten";       break;
                    case 1: s = "eleven";    break;
                    case 2: s = "twelve";    break;
                    case 3: s = "thirteen";  break;
                    case 4: s = "fourteen";  break;
                    case 5: s = "quarter";   break;
                    case 6: s = "sixteen";   break;
                    case 7: s = "seventeen"; break;
                    case 8: s = "eighteen";  break;
                    case 9: s = "nineteen";  break; 
                }
                return s;
                break;
            case 2: s = "tweenty";   break;
        }
        switch (u) {
            case  1: s += " one";   break;
            case  2: s += " two";   break;
            case  3: s += " three"; break;
            case  4: s += " four";  break;
            case  5: s += " five";  break;
            case  6: s += " six";   break;
            case  7: s += " seven"; break;
            case  8: s += " eight"; break;
            case  9: s += " nine";  break;
        }
        
        return ltrim(s);
    }
    
    string timeInWords(int h, int m) {
      string s;
      
     if (h == 0 && m == 0)  return "midnight";
     if (h == 12 && m == 0) return "noon";
     if (m == 30)           return "half past " + num2word(h);
     if (m == 15)           return "quarter past " + num2word(h);
     if (m == 45)           return "quarter to " + num2word(h+1);
     if (m == 0)            return num2word(h) + " o' clock";
     
      switch (m) {
          case 1:  s = " minute";   break;
          default: s = " minutes";  break;
      }
      
      if (m > 30)
        return num2word(60 - m) + s + " to " + num2word(h+1);
      return num2word(m) + s + " past " + num2word(h);  
    }