How to improve my code

So I need some tips on how to improve this code by making it shorter or so basically you have to input a number from 1-100 and display it's name.

http://pastebin.com/wxK6ffTN

I would really appreciate the help thanks! :)
Please, do not use goto unless you will know how to not use it, so you could adequately assess the tradeoffs between using goto or not.
Seriously, forget that goto exist at all until you will have a good knowledge of C++. It gives you the "easy" way which leads to writing such horrible code.

Look at how names are created. Notice the pattern between digits of the number and number name. Think about how to generate name dynamically. Think about exception. Write code to generate names.
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
#include <iostream>
#include <string>

const std::string ones[] {"", "one", "two", "three", "four", "five",
                          "six", "seven", "eight", "nine"
                         };

const std::string tens[] {"", "ten", "twenty", "thirty", "forty", "fifty",
                          "sixty", "seventy", "eighty", "ninety",
                          "one hundred" /*slight cheating*/
                         };
const std::string special[] {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
                             "sixteen", "seventeen", "eighteen", "nineteen"
                            };
int main()
{
    int num;
    std:: cout<< "Input a value from 1-100: ";
    std::cin >> num;
    while (num<=0 || num>100) {
        std::cout << "ONLY INPUT A VALUE RANGING FROM 1-100!!!\n";
        std::cin >> num;
    }
    std::string name;
    if(10 < num && num < 20) {
        name = special[num % 10];
    } else {
        name = tens[num/10];
        if(num/10 && num%10)
            name += '-';
        name += ones[num % 10];
    }
    std::cout << "The number you've entered is: " << name;
}
Even if you did not think about that, why didn't you just write all number names in one array
1
2
3
4
5
const std::string numbers[] {
    "zero", "one", "two", /*...*/ "ninety-nine", "one hundred" };
//...
std::cin >> num;
std::cout << numbers[num];
Thanks man! Really appreciate it and I would really keep in mind on not using goto in my codes. Thanks! :)
Topic archived. No new replies allowed.