Преобразование числа в слова с использованием if else - PullRequest
0 голосов
/ 29 октября 2018

Я новичок в с ++. Я написал программу для преобразования чисел в слова. Я получаю неправильный вывод для чисел между 11-19 и числами как это, 20,30,40,50,60,70,80,90. Я также попытался добавить оператор else между двумя последними операторами if, чтобы он не выполнял все операторы if, даже если первый if равен true, но он выдает мне ошибку «else без предыдущего if».

//This program converts number into words
#include<iostream>
using namespace std;
main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin>>number;
    ten=number/10;
    unit=number%10;
    if(number<0 | number>99)
        cout<<"Number is out of range";
    if(number>=11 && number <=19);
    {
        if(number==11) cout<<"eleven"; 
        if(number==12) cout<<"twelve";
        if(number==13) cout<<"thirteen"; 
        if(number==14) cout<<"fourteen";
        if(number==15) cout<<"fifteen";
        if(number==16) cout<<"sixteen";
        if(number==17) cout<<"seventeen";
        if(number==18) cout<<"eighteen";
        if(number==19) cout<<"ninteen";
    }
    if(ten>=2 && ten<=9)    
    {   
        if(ten==1) cout<<"ten";
        if(ten==2) cout<<"twenty";
        if(ten==3) cout<<"thirty";
        if(ten==4) cout<<"fourty";
        if(ten==5) cout<<"fifty";
        if(ten==6) cout<<"sixty";
        if(ten==7) cout<<"seventy";
        if(ten==8) cout<<"eighty";
        if(ten==9) cout<<"ninty";
    }
    if(unit>=0 && unit <=9)
    {
        if(unit==0) cout<<" zero";
        if(unit==1) cout<<" one";
        if(unit==2) cout<<" two";
        if(unit==3) cout<<" three";
        if(unit==4) cout<<" four";
        if(unit==5) cout<<" five";
        if(unit==6) cout<<" six";
        if(unit==7) cout<<" seven";
        if(unit==8) cout<<" eight";
        if(unit==9) cout<<" nine";
    }
}

Выход 1: -

Please enter any number between 0-99: 12
twelve two

Выход 2: -

Please enter any number between 0-99: 30
thirty zero

Ответы [ 4 ]

0 голосов
/ 29 октября 2018

В таких случаях оператор switch имеет больше смысла. Попробуйте этот код:

