尝试将数字从1-99转换成英语,但老实说,我不知道我错过了什么。每当我输入20这样的数字时,它就不会像它应该那样输出Twenty。任何帮助,将不胜感激! :)
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
cout << "In this program we will convert integers to English" << endl;
int Number, leftDigit, rightDigit, yes;
yes = 1;
string ones[] = {" Zero"
" One "
" Two "
" Three "
" Four "
" Five "
" Six "
" Seven "
" Eight "
" Nine "
" Ten "
" Eleven "
" Twelve "
" Thirteen"
" Fourteen "
" Fifteen "
" Sixteen "
" Seventeen "
" Eighteen "
" Nineteen " };
string tens[] = {" Twenty "
" Thirty "
" Fourty "
" Fifty "
" Sixty "
" Seventy "
" Eighty "
" Ninety "};
do{
cout << "Enter any integer value: " << endl;
cin >> Number;
if ( Number <= 0 || Number >= 100 )
{
cout << "The number is not between 1 and 99" << endl;
}
else ( Number >= 1 && Number <= 19 )
{
cout << "The number you have entered is: " << ones[Number] << endl;
}
else ( Number >= 20 && Number <= 99)
{
leftDigit = Number / 10;
rightDigit = Number % 10;
cout << "The number you have entered is: " << tens[leftDigit - 2 ] << " " << ones[rightDigit] << endl;
}
} while (yes == 1);
return 0;
}
答案 0 :(得分:4)
您的代码中有两个问题:
首先,您需要在string ones[]
和string tens[]
之间的参数之间添加逗号:
string tens[] = { " Twenty ",
" Thirty ",
" Fourty ",
" Fifty ",
" Sixty ",
" Seventy ",
" Eighty ",
" Ninety " };
第二次,您需要在每个if
中添加else
,如下所示:
// ..
else if (Number >= 1 && Number <= 19)
{
cout << "The number you have entered is: " << ones[Number] << endl;
}
// ..
修复这两个问题后,代码结果: