C ++程序将数字转换为单词不起作用
c++ program to convert number into words not working
这是一个用 c++ 编写的程序,从这个 编写代码 将给定的数字转换为单词(例如 1234 作为输入应该输出一千二百三十四(我修改的问题将数字转换为单词。
在我的程序中,我没有使用 cout 重复,而是创建了一个 ostream 对象 out 并将返回值放在 out 中。
这是程序
#include<iostream>
using namespace std;
ostream & expand(int);
int main()
{
int num;
cin>>num;
cout<<expand(num);
}
ostream & expand(int value)
{
ostream &out;
out<<"";
const char * const ones[21] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
if(value<0)
{
out<<"minus "<<expand(-value);
}
else if(value>=1000000){
out<<expand(value/1000000)<<" million";
if(value % 1000000)
{
out<<" "<<expand(value % 1000000);
}
}
else if(value>=1000)
{
out<<expand(value/1000)<<" thousand";
if(value % 1000)
{
if(value % 1000 < 100)
{
out << " and";
}
out << " " <<expand(value % 1000);
}
}
else if(value >= 100)
{
out<<expand(value / 100)<<" hundred";
if(value % 100)
{
out << " and "<<expand (value % 100);
}
}
else if(value >= 20)
{
out << tens[value / 10];
if(value % 10)
{
out << " " << expand(value % 10);
}
}
else
{
out << ones[value];
}
return &out;
}
但是,我在编译时收到以下错误。
In function 'std::ostream& expand(int)':
Line 13: error: 'out' declared as reference but not initialized
compilation terminated due to -Wfatal-errors.
请帮助我。
我尝试设置ostream &out=cout;
,最后return out
.但是我得到以下结果cout<<expand(111234)
.
one0x8050884 hundredeleven and 0x80508840x8050884 thousandtwo0x8050884 hundredthirtyfour 0x8050884 and 0x8050884 0x80508840x8050884
问题就在这里:
ostream &out;
正如编译器告诉您的那样,必须初始化引用。它们不能是默认构造的。根据 C++11 标准的第 8.3.5/1 段:
声明为 T& 或 T&&&的变量,即"对类型 T 的引用"(8.3.2(,应由对象初始化, 或函数,类型 T 或由可转换为 T 的对象。
如果您打算将out
绑定到标准输出,则可以这样做:
ostream& out = std::cout;
或者,您可以将out
作为函数的参数,默认情况下绑定到 std::cout
:
ostream& expand(int value, ostream& out = std::cout)
{
out << "";
// ...
}
这样,客户端可以调用expand()
并让它输出到指定的流(例如std::ostringstream
或std::ofstream
(,如果未指定流,则输出到标准输出。
ostream &out; // Error
必须通过对象初始化out
。因为引用总是必须引用某些东西。
ostream &out = cout;
如果你想像这样链接输出,你应该使用 Andy 答案中给出的方法。目前,系统正在尝试将 ostream 指针输出为值,它无法正确链接,因为您没有提供流作为输入参数。
#include<stdio.h>
#include<conio.h>
void two_dgt(int);
void three_dgt(int); //Function Declarations
void four_dgt(int);
long int num,hd_place,th_place;
char single[10][6] = {"","one","two","three","four",
"five","six","seven","eight","nine"};
char tens_multi[10][8] = {"","ten","twenty","thirty","forty",
"fifty","sixty","seventy","eighty","ninety"};
char dual[10][10] = {"ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
void main()
{
clrscr();
printf("Enter the number upto 5 digits: ");
scanf("%ld",&num);
if(num == 0)
printf("Zero");
else
{
clrscr();
printf("nnEntered number: %ld",num);
printf("nIn words: ");
if(num < 10)
printf("%s",single[num]); //use to print single digit number
else if(num < 100)
two_dgt(num); //call a function to print two digits
else if(num < 1000)
three_dgt(num); //call a function to print three digits
else if(num < 10000)
four_dgt(num); //call a function to print four digits
else if(num < 100000)
{
th_place = num/1000;
two_dgt(th_place);
printf(" thousand ");
num = num - (th_place*1000);
hd_place = num/100;
if(hd_place == 0)
two_dgt(num);
else
three_dgt(num);
}
else if(num > 99999)
{
clrscr();
printf("nError: number range should be upto 5 digits");
}
}
getch();
}
void two_dgt(int num)
{
if(num%10 == 0)
printf("%s",tens_multi[num/10]);
else
{
if(num/10 == 1)
printf("%s",dual[num%10]);
else
printf("%s %s",tens_multi[num/10],single[num%10]);
}
}
void three_dgt(int num)
{
hd_place = num/100;
printf("%s hundred ",single[hd_place]);
num = num - (hd_place*100);
two_dgt(num);
}
void four_dgt(int num)
{
th_place = num/1000;
printf("%s thousand ",single[th_place]);
num = num - (th_place*1000);
hd_place = num/100;
if(hd_place == 0)
two_dgt(num);
else
three_dgt(num);
}
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我需要在C++中构建一些单词搜索功能,但似乎它不起作用.任何线索
- C ++程序将数字转换为单词不起作用