#include<iostream>
using namespace std;
int main()
{
    for (int number = 1; number <= 99; number++)
    {
        int ten=number/10;
        int unit=number%10;
        if(number<0 || number>99)
        {
            cout<<"Number is out of range";
            return 1;
        }
        if(ten == 1)
        {
            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
        {
            if(ten>=2 && ten<=9)    
            {   
                switch (ten)
                {
                case 2: cout<<"twenty"; break;
                case 3: cout<<"thirty"; break;
                case 4: cout<<"fourty"; break;
                case 5: cout<<"fifty"; break;
                case 6: cout<<"sixty"; break;
                case 7: cout<<"seventy"; break;
                case 8: cout<<"eighty"; break;
                case 9: cout<<"ninty"; break;
                }
            }
            if ( unit != 0 and ten != 0 )
            {
                cout << " ";
            }
            switch ( unit )
            {
                case 0: if ( ten == 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 << "\n";
    }
    return 0;
}

Я также нашел следующие ошибки:

  1. вы используете битовый или | вместо логического или || в вашем первом операторе if. Вы также не останавливаете остальную часть своего кода после печати "Number is out of range", я исправил это, вернув.
  2. В конце вашего второго оператора if стоит точка с запятой, этот оператор if также можно упростить до ten == 1.
  3. Вам нужно запретить выполнение остальной части кода после печати подростков, это можно сделать с помощью else.
  4. Вы никогда не печатаете "ten", вероятно, оно должно быть напечатано с 'подростками, но напечатано с 20, 30 и т. Д., Но фактически никогда не будет напечатано из-за окружающего оператора if.
  5. Вы печатаете «ноль» после 20, 30 и т. Д. Это можно исправить, только напечатав "zero", если ten равно 0.
  6. Оператор if if(unit>=0 && unit <=9) всегда будет истинным, поэтому его можно удалить.
0 голосов
/ 29 октября 2018

У вас была точка с запятой ; после второго if. А также, вы должны использовать || вместо |. Также следует исключить случай, когда ten==1.

//This program converts number into words
#include<iostream>
using namespace std;
main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin>>number;
    ten=number/10;
    unit=number%10;
    if(number<0 || number>99)
    {
        cout<<"Number is out of range";
    }
    if(ten == 1)
    {
        if(number==10) cout<<"ten"; 
        if(number==11) cout<<"eleven"; 
        if(number==12) cout<<"twelve";
        if(number==13) cout<<"thirteen"; 
        if(number==14) cout<<"fourteen";
        if(number==15) cout<<"fifteen";
        if(number==16) cout<<"sixteen";
        if(number==17) cout<<"seventeen";
        if(number==18) cout<<"eighteen";
        if(number==19) cout<<"ninteen";
    }
    else {   
        if(ten==2) cout<<"twenty";
        if(ten==3) cout<<"thirty";
        if(ten==4) cout<<"fourty";
        if(ten==5) cout<<"fifty";
        if(ten==6) cout<<"sixty";
        if(ten==7) cout<<"seventy";
        if(ten==8) cout<<"eighty";
        if(ten==9) cout<<"ninty";

        // ****
        if(unit==0 && ten ==0) cout<<" zero";
        if(unit==1) cout<<" one";
        if(unit==2) cout<<" two";
        if(unit==3) cout<<" three";
        if(unit==4) cout<<" four";
        if(unit==5) cout<<" five";
        if(unit==6) cout<<" six";
        if(unit==7) cout<<" seven";
        if(unit==8) cout<<" eight";
        if(unit==9) cout<<" nine";
    }
}

Выход для всех случаев:

Please enter any number between 0-99: -1
Number is out of range

Please enter any number between 0-99: 0
 zero

Please enter any number between 0-99: 1
 one

Please enter any number between 0-99: 2
 two

Please enter any number between 0-99: 3
 three

Please enter any number between 0-99: 4
 four

Please enter any number between 0-99: 5
 five

Please enter any number between 0-99: 6
 six

Please enter any number between 0-99: 7
 seven

Please enter any number between 0-99: 8
 eight

Please enter any number between 0-99: 9
 nine

Please enter any number between 0-99: 10
ten

Please enter any number between 0-99: 11
eleven

Please enter any number between 0-99: 12
twelve

Please enter any number between 0-99: 13
thirteen

Please enter any number between 0-99: 14
fourteen

Please enter any number between 0-99: 15
fifteen

Please enter any number between 0-99: 16
sixteen

Please enter any number between 0-99: 17
seventeen

Please enter any number between 0-99: 18
eighteen

Please enter any number between 0-99: 19
ninteen

Please enter any number between 0-99: 20
twenty

Please enter any number between 0-99: 21
twenty one

Please enter any number between 0-99: 22
twenty two

Please enter any number between 0-99: 23
twenty three

Please enter any number between 0-99: 24
twenty four

Please enter any number between 0-99: 25
twenty five

Please enter any number between 0-99: 26
twenty six

Please enter any number between 0-99: 27
twenty seven

Please enter any number between 0-99: 28
twenty eight

Please enter any number between 0-99: 29
twenty nine

Please enter any number between 0-99: 30
thirty

Please enter any number between 0-99: 31
thirty one

Please enter any number between 0-99: 32
thirty two

Please enter any number between 0-99: 33
thirty three

Please enter any number between 0-99: 34
thirty four

Please enter any number between 0-99: 35
thirty five

Please enter any number between 0-99: 36
thirty six

Please enter any number between 0-99: 37
thirty seven

Please enter any number between 0-99: 38
thirty eight

Please enter any number between 0-99: 39
thirty nine

Please enter any number between 0-99: 40
fourty

Please enter any number between 0-99: 41
fourty one

Please enter any number between 0-99: 42
fourty two

Please enter any number between 0-99: 43
fourty three

Please enter any number between 0-99: 44
fourty four

Please enter any number between 0-99: 45
fourty five

Please enter any number between 0-99: 46
fourty six

Please enter any number between 0-99: 47
fourty seven

Please enter any number between 0-99: 48
fourty eight

Please enter any number between 0-99: 49
fourty nine

Please enter any number between 0-99: 50
fifty

Please enter any number between 0-99: 51
fifty one

Please enter any number between 0-99: 52
fifty two

Please enter any number between 0-99: 53
fifty three

Please enter any number between 0-99: 54
fifty four

Please enter any number between 0-99: 55
fifty five

Please enter any number between 0-99: 56
fifty six

Please enter any number between 0-99: 57
fifty seven

Please enter any number between 0-99: 58
fifty eight

Please enter any number between 0-99: 59
fifty nine

Please enter any number between 0-99: 60
sixty

Please enter any number between 0-99: 61
sixty one

Please enter any number between 0-99: 62
sixty two

Please enter any number between 0-99: 63
sixty three

Please enter any number between 0-99: 64
sixty four

Please enter any number between 0-99: 65
sixty five

Please enter any number between 0-99: 66
sixty six

Please enter any number between 0-99: 67
sixty seven

Please enter any number between 0-99: 68
sixty eight

Please enter any number between 0-99: 69
sixty nine

Please enter any number between 0-99: 70
seventy

Please enter any number between 0-99: 71
seventy one

Please enter any number between 0-99: 72
seventy two

Please enter any number between 0-99: 73
seventy three

Please enter any number between 0-99: 74
seventy four

Please enter any number between 0-99: 75
seventy five

Please enter any number between 0-99: 76
seventy six

Please enter any number between 0-99: 77
seventy seven

Please enter any number between 0-99: 78
seventy eight

Please enter any number between 0-99: 79
seventy nine

Please enter any number between 0-99: 80
eighty

Please enter any number between 0-99: 81
eighty one

Please enter any number between 0-99: 82
eighty two

Please enter any number between 0-99: 83
eighty three

Please enter any number between 0-99: 84
eighty four

Please enter any number between 0-99: 85
eighty five

Please enter any number between 0-99: 86
eighty six

Please enter any number between 0-99: 87
eighty seven

Please enter any number between 0-99: 88
eighty eight

Please enter any number between 0-99: 89
eighty nine

Please enter any number between 0-99: 90
ninty

Please enter any number between 0-99: 91
ninty one

Please enter any number between 0-99: 92
ninty two

Please enter any number between 0-99: 93
ninty three

Please enter any number between 0-99: 94
ninty four

Please enter any number between 0-99: 95
ninty five

Please enter any number between 0-99: 96
ninty six

Please enter any number between 0-99: 97
ninty seven

Please enter any number between 0-99: 98
ninty eight

Please enter any number between 0-99: 99
ninty nine

Please enter any number between 0-99: 100
Number is out of range
0 голосов
/ 29 октября 2018
  1. Вы должны использовать int main(), а не main() и добавить return 0; в int main()
  2. Строка if(number<0 | number>99) должна быть if(number<0 || number>99)
  3. Строка if(number>=11 && number <=19); должна быть if(number>=11 && number <=19)
  4. До if(ten>=2 && ten<=9) следует добавить else.

Может работать следующее code:

//This program converts number into words
#include<iostream>
using namespace std;
int main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin>>number;
    ten=number/10;
    unit=number%10;
    if(number<0 || number>99)
        cout<<"Number is out of range";
    if(number>=11 && number <=19)
    {
        if(number==11) cout<<"eleven";
        if(number==12) cout<<"twelve";
        if(number==13) cout<<"thirteen";
        if(number==14) cout<<"fourteen";
        if(number==15) cout<<"fifteen";
        if(number==16) cout<<"sixteen";
        if(number==17) cout<<"seventeen";
        if(number==18) cout<<"eighteen";
        if(number==19) cout<<"ninteen";
    } else {
        if(ten>=2 && ten<=9)
        {
            if(ten==1) cout<<"ten";
            if(ten==2) cout<<"twenty";
            if(ten==3) cout<<"thirty";
            if(ten==4) cout<<"fourty";
            if(ten==5) cout<<"fifty";
            if(ten==6) cout<<"sixty";
            if(ten==7) cout<<"seventy";
            if(ten==8) cout<<"eighty";
            if(ten==9) cout<<"ninty";
        }
        if(unit>=0 && unit <=9)
        {
            if(unit==0) cout<<" zero";
            if(unit==1) cout<<" one";
            if(unit==2) cout<<" two";
            if(unit==3) cout<<" three";
            if(unit==4) cout<<" four";
            if(unit==5) cout<<" five";
            if(unit==6) cout<<" six";
            if(unit==7) cout<<" seven";
            if(unit==8) cout<<" eight";
            if(unit==9) cout<<" nine";
        }
    }

    return 0;
}
0 голосов
/ 29 октября 2018

То, что вы получаете, на самом деле правильно из-за ваших if условий. Если вы запишите 3 if условия, он проверит ТРИ из них. Также выньте точку с запятой на втором, если!

Итак, первый, 12, идет к первому if и печатает «двенадцать».

Переход ко второму if. 12/10 равно 1.2, поэтому как int это `1. Это не идет во втором, если. В любом случае это не происходит из-за точки с запятой.

Но это идет в третьем: 12%10.

Так же, как у вас в ifs!

Кроме того, я беспокоюсь, что это работает для вас: if(number<0 | number>99), как и должно быть ||, а не |.

...