0

i want to numbers in the text entered by the user are converted into text and printed on the screen. Example:

cin>> My School Number is 5674 and i want to "my school number is five six seven four" output like this. I make only Convert to number to text but i cant put together text and numbers please help me

#include <iostream>
using namespace std;
void NumbertoCharacter(int n)
{
    int rev = 0, r = 0;

    while (n > 0) {

        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }

    while (rev > 0) {
        r = rev % 10;

        switch (r) {
        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;
        case 0:
            cout << "zero ";
            break;
        default:
            cout << "invalid ";
            break;
        }
        rev = rev / 10;
    }
}

int main()
{
    int n;
    cin >> n;
    NumbertoCharacter(n);
    return 0;
}

chill389cc
  • 365
  • 1
  • 10
  • So you are asking how to get the number out of "My School Number is 5674" – 001 Oct 28 '22 at 15:26
  • It would be much easier if you built-up a string, and then output the entire string, instead of doing individual `cout` statements. Maybe if you thought about it that way, you would redesign your code to properly get the desired output. – PaulMcKenzie Oct 28 '22 at 15:31
  • Also, shouldn't `My School Number is ` be part of the *output*, and not the input? Why are you inputting the words for the output? The only input you need is the number, and then when that gets converted, you `std::cout << "My School Number is " << x;` given that `x` is the text of the number. – PaulMcKenzie Oct 28 '22 at 15:36
  • i want to write My school Number is 123 and want to give me result like this – deadangel13 Oct 28 '22 at 15:46
  • i want to write My school Number is 123 and want to give me result like this my school number is one two three. İ do just enter the number and convert i cant do it together with text and number. i think use std::cout << "My School Number is " << x; but it doesnt work if numbers are middle in the sentence – deadangel13 Oct 28 '22 at 15:54
  • @ErenAkyol You should write a function that does one thing and one thing only. The `NumberToCharacter` function should take an integer, and return the string of words representing that number. It shouldn't be in the business of doing anything else (no need to validate input, or anything else). – PaulMcKenzie Oct 28 '22 at 16:05
  • I recommend using an array to map digits to their names: `char const * digit_name[] = {"zero", "one", "two", /*... */, "eight", "nine"};` Likewise, you can use an array for the "tens" digits: `char const * tens_name[] = {"twenty", "thirty", "forty", /*... */, "eighty", "ninety"};` The numbers 10..19 are unique don't follow standard rules. :-( – Thomas Matthews Oct 28 '22 at 18:12

3 Answers3

1

It would be much easier if you built the output into a string, instead of using individual cout statements. Then the NumberToCharacter function just needs to build the string and return the results.

The second issue is one of redundancy: You have 10 separate if statements for each digit, when it would be much simpler to have an array that has a string that corresponds to the digit found.

Putting that all together, here is a proposed solution:

#include <iostream>
#include <string>

std::string NumbertoCharacter(int n)
{
    std::string word[] = {"zero", "one", "two", "three", 
                          "four", "five", "six", "seven", 
                          "eight", "nine"};
    std::string output;

    int r;
    while (n > 0) 
    {
       r = n % 10;
       output = word[r] + " " + output;
       n = n / 10;
    }
    return output;
 }

int main()
{
    std::cout << "My School Number is " << NumbertoCharacter(5764) << "\n";
    std::cout << "My School Number is " << NumbertoCharacter(5) << "\n";
    std::cout << "My School Number is " << NumbertoCharacter(50) << "\n";
    return 0;
}

Output:

My School Number is five seven six four 
My School Number is five 
My School Number is five zero 
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
-1

I'll give you high-level plan, no doubts you can implement it:

  • Read input to string
  • Iterate through this string:
    • print leading non-numbers as is
    • print numbers converted as you want Done!
v.v
  • 47
  • 1
  • 6
-1

Here is a solution. The key line is the int charToInt = a - '0';. Here is a link to more details on that technique. If the value returned (charToInt) is between 0 and 9, then the character is a valid integer and can be converted. If not, then just print the original character.

I also changed cin to getline (documentation here) so that your input can accept multiple words.

void NumbertoCharacter(string n)
{
    for (auto a : n) {
        int charToInt = a - '0';
        if (charToInt >= 0 && charToInt <= 9) {
            switch (charToInt) {
                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;
                case 0:
                    cout << "zero ";
                    break;
                default:
                    cout << "invalid ";
                    break;
            }
        }
        else {
            cout << a;
        }
    }
    cout << endl;
}

int main(int argc, char** argv) {
    string n;
    getline(cin, n);
    NumbertoCharacter(n);
    return 0;
}
chill389cc
  • 365
  • 1
  • 10
  • thank you man it works. I want to ask more question. Can i use arrays instead of switch case? and I want all letters in the result to be lowercase can you help me againn – deadangel13 Oct 28 '22 at 16:01
  • @deadangel13 Yes, you could easily use an array instead of switch case. Do you mean like `string[] numbers = ["one","two" ...]`? Then you could just use `cout << numbers[charToInt];` instead of that switch statement. For converting all letters to be lowercase, you can use `tolower(char)` (documentation [here](https://cplusplus.com/reference/cctype/tolower/). To use this you'll need to have `#include ` at the top of your file. – chill389cc Oct 28 '22 at 16:26
  • i do al lower case but i want to learn can i do it with arrays thanks for helping again – deadangel13 Oct 28 '22 at 16:28