问题
I am a beginner in c++. I have written a program to convert numbers into words. I am getting wrong output for numbers between 11-19 and numbers like this, 20,30,40,50,60,70,80,90. I also tried to add else statement between last two if statements so that It does not execute all the if statements even if the first if is true but it gives me an error that "else without a previous 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";
}
}
Output 1:-
Please enter any number between 0-99: 12
twelve two
Output 2:-
Please enter any number between 0-99: 30
thirty zero
回答1:
You had a semicolon ;
after the second if
. And also, you should use ||
instead of |
. Also you should exlude the case when 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";
}
}
Output for all cases:
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
回答2:
- You should use
int main()
notmain()
and addreturn 0;
inint main()
- The line
if(number<0 | number>99)
should beif(number<0 || number>99)
- The line
if(number>=11 && number <=19);
should beif(number>=11 && number <=19)
- Before
if(ten>=2 && ten<=9)
should addelse
.
The follow code
could work:
//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;
}
回答3:
What you are getting is actually right due to your if
conditions. If you write down 3 if
conditions, it will check the THREE of them. Also, take out the semicolon on the second if!
So the first one, 12, goes to the first if
and prints "twelve".
Goes to the second if
. 12/10
is 1.2
so as an int, it is `1. It does not go in the second if. It is not going in anyways due to the semicolon.
But it goes in the third one: 12%10
.
So it is as you have in your ifs
!
Also, I am worried that this is working for you: if(number<0 | number>99)
, as it should be ||
, and not |
.
回答4:
For cases like this a switch statement makes more sense. Try this code:
#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;
}
I've also found the following bugs:
- you use the bitwise or
|
rather than the boolean or||
in your first if statement. You also don't stop the rest of your code after printing"Number is out of range"
, I've fixed this by returning. - There is a semicolon on the end of your second if statement, this if statement can also be simplified to
ten == 1
. - You need to prevent the rest of the code running after printing the 'teens, this can be done with
else
. - You never print
"ten"
, it should probably be printed with the 'teens but is printed with 20, 30 etc. but will never actually be printed due to the surrounding if statement. - You print "zero" after 20, 30, etc. This can be fixed by only printing
"zero"
iften
is0
. - The if statement
if(unit>=0 && unit <=9)
will always be true so can be removed.
来源:https://stackoverflow.com/questions/53040738/converting-number-into-words-using-if-else