Home > OS >  how to print multiple numbers to words
how to print multiple numbers to words

Time:11-18

in my code, i don't understand why zero doesn't print i did all possible solutions that I know but it doesn't print zero.

#include <iostream>
using namespace std;

int main(){
    int digits; 
    int numberOne = 0;
    int integer;
    
    cout<<"Enter the number: ";
    cin>>digits;
    while (digits != 0) {
        numberOne = (numberOne * 10)   (digits % 10);
        digits /= 10;
    }
    for (integer = numberOne; integer > 0; integer = integer / 10){
        switch (integer  % 10) {
            case 0:
                cout<<"Zero\n";
                break;
            case 1:
                cout<<"One\n";
                break;
            case 2:
                cout<<"Two\n";
                break;
            case 3:
                cout<<"Three\n";
                break;
            case 4:
                cout<<"Four\n";
                break;
            case 5:
                cout<<"Five\n";
                break;
            case 6:
                cout<<"Six\n";
                break;
            case 7:
                cout<<"Seven\n";
                break;
            case 8:
                cout<<"Eight\n";
                break;
            case 9:
                cout<<"Nine\n";
                break;
        }
    }
        return 0;
    }

zero doesn't print how do I fix it? Expected output is 900 (nine zero zero) but zero doesn't print in my case. help thanks.

CodePudding user response:

Because in this line:

for (integer = numberOne; integer > 0; integer = integer / 10){

the loop continues only if integer>0. Therefore you never see "Zero".

CodePudding user response:

Why doesn't it print zero? Look at your loop

for (integer = numberOne; integer > 0; integer = integer / 10){

If integer equals zero then the loop is never entered. You need to be a bit smarter in your loop. How about counting digits?

int num_digits = 0;
while (digits != 0) {
    numberOne = (numberOne * 10)   (digits % 10);
    digits /= 10;
      num_digits;
}

Now that you have the number of digits, you can use that in your second loop

for (integer = numberOne; num_digits > 0; integer = integer / 10, --num_digits) {
    ...
}

You still need to treat zero as a special case, because if the user enters 0 then num_digits will equal zero and you again won't print anything. I'll leave you to figure out how to fix that.

CodePudding user response:

#include <iostream>
using namespace std;

int main(){
    int digits; 
    int arrayLength = 10;  
    
    cout<<"Enter the number: ";
    cin>>digits;
    
    string reverse_number[arrayLength]; //array to store the string of numbers like "Zero"
    int array_counter=0;
    while (digits != 0) {
        switch (digits  % 10) {
            case 0:
                reverse_number[array_counter  ] = "Zero";
                break;
            case 1:
                reverse_number[array_counter  ] = "One";
                break;
            case 2:
                reverse_number[array_counter  ] = "Two";
                break;
            case 3:
                reverse_number[array_counter  ] = "Three";
                break;
            case 4:
                reverse_number[array_counter  ] = "Four";
                break;
            case 5:
                reverse_number[array_counter  ] = "Five";
                break;
            case 6:
                reverse_number[array_counter  ] = "Six";
                break;
            case 7:
                reverse_number[array_counter  ] = "Seven";
                break;
            case 8:
                reverse_number[array_counter  ] = "Eight";
                break;
            case 9:
                reverse_number[array_counter  ] = "Nine";
                break;
        }
        digits /=10;
    }
        int num_digits = array_counter;
        for (int i = 0; i < num_digits; i  ) {
            cout << reverse_number[--array_counter] <<" ";
        }
        return 0;
    }

The reason why your zeros are not showing while doing for 900 is because when you are reversing 900 to 009, there is no condition mentioned to handle the leading zeros in 009 which are just ignored. For this the best approach would be to store them somewhere else like in a string or an array. You can also just store the number words directly to an array while you are converting, that way you wont lose the leading zeros.

  •  Tags:  
  • c
  • Related