我正在编写一个将数字转换为文本的 c++ 程序。
我遇到了两个问题(编辑:现在只有一个):
第一个问题是程序只写出正确的 1-19 数字,从 20-99 的所有数字,例如当我写 34 时,我得到的答案是 30,而不是应该的 34。三十之后它只是出现错误并且程序关闭。[问题已解决]
第二个问题是我希望我可以写 0-999 之间的数字而不仅仅是 99 但我不知道该怎么做
#include <iostream> #include <string> using namespace std; int main() { int num, Ldight, Rdight; 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"}; cout << "Pick a number between 1-99: "; cin >> num; if(num <= 0) { cout << "ERROR!" << endl; } else if (num >= 0 && num <= 19) { cout << "Your number is: " << ones[Rdight] ; } else if (num >=20 && num <=99) { Rdight = num % 10; Ldight = num / 10; cout << "Your number is: " << tens[Ldight - 2] << ones[Rdight]; } return 0; }