Suggestions to improve code

Hi,
I'm trying to get my brain working again and running through a new (to me) book. One of the exercises is this:
Make a vector holding the ten string values "zero", "one", ... "nine".
Use that in a program that converts a digit to its corresponding spelled-Out
value; e.g., the input 7 gives the output seven. Have the same program,
using the same input loop, convert spelled-out numbers into their
digit form; e.g., the input seven gives the output 7.

Without the includes, this is what I came up with

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
int main()
{
    vector<string> s_nums(10);
        s_nums[0] = "zero";
        s_nums[1] = "one";
        s_nums[2] = "two";
        s_nums[3] = "three";
        s_nums[4] = "four";
        s_nums[5] = "five";
        s_nums[6] = "six";
        s_nums[7] = "seven";
        s_nums[8] = "eight";
        s_nums[9] = "nine";

    string num = "0";
    int out_num = 0;

    cout << "\t** NUMBER SPELLER **\n\n";
    cout << "Enter a number to be spelled out (0-9)" << "\n"
        << "or spell out a number to print its digit (ctrl+z to end): ";

    while(cin >> num)
    {
        if(num == s_nums[0] || num == "0")
        {
            out_num = 0;
            if(num == "0")
            {
                cout << s_nums[0] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[1] || num == "1")
        {
            out_num = 1;
            if(num == "1")
            {
                cout << s_nums[1] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[2] || num == "2")
        {
            out_num = 2;
            if(num == "2")
            {
                cout << s_nums[2] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[3] || num == "3")
        {
            out_num = 3;
            if(num == "3")
            {
                cout << s_nums[3] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[4] || num == "4")
        {
            out_num = 4;
            if(num == "4")
            {
                cout << s_nums[4] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[5] || num == "5")
        {
            out_num = 5;
            if(num == "5")
            {
                cout << s_nums[5] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[6] || num == "6")
        {
            out_num = 6;
            if(num == "6")
            {
                cout << s_nums[6] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[7] || num == "7")
        {
            out_num = 7;
            if(num == "7")
            {
                cout << s_nums[7] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[8] || num == "8")
        {
            out_num = 8;
            if(num == "8")
            {
                cout << s_nums[8] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
        else if(num == s_nums[9] || num == "9")
        {
            out_num = 9;
            if(num == "9")
            {
                cout << s_nums[9] << '\n';
            }
            else
            {
                cout << out_num << '\n';
            }
        }
    }

    return 0;
}

It works fine, but it seems a bit long winded to me and I can't think of a way to really shorten it. I know I don't really need out_num (I was thinking to use it for the subscript originally), but I will need it for the next step that combines this with a calculator. I'd be glad to hear some suggestions.
You can transform the read digit to int, then pass that in to subscript operator of vector

something like

1
2
3
4
5
6
7
8
char digit;
while( cin >> digit ) {
    int n = digit - '0'; // http://www.cplusplus.com/forum/beginner/68260/#msg364068

    if( n < nums.size() ) { // if num entered is between 0 && 9
        std::cout << s_nums[ n ] << " ";
    }
}
You can simplify
1
2
3
4
5
6
7
8
9
10
11
    vector<string> s_nums(10);
        s_nums[0] = "zero";
        s_nums[1] = "one";
        s_nums[2] = "two";
        s_nums[3] = "three";
        s_nums[4] = "four";
        s_nums[5] = "five";
        s_nums[6] = "six";
        s_nums[7] = "seven";
        s_nums[8] = "eight";
        s_nums[9] = "nine";
to
vector<string> s_nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

@Nvrmind

1
2
3
4
5
char digit;
while( cin >> digit ) {
    int n = digit - '0'; // http://www.cplusplus.com/forum/beginner/68260/#msg364068

    if( n < nums.size() ) { // if num entered is between 0 && 9 

Why not simply read into an int? Though I believe the assignment wants the user to enter
zero, one, two, three, four, five, six, seven, eight, nine, 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9
Then convert accordingly.


Maybe something like this to start:
1
2
3
4
if(input[0] >= '0' && input[0] <= '9' && input.size() == 1)
{
    std::cout << s_nums[input[0]-'0'] << std::endl;
}


Converting it the other way would be a bit more tricky.

I would suggest using a std::map<std::string, int> instead of a vector. Or even std::map<std::string, std::string> I suppose that way you could go both ways easier.
http://www.cplusplus.com/reference/map/map/

[edit] fixed code tags.
Last edited on
Why not simply read into an int?
idk what i'm thinking when i wrote that :DD
Thanks guys,

It is a bit tricky having both the numbers and having them spelled out, if it was one or the other it would be simple. I've never use map, but this specifies using a vector so I'm pretty much stuck with that. The next part doesn't specify an output so I won't have to change it to its opposite anymore, just accept either as input. That should simplify it a bit since I can just convert the string to an int and go from there.
Topic archived. No new replies allowed.