Saluton Mundo,我一直致力于此功能,根据数字打印支票上的字样。唯一的问题是我把它变成了一个空白,我不知道如何转换为void->串。如果你问我,基本上不可能。简而言之,我如何重写它以便它返回一个字符串。
我的代码如下:
void numALet(string &can)
{
int conNum= atoi(can.c_str());
float conNumF= atof(can.c_str());
//Snippet to find the Cents.
float cen= (int)((conNumF- conNum)* 100);
string basic[20]= { " ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"},
diez[9]= { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
if(conNum< 20)
cout<< basic[conNum]<< " and "<< (cen+ 1) << " Cents.";
else if(conNum< 100 && conNum> 19)
{
int num= conNum% 100;
if(num% 10!= 0)
cout<< diez[num/10- 1]<< " "<< basic[num% 10]<< " and "<< (cen+ 1) << " Cents.";
else
cout<< diez[conNum/ 10- 1]<< " and "<< (cen+ 1)<< " Cents.";
}
else if(conNum> 99 && conNum< 1000)
{
int num1= conNum% 1000;
cout<< basic[num1/ 100]<< " "<< "Hundred ";
int num2= conNum% 100;
if(conNum< 20)
cout<< basic[num2]<< " and "<< (cen+ 1) << " Cents.";
else if(num2< 100 && num2> 19)
{
if(num2% 10!= 0)
cout<< diez[num2/ 10- 1]<< " "<< basic[num2% 10]<< " and "<< (cen+ 1) << " Cents.";
else
cout<< diez[num2/ 10- 1]<< " and "<< (cen+ 1) << " Cents.";
}
}
else if(conNum> 999 && conNum< 10000)
{
cout<< basic[conNum/ 1000]<< " "<< "Thousand ";
int num1= conNum% 1000;
cout<< basic[num1/ 100]<< " "<< "Hundred ";
int num2= conNum% 100;
if(conNum< 20)
cout<< basic[num2]<< " and "<< cen << " Cents.";
else if(num2< 100 && num2> 19)
{
if(num2% 10!= 0)
cout<< diez[num2/ 10- 1]<< " "<< basic[num2% 10]<< " and "<< (cen) << " Cents.";
else
cout<< diez[num2/ 10- 1];
}
}
}
输出:
九千九百九十九和九十九分。
P.S:当调用cen
时,它突然返回99,100,这就是为什么我加了+1。
感谢您的帮助。
答案 0 :(得分:2)
首先,包括lib:
#include <sstream>
然后,实例化一个std :: stringstream:
std::stringstream ss;
将cout<<
替换为ss<<
。
最后,将字符串流作为字符串返回。
return ss.str();