c++程序将数字转换为不起作用的单词

标签 c++ ostream

这是来自 Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four) 的用 C++ 编写的程序我修改了将数字转换为单词的问题。

在我的程序中,我没有重复使用 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 段:

A variable declared to be a T& or T&&, that is, “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T.

如果您打算将 out 绑定(bind)到标准输出,那么您可以这样做:

ostream& out = std::cout;

或者,您可以将 out 作为函数的参数,默认绑定(bind)到 std::cout:

ostream& expand(int value, ostream& out = std::cout)
{
    out << "";
    // ...
}

这样,客户端可以调用 expand() 并让它输出到指定的流(例如 std::ostringstreamstd::ofstream ),如果没有指定流,则输出到标准输出。

关于c++程序将数字转换为不起作用的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783380/

相关文章:

c++ - ostream 是如何管理内存的?

通用模板 ostream << 运算符的 C++ 不明确重载

c++ - 函数指针 vector

c++ - fstream、ofstream、ostream、iostream 之间的区别

c++ - 子对象上的 ostream 获取其父对象的 ostream

c++ - 通过 GetProcAddress 提取的函数指针使应用程序崩溃

C++ - 试图重载 "<<"运算符

c++ - DLL 导出构造函数有导致堆损坏的风险

c++ - 通过 Mex 文件与 C++ 对象交互

c++ - MSVCP110D.dll 和 Visual Studio 2013