Help me fix my code. I have a problem my code when I input 112 output one hundred twelve two I want fix when I put 112 out one hundred twelve. Homework Write a program that reads a whole number of up to nine digits and prints it in words. For example, the input 13247 ought to produce "thirteen thousand two hundred forty seven" Please use my code. Thank you so much
Help me fix my code. I have a problem my code when I input 112 output one hundred twelve two
I want fix when I put 112 out one hundred twelve.
Homework Write a program that reads a whole number of up to nine digits and prints it in words. For example, the input 13247 ought to produce "thirteen thousand two hundred forty seven"
Please use my code. Thank you so much
#include <iostream>
using namespace std;
void breakapart(int n, int &a, int &b, int &c);
void writeNum(int digit);
void writeSingle(int n);
void writeTens(int tensD, int onesD);
int main()
{
int num, first, second, third;
cout<<"Input a 9 digit number "<<endl;
cin>>num;
// Break the number into three three-digit numbers
breakapart(num,first,second,third);
writeNum(first);
// If number is above or equal to one million, print name million
if(num>=1000000)
{
cout<<"million ";
}
writeNum(second);
// If number second in break not equal to 0, print name thousand
if(second!=0)
{
cout<<"thousand ";
}
writeNum(third);
return 0;
}
//Break the number into three three-digit numbers
void breakapart(int n, int &a, int &b, int &c)
{
c = n%1000;
n = n/1000;
b = n%1000;
n = n/1000;
a = n%1000;
n = n/1000;
}
// Read number one digit
void writeSingle(int digit)
{
switch(digit)
{
case 1: cout<<"one "; break;
case 2: cout<<"two "; break;
case 3: cout<<"three "; break;
case 4: cout<<"four "; break;
case 5: cout<<"five "; break;
case 6: cout<<"six "; break;
case 7: cout<<"seven "; break;
case 8: cout<<"eight "; break;
case 9: cout<<"nine "; break;
}
}
// Read number ten digit
void writeTens(int tensD, int onesD)
{
switch(tensD)
{
case 1:
switch(onesD)
{
case 0: cout<<"ten "; break;
case 1: cout<<"eleven "; break;
case 2: cout<<"twelve "; break;
case 3: cout<<"thirteen "; break;
case 4: cout<<"fourteen "; break;
case 5: cout<<"fifteen "; break;
case 6: cout<<"sixteen "; break;
case 7: cout<<"seventeen "; break;
case 8: cout<<"eighteen "; break;
case 9: cout<<"nineteen "; break;
}
break;
case 2: cout<<"twenty "; break;
case 3: cout<<"thirty "; break;
case 4: cout<<"forty "; break;
case 5: cout<<"fifty "; break;
case 6: cout<<"sixty "; break;
case 7: cout<<"seventy "; break;
case 8: cout<<"eighty "; break;
case 9: cout<<"ninty "; break;
}
}
void writeNum(int n)
{
// break number into single digit
int one, two, three;
three = n%10;
n = n/10;
two = n%10;
n = n/10;
one = n;
writeSingle(one);
// If number one above equal to 0, print name hundred
if(one > 0)
{
cout<<"hundred ";
}
writeTens(two, three);
writeSingle(three);
}
![```cpp
void writeNum(int n)
{
// break number into single digit
int one, two, three;
three = n % 10;
n = n / 10;
two = n % 10;
n = n / 10;
one = n;
writeSingle(one);
// If number one above equal to 0, print name hundred
if (one > 0)
{
cout << "hundred ";
}
writeTens(two, three);
writeSingle(three); // Not if n is in the teens.
}
```
### Explanation
This code snippet is a C++ function called `writeNum` which takes an integer `n` and breaks it into its component digits (ones, tens, and hundreds). Here's what each part does:
1. **Variable Declaration and Initialization:**
- Declares three integer variables: `one`, `two`, and `three`.
- These variables represent the hundreds, tens, and ones places of the number `n`.
2. **Breaking Down the Number:**
- `three = n % 10;` retrieves the last digit (ones place) of `n`.
- `n = n / 10;` removes the last digit from `n`.
- `two = n % 10;` retrieves the next digit (tens place).
- `n = n / 10;` further reduces `n`.
- `one = n;` assigns the remaining digit (hundreds place) to `one`.
3. **Output Logic:**
- Calls `writeSingle(one);` to output the hundreds place if non-zero.
- If `one` is greater than 0, outputs the word "hundred" followed by a space.
- Calls `writeTens(two, three);` to handle the tens and ones places, except when the number is in the teens.
- Calls `writeSingle(three);` to output the ones place unless the number is in the teens.
4. **Graph/Diagram Explanation:**
- An arrow points to `writeSingle(three);` with a note "Not if n is in the teens," suggesting a condition might be missing to correctly handle numbers between 10 and 19.
This function is part of a system that likely converts numbers to their word representations, and highlights the importance of](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F7da71b53-e1d4-41a9-91b2-9ce061cdeaff%2Fa03f954b-9d2e-4c9f-bf29-07cc04ac4bbc%2F3zbzfbl_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)