How to make the program look at each integer in a 3 digit number individually

I am trying to get the program to read the number that the user puts in (eg. user inputs 835, my program says "eight three five ". I can get it to say it if it is a single digit (I just need to put in the breaks). I want to be able to do 2+ digits. What am I doing wrong?

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

using namespace std;

int number;


int main()
{
    cout << "Give me a number, and I will convert it to a word: ";
    cin >> number;

    while (1)
    {
        switch (number)
        {
            case 0:
                cout << "zero ";
            case 1:
                cout << "one ";
            case 2:
                cout << "two ";
            case 3:
                cout << "three ";
            case 4:
                cout << "four ";
            case 5:
                cout << "five ";
            case 6:
                cout << "six ";
            case 7:
                cout << "seven ";
            case 8:
                cout << "eight ";
            case 9:
                cout << "nine ";
        }
    break;
    }
Last edited on
Take the input as a string, process each character in it sequentially.
Ok I think I changed it, but it is still not working. I changed it to a for loop, and I think I made "number" an array. It is basically doing the same thing.
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.h>

using namespace std;

int number[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;

int main()
{    

    cout << "Give me a number, and I will convert it to a word: ";
    cin >> number[10];

    for (i == 0; i <= 9; i++)
    {
        switch (number[10])
        {
            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;
        }
    }



return 0;

}
Last edited on
cin >> number[10];

This stores the input integer into the location after the end of your array (so you're trashing memory too, which is nice). I expect you were hoping that each digit in the input integer would be taken and stored as a different integer in the array. it won't. If you input one integer, it gets stored as one integer.

Input a string (remember to include <string> - string.h is a C header that you shouldn't ever use in C++ anyway).

string inputValue;

then take each char in the string one at a time:

1
2
3
4
5
6
7
8
9
10
for ( int i =0; i < inputValue.length(); i++)
{
  switch (inputValue[i])
  {
     case '1':  // Note that this is '1', the char
         cout << "one ";
         break;
     // and so on
  }
}


Thank you! It worked! But I have two quick quesitons:

1) What is the point of .length() after inputValue? I know that if it is not there, it will not work, but what does it mean?

2) What is the point of [i] inside of inputValue in the switch?
1) string is a class. It has class function. length is one of those functions:
http://www.cplusplus.com/reference/string/string/

2) Same answer, under operator[]

You are the best! Thank you!
Topic archived. No new replies allowed.