Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ program to convert number into words not working
    primarykey
    data
    text
    <p>Here is a program written in c++ from this <a href="https://stackoverflow.com/questions/12284768/write-code-to-convert-given-number-into-words-eg-1234-as-input-should-output-on">Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)</a> question I modified to convert number into words.</p> <p>In my program, instead of repeated using cout, I created an ostream object out and placed the return value in out.</p> <p>Here is the program</p> <pre><code>#include&lt;iostream&gt; using namespace std; ostream &amp; expand(int); int main() { int num; cin&gt;&gt;num; cout&lt;&lt;expand(num); } ostream &amp; expand(int value) { ostream &amp;out; out&lt;&lt;""; 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&lt;0) { out&lt;&lt;"minus "&lt;&lt;expand(-value); } else if(value&gt;=1000000){ out&lt;&lt;expand(value/1000000)&lt;&lt;" million"; if(value % 1000000) { out&lt;&lt;" "&lt;&lt;expand(value % 1000000); } } else if(value&gt;=1000) { out&lt;&lt;expand(value/1000)&lt;&lt;" thousand"; if(value % 1000) { if(value % 1000 &lt; 100) { out &lt;&lt; " and"; } out &lt;&lt; " " &lt;&lt;expand(value % 1000); } } else if(value &gt;= 100) { out&lt;&lt;expand(value / 100)&lt;&lt;" hundred"; if(value % 100) { out &lt;&lt; " and "&lt;&lt;expand (value % 100); } } else if(value &gt;= 20) { out &lt;&lt; tens[value / 10]; if(value % 10) { out &lt;&lt; " " &lt;&lt; expand(value % 10); } } else { out &lt;&lt; ones[value]; } return &amp;out; } </code></pre> <p>However, I get the following error while compiling. </p> <pre><code>In function 'std::ostream&amp; expand(int)': Line 13: error: 'out' declared as reference but not initialized compilation terminated due to -Wfatal-errors. </code></pre> <p>Please help me.</p> <p>I tried setting <code>ostream &amp;out=cout;</code> and at the end <code>return out</code>. But I get the following result for <code>cout&lt;&lt;expand(111234)</code>. </p> <p><code>one0x8050884 hundredeleven and 0x80508840x8050884 thousandtwo0x8050884 hundredthirtyfour 0x8050884 and 0x8050884 0x80508840x8050884</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload