C/C++ Printing Integer Digits as Words in C++

AI Thread Summary
The discussion focuses on creating a C++ program that reads an integer and prints each digit in word form. Initial attempts included basic input and output, but the logic for processing the digits was incomplete. Participants emphasized the importance of understanding the problem conceptually before coding, suggesting that one should outline the steps to extract and print each digit. Key considerations include handling various integer lengths, negative numbers, and invalid inputs like letters or punctuation. It was advised to use loops for iterating through digits and to structure the code properly with attention to readability and standard practices. The discussion also highlighted the need for a complete program structure at each coding step and the use of code formatting tools in the forum for clarity.
Raghav Gupta
Messages
1,010
Reaction score
76
Write a “C++” program to read any integer number and print each digit into word.
I went like this

# include <iostream.h>
#include <conio.h>
void main(){
int a,b,c,d;
clrscr();
b=a
cout<<"Enter any number"<<endl;
cin>>a;
c=a%10;
d=a/10;
I am not getting further logic.
someone was saying for loop we also have to apply.
 
Technology news on Phys.org
This is clearly homework. So I will only give hints.

Do not start by writing C++ code. That comes only very near the end.

Can you solve the problem "on paper?" I mean, can you work out the steps that you would take to get the results you want?

If you can't then it is NOT time to start writing computer code. You need to stop and be sure you understand what it is you are meant to accomplish.

Work out each step in detail. Work out how you would know when you were done. Work out how you would get each next digit, and what you would do to determine the next digit. Work out whether you will print out the digits as you get them or if you will store them and print them out all at once. Work out which order you will print them, meaning if the integer is 146 do you print 1 then 4 then 6, or do you print 6 then 4 then 1?

Work through your steps with several example integers. Be sure to use different length integers. Be sure to include cases like 4000 with several trailing 0's. Be sure to include cases like -45 with a negative sign. Be sure to include 5, one digit, 37, two digits, 389484, a bunch of digits.

Be sure to think about cases where the user types in letters or punctuation marks instead of integers, and figure out what your program should do about that. Maybe the spec of the problem does not include this, so the behaviour of your program satisfies the spec no matter what it does in this case.

Once you have all the rules and steps worked out, only then is it time to start writing computer code.

C++ is based on certain standard structures. Using "void main" is non-standard, but many compilers let you do it. The routine clscr() isn't standard either, but again, many compilers let you away with it.

You have not got a complete program. When you are coding, always be sure you have a complete program at each step. For example, make sure you balance all { and } as soon as you type them. So when you put in {, immediately go put in the balancing }.

Use white space to your advantage. Line up { with } in this kind of fashion.
{
... stuff
}

Note that the { and } should be in the same column on different lines. Don't jam it all on one line like this {...stuff} because that is hard to read. If you have another layer of {} inside the first layer, then indent the inside layer.
{
<tab here> {
<tab here> ... stuff
<tab here> }
}

The forum editor does not like leading white space, so where it says <tab here> you should put in a tab.

Your understanding of the problem will have some kind of thing like this: Determine how long the integer is. Figure out the next digit. Keep outputting digits for that long. You need to figure out how to do that. It will probably be either a for loop or a while loop. You need to look in your text and find those and figure out how to enter them.
 
Tip: when posting code in this forum, enclose it in "code tags" like this:

C:
#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello, world!" << endl;
    return 0;
}

Then it will preserve any spaces that you use at the beginning of a line to indent your code. In the code above, I actually indented two lines. The forum software strips out the extra spaces in "normal" text, but not in "code" text.

Code:
#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello, world!" << endl;
    return 0;
}

Also, if you put the language name in the code tag, the software can colorize certain features of your code to make it easier to read. (I used "C" instead of "C++" because this trick apparently treats them as the same)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top