将数字转换为文本时出错 [英] Error with converting numbers to text
问题描述
我编写了一个将数字转换为文本的c ++程序。
我有两个问题(编辑:现在只有一个):
$第一个问题是程序只将数字写成1-19正确,一切从20-99,例如当我写了一个例子34, answear我得到三十,而不是三十四,因为它应该是。三十之后,它只是出现错误,程序关闭。 [问题修复]
第二个问题是,我希望我可以在0-999之间写数字,而不仅仅是99, / p>
#include< iostream>
#include< string>
using namespace std;
int main()
{
int num,Ldight,Rdight;
string ones [] = {zero,one,two,three,four,
five,six,seven 八,九,十,b,十一,十二,十三,十四,十五,十七 ,nineteen};
string tens [] = {二十,三十,四十,五十,六十,七十,八十,九十
cout<< 选择1-99之间的数字:;
cin>> num;
if(num <= 0)
{
cout< 错误! << endl;
}
else if(num> = 0&& num< = 19)
{
cout< 您的号码是:< ones [Rdight];
}
else if(num> = 20&& num< = 99)
{
Rdight = num%10;
Ldight = num / 10;
cout<< 您的号码是:< times [Ldight-2]< ones [Rdight];
}
return 0;
}
您应该更改:
cout< 您的号码是:< times [Ldight-2]< ones [num];
至
cout<< 您的号码是:< times [Ldight-2]< ones [Rdight];
您计算一个从未使用的值。并且在之上的行
num访问索引超出范围。
Im writing a c++ program that convert numbers to text.
I got two problems(edit: just one now):
The first problem is that the program only write out the numbers as 1-19 correct, everything from 20-99 such as when i wrote 34 for an example, the answear i get is thirty and not thirtyfour as it should be. After thirty it just comes errors and the program shut down. [Problem fixed]
The second problem is that i wish i could write numbers between 0-999 and not only 99 but im not sure how to do that
#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; }
You should change this:
cout << "Your number is: " << tens[Ldight - 2] << ones[num];
To
cout << "Your number is: " << tens[Ldight - 2] << ones[Rdight];
You compute a value you never use. And also in the line above ones
num accesses index out of bounds.
这篇关于将数字转换为文本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!