number place value

is there a way to make it so i can have a switch change the number 127 into one two seven in that order and to recognize which place a number(such as 1s 10s 100s....etc) is in so i can have the program out put the number in words
Is this what you wanted?

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
#include <iostream.h>
#include <stdlib.h>
#include <cmath>
using namespace std;

void convertIntToWords(int);

int main()
{
  convertIntToWords(127);
  system("PAUSE");
  return 0;
}

void convertIntToWords(int num) {
  int i;
  int digit;
  for (i=1;i<=10000;i*=10) {
    if (floor(num/i==0)) {
      i/=10;
      break;
    }
  }
  if (i>=10000) i/=10;
  for (int j=i;j>=1;j/=10) {
    digit=(int)floor(num/j);
    num-=digit*j;
    switch (digit) {
      case 0:
        cout << "zero ";
        break;
      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;
    }
  }
  cout << endl;
}


~psault
Last edited on
yeah could you comment it so i can better understand it

this part in particular

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void convertIntToWords(int num) {
  int i;
  int digit;
  for (i=1;i<=10000;i*=10) {
    if (floor(num/i==0)) {
      i/=10;
      break;
    }
  }
  if (i>=10000) i/=10;
  for (int j=i;j>=1;j/=10) {
    digit=(int)floor(num/j);
    num-=digit*j;
    switch (digit) {


Last edited on
i have another idea but i cant get it to work




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
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string Number;
    int length;
    cout << "type in a number: ";
    cin  >> Number;
    length = Number.length();
    cout << "Length: " << length;
    
    
    for (int i = 1;length >= i; i++)
    {
        switch((int)Number[i])
        {
                         case 0:
                              cout << "zero ";
                              break;
                         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;
         }
     }
     
     system("pause");
     return 0;
}
Last edited on
The index operator on strings is zero based, so the loop arounf the switch statement should initialise i to 0.
You also do not need to try and case the individual elements to int - you can switch on the Char directly

1
2
3
4
5
6
7
8
9
10
    for (int i = 0;length >= i; i++)
    {
        switch(Number[i])
        {
                         case '0':
                              cout << "zero ";
                              break;
                         case '1':
                              cout << "one ";
                              break;

etc.

To get the position part (thousdand, hundred, etc.) I would suggest a second int, initialised to Length and decrementing in the loop.
A second switch statement would use this to output the position

So the loop would become

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    for (int i = 0, int j=length;length >= i; i++,j--)
    {
        switch(Number[i])
        {
                         case '0':
                        ...
       }
       switch (j)
       {
                     case 3 : 
                          cout << "hundred ";
                          break;
                     case 4 :
                          cout << "thousand ";
                          break
       }

    }


Then all you have to do is add a few additional rules to handle commas etc
EG To have the output 'One hunderd and twenty three'

Last edited on
Last edited on
@firedraco - are you sure?
the intention was
1
2
3
4
for(
      int i = 0, int j = length;          //initialisation
      length >= i;                           //condition
      i++, j--)                                 //reinitialisation 

so i runs from 0 to length, j runs from length down to 0

My compiler (VS 2005) will certainly throw an error at seeing 3 ';' in the for loop!
Last edited on
Oh whoops, yeah my bad. I missed the ; after j=length, sorry.
thanks dudes
Topic archived. No new replies allowed.