Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Mr. Stephen is working in a translator company. His task is to translate the given cash (positive integer number) into words. He found that maximum cash value cannot be more than 10000000000. Write the C++ code to help Mr. Stephen.


What I have tried:

 #include <iostream>
using namespace std;
 
// strings at index 0 is not used, it is to make array
// indexing simple
string one[] = { "", "one ", "two ", "three ", "four ",
                 "five ", "six ", "seven ", "eight ",
                 "nine ", "ten ", "eleven ", "twelve ",
                 "thirteen ", "fourteen ", "fifteen ",
                 "sixteen ", "seventeen ", "eighteen ",
                 "nineteen " };
 
// strings at index 0 and 1 are not used, they are to
// make array indexing simple
string ten[] = { "", "", "twenty ", "thirty ", "forty ",
                 "fifty ", "sixty ", "seventy ", "eighty ",
                 "ninety " };
 
// n is 1- or 2-digit number
string numToWords(int n, string s)
{
    string str = "";
    // if n is more than 19, divide it
    if (n > 19)
        str += ten[n / 10] + one[n % 10];
    else
        str += one[n];
 
    // if n is non-zero
    if (n)
        str += s;
 
    return str;
}
 
// Function to print a given number in words
string convertToWords(long n)
{
    // stores word representation of given number n
    string out;
 
    // handles digits at ten millions and hundred
    // millions places (if any)
    out += numToWords((n / 10000000), "crore ");
 
    // handles digits at hundred thousands and one
    // millions places (if any)
    out += numToWords(((n / 100000) % 100), "lakh ");
 
    // handles digits at thousands and tens thousands
    // places (if any)
    out += numToWords(((n / 1000) % 100), "thousand ");
 
    // handles digit at hundreds places (if any)
    out += numToWords(((n / 100) % 10), "hundred ");
    out += numToWords(((n/100) %10), "tens");
 
    if (n > 100 && n % 100)
       // out += "and ";
 
    // handles digits at ones and tens places (if any)
    out += numToWords((n % 100), "");
   
    //Handling the n=0 case
    if(out=="")
    out = "zero";
 
    return out;
}
 
// Driver code
int main()
{
    int num,temp,length=0,result,n,m=0;
    cin>>num;
    temp=num;
    
    cout << convertToWords(n) << endl;
    
    return 0;
}
Posted
Updated 23-Sep-22 7:22am
Comments
CPallini 23-Sep-22 3:28am    
And... What is the problem with this code you found on the web?
Rick York 23-Sep-22 4:06am    
Do you really not see the problem? It is obvious to me. Hint - it's in the main function.
CPallini 23-Sep-22 4:37am    
Maybe the 'Driver' code is the OP contribution. :-D
Rick York 23-Sep-22 13:12pm    
Probably. That would explain the lack of a clue about it.

Here is a tip giving full instructions: Converting numbers to the word equivalent. [^] - the code is in C#, but that should be no problem to someone who can write C++ code like that!
 
Share this answer
 
Hint: Look in your main method. Why are you defining all those variables when you only need one?

This leads me to suspect you didn't write the rest of the code. You only wrote the code in main. Copying and pasting code from the web and turning it in as your own work will only get you a failing grade for plagiarism.
 
Share this answer
 
v4
I guess you didn't get the hint. Let's look at main a little closer. I split the variables out so each one has their own line.
C++
int main()
{
    int length=0;    // not used
    int result;      // not used
    int m=0;         // not used
    int n;           // passed to convertToWords but never set to a value
    int temp;        // assigned but not used otherwise
    int num;         // this is used but its value is thrown away

    cin>>num;
    temp=num;
    
    cout << convertToWords(n) << endl;
    
    return 0;
}
The problem is none of these variables were used correctly. If I were you, I would pass num to convertWords instead of n because it is assigned a value from user input. There is no need for any of the other variables. This is what I would do :
C++
int main()
{
    int num;
    cin>>num;
    
    cout << convertToWords( num ) << endl;
    
    return 0;
}
Then you get to debug convertToWords. Debuggers are a very, very useful tool so you should learn how to use one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900