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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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
Transcribed Image Text:```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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY