将数字转换成单词

时间:2018-10-26 21:58:40

标签: c++ numbers word

我是C ++的初学者,我编写了一个程序来将数字打印成0到99之间的单词。该程序运行正常,除了10-20的数字输出错误之外。

    #include<iostream>
    using namespace std;
    main()
    {
        int number,digit_1,digit_2;
        cout<<"Please enter a number between 0-99: ";
        cin >> number;
        digit_1=number/10;
        digit_2=number%10;
        if(number>=10 && number<=19)
        {
        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;
        }
        }
        switch(digit_1)
        {
            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;
        }
            switch(digit_2)
        {
            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;
        }
    }
  

输出:-

     
    

请输入0-99之间的数字:12

         
      

十二点二

    
  

4 个答案:

答案 0 :(得分:1)

在“ if”块结束之后,您缺少“ else”语句。

如果数字在10到19之间,则要用第一个开关输入第一个程序段。

否则,(即数字不在10到19之间)您要使用最后两个switch语句将数字划分为数字。

您的问题是,如果当前数字在10到19之间,则将运行所有三个switch语句。第一个将降落在“十二”中,第二个将不打印任何内容,因为没有“情况1”,而第三个将打印“两个”。

包围最后两个开关块
else 
{
    switch(digit_1) {
        //...
    }
    switch(digit_2) {
        //...
    }
}

答案 1 :(得分:1)

switch (digit_1) {
    case 1:
        switch (digit_2) {
            case 0: cout << "Ten"; break;
            case 1: cout << "Eleven"; break;
            case 2: cout << "Twelve"; break;
            case 3: cout << "Thirteen"; break;
            case 4: cout << "Fourteen"; break;
            case 5: cout << "Fifteen"; break;
            case 6: cout << "Sixteen"; break;
            case 7: cout << "Seventeen"; break;
            case 8: cout << "Eighteen"; break;
            case 9: cout << "Nineteen"; break;
        }
        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;
}
if (digit_1 != 1) {
    if (digit_1) cout << " ";

    switch (digit_2) {
        case 0: if (!digit_1) 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;
    }
}

答案 2 :(得分:0)

在第一个切换状态之后,您的程序不会停止执行解决方案:

using namespace std;
    main()
    {
        int number,digit_1,digit_2;
        cout<<"Please enter a number between 0-99: ";
        cin >> number;
        digit_1=number/10;
        digit_2=number%10;
        if(number>=10 && number<=19)
        {
        switch(number)
        {
            case 10: cout<<"Ten"; return 0;    
            case 11: cout<<"Eleven"; return 0;
            case 12: cout<<"Twelve"; return 0;
            case 13: cout<<"Thirteen"; return 0;
            case 14: cout<<"Fourteen"; return 0;
            case 15: cout<<"Fifteen"; return 0;
            case 16: cout<<"Sixteen"; return 0;
            case 17: cout<<"Seventeen"; return 0;
            case 18: cout<<"Eighteen"; return 0;
            case 19: cout<<"Nineteen"; return 0;
        }
        }
        switch(digit_1)
        {
            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;
        }
            switch(digit_2)
        {
            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;
        }
    }

using namespace std;
    main()
    {
        int number,digit_1,digit_2;
        cout<<"Please enter a number between 0-99: ";
        cin >> number;
        digit_1=number/10;
        digit_2=number%10;
        if(number>=10 && number<=19)
        {
        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;
        }
        }
else {
        switch(digit_1)
        {
            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;
        }
            switch(digit_2)
        {
            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;
        }
    }
}

答案 3 :(得分:-1)

我认为结果是正确的。12在10到19之间,那么cout“ Twelve”。数字_1是1,但不包括在内。数字_2是2,然后cout“ Two”。所以结果是“ Twelve Two”