switch and if statement

Hi, I have an error in my code which I cannot find;
It prints out numbers 1-9 and 20-99.BUT it does not print out anything for 10-19;
(and to be honest I don't understand WHY it is printing out 1-9 correctly). Hopefully someone can help me with it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void BreakIntoDigits(int user_num)

{
    first_digit=user_num/10;
    last_digit=user_num%10;

    if (user_num>=10 &&user_num<20)
    {
       first_digit=user_num;
       DisplayWords (first_digit);
       cout<<" ";
    }

    else if (user_num>=20 && user_num<=999,999,999)



    {
         DisplayWords (first_digit);
         cout<<" ";
         DisplayW (last_digit);
         cout<<" "<<endl;
    }
}

void DisplayWords (int beginning_digit)
{
//10-19
if (beginning_digit==0 || beginning_digit==1)

{
    switch(beginning_digit)
    {
        case 1:cout << "ten ";break;
        case 2:cout << "eleven";break;
        case 3:cout << "twelve";break;
        case 4:cout << "thirteen";break;
        case 5:cout << "forteen";break;
        case 6:cout << "fifteen";break;
        case 7:cout << "sixteen";break;
        case 8:cout << "seventeen";break;
        case 9:cout << "eighteen";break;
        case 10:cout<<"nineteen";break;

    }
}
//20-99
   else if (beginning_digit>1&& beginning_digit<10)
{

    switch (beginning_digit)
  {
     case 1:cout << " ";break;
     case 2:cout << "twenty";break;
     case 3:cout << "thirty";break;
     case 4:cout << "forty";break;
     case 5:cout << "fifty";break;
     case 6:cout << "sixty";break;
     case 7:cout << "seventy";break;
     case 8:cout << "eighty";break;
     case 9:cout << "ninety";break;

  }

}
Would something like this work for you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void DisplayWord(int number)
{
  int firstDigit = number/10;
  int secondDigit = number%10;
  
  switch (firstDigit)
  {
  case 1:
    switch (number)
    {
    case 10:  cout << "ten ";      break;
    case 11:  cout << "eleven ";   break;
    case 12:  cout << "twelve ";   break;
    case 13:  cout << "thirteen "; break;
    case 14:  cout << "fourteen "; break;
    case 15:  cout << "fifteen ";  break;
    case 16:  cout << "sixteen ";  break;
    case 17:  cout << "seventeen ";break;
    case 18:  cout << "eighteen "; break;
    case 19:  cout << "nineteen "; break;
    }
    return;
  case 2:  cout << "twenty ";      break;
  case 3:  cout << "thirty ";      break;
  case 4:  cout << "fourty ";      break;
  case 5:  cout << "fifty ";       break;
  case 6:  cout << "sixty ";       break;
  case 7:  cout << "seventy ";     break;
  case 8:  cout << "eighty ";      break;
  case 9:  cout << "ninety ";      break;
  }
  
  switch (secondDigit)
  {
  case 1:  cout << "one ";         break;
  case 2:  cout << "two ";         break;
  case 3:  cout << "three ";       break;
  case 4:  cout << "four ";        break;
  case 5:  cout << "five ";        break;
  case 6:  cout << "six ";         break;
  case 7:  cout << "seven ";       break;
  case 8:  cout << "eight ";       break;
  case 9:  cout << "nine ";        break;
  }
}
Last edited on
Thank you, Stewbond. Your code will probably work but I want to understand my code and why i am getting certain output. Another thing is that I think once I get why I am having problems with 10-19 and why i am getting correct output for 1-9 it will be much easier for me to write the rest (I need this program to conver numbers up to 999,999,999)
why i am getting correct output for 1-9

Yourcode contains no logic for printing the numbers "one" to "nine", so there is no way to tell you why it is working or not working.

One reason you're having problems with 10-19 is that lines 35-43 can never be executed because of the if statement at line 29.
Topic archived. No new replies allowed.