I got it figured out, not sure how to delete this thread so I'll leave with this
I got it figured out, not sure how to delete this thread so I'll leave with this
Last edited by treebeard; 07-10-2015 at 12:43 PM.
lel... You did? Oh well, I was ready to provide some tips too.
Devoted my life to programming...
Well I got past my blockade. I have to write a program that converts integers as input to the english word version of it. Example:
Enter number between -999,999 and 999,999: 1234
One Thousand Two Hundred Thirty Four
I'm still working on it, but I'll definitely DM you if I need help. Thanks!
Note that it's considered poor etiquette to edit out an original post after the problem has been solved.
It's not a big deal this time, since you did it before there were any responses. But once people start replying, editing/deleting the original post breaks thread continuity, and cheapens the contribution made by whomever volunteered their time to help.
Faux pas aside, I appreciate your attempt at a cover up. Welcome to the forum.
And I appreciate the fact that the OP is spending the summer learning the language on their own to get prepared for a class next semester.
Such initiative is usually awarded with success.
I figured I'd post my code on here to have fresh eyes look at it and point out inefficiencies/critique the code. Any and all feedback is appreciated.
DISCLAIMER: My knowledge of C++ is limited pretty much to what I've read through the book so far. I don't know vectors or arrays, stuff like that that I'm sure I could've used to make this program much more efficient. (I took a class in python so I know what those terms mean, generally speaking.)
DISCLAIMER v2.0: This may be painful to look at. You may be plagued by facepalms and sighs whilst reading my code. I am new to C++ and also my brain is fried from waking up at 4am for my summer job. Excuses aside, please let me have it. The only way I can get better is if people are honest about what does/doesn't work well and what I should/shouldn't do.
lol forgot to mention what the purpose was.
1. Take in an integer between negative and positive 999,999.
2. Output the english word version of the number.
example:
Enter a number between -999,999 and 999,999: 1234
One Thousand Two Hundred Thirty Four
Thanks.
Code:#include <iostream> #include <string> #include <math.h> using namespace std; int number; unsigned GetNumberOfDigits(int i); int lessThanTwenty (int number); int nameDigit (int number); int twentyToHundred (int number); int main () { cout << "Enter a number: "; cin >> number; int length = GetNumberOfDigits(number); if (number < 0) { cout << "Negative "; number = number * (-1); } if (number == 0) { cout << "Zero..." << endl; } else if (number > 0 && number < 10) // 1 - 10 { nameDigit(number); } else if (number > 10 && number <= 20) // 10 - 20 { lessThanTwenty(number); } else if (number > 20 && number < 100) // 20 -100 { twentyToHundred(number); } else if (number >= 100 && number < 1000) // 100 - 1000 { int first_digit = number / 100; int second_digit = (number - (first_digit * 100)) / 10; int third_digit = (number - (first_digit * 100)) - (second_digit * 10); nameDigit(first_digit); cout << " Hundred "; if (second_digit == 1) { lessThanTwenty(number - (first_digit * 100)); } else if (second_digit > 1 && second_digit <= 9) { twentyToHundred(number - (first_digit * 100)); } else { nameDigit(third_digit); } } else if (number >= 1000 && number < 11000) // 1000 - 11000 { if(number < 10000) { int first_digit = number / (1000); int second_digit = (number - (first_digit * 1000)) / 100; int third_digit = ((number - (first_digit * 1000)) - (second_digit * 100)) / 10; int fourth_digit = (number - (first_digit * 1000)) - (second_digit * 100) - (third_digit * 10); nameDigit(first_digit); cout << " Thousand "; if (second_digit != 0) { nameDigit(second_digit); cout << " Hundred "; if (third_digit == 1) { lessThanTwenty((number - (first_digit * 1000)) - (second_digit * 100)); } else if (third_digit > 1 && third_digit <= 9) { twentyToHundred((number - (first_digit * 1000)) - (second_digit * 100)); } else { nameDigit(fourth_digit); } } else { if (third_digit == 1) { lessThanTwenty((number - (first_digit * 1000)) - (second_digit * 100)); } else if (third_digit > 1 && third_digit <= 9) { twentyToHundred((number - (first_digit * 1000)) - (second_digit * 100)); } else { nameDigit(fourth_digit); } } } else { cout << "Ten Thousand "; int hundreds = (number - 10000) / 100; int tens = ((number - 10000) - (hundreds * 100)) / 10; int ones = (number - 10000) - (hundreds * 100) - (tens * 10); nameDigit(hundreds); cout << " Hundred "; if (tens == 1) { lessThanTwenty((number - 10000) - (hundreds * 100)); } else if (tens > 1 && tens <= 9) { twentyToHundred((number - 10000) - (hundreds * 100)); } else { nameDigit(ones); } } } else if (number >= 11000 && number < 100000) // 11000 - 100000 { int thousands = number / 1000; if (thousands < 20) { lessThanTwenty(thousands); cout << " Thousand "; } else if (thousands >= 20) { twentyToHundred(thousands); cout << " Thousand "; } int hundreds = (number - (thousands * 1000)) / 100; int tens = (number - (thousands * 1000) - (hundreds * 100)) / 10; int ones = (number - (thousands * 1000) - (hundreds * 100) - (tens * 10)); if (hundreds != 0) { nameDigit(hundreds); cout << " Hundred "; if (tens == 1) { lessThanTwenty(number - (thousands * 1000) - (hundreds * 100)); } else if (tens > 1 && tens <= 9) { twentyToHundred(number - (thousands * 1000) - (hundreds * 100)); } else { nameDigit(ones); } } else { if (tens == 1) { lessThanTwenty(number - (thousands * 1000) - (hundreds * 100)); } else if (tens > 1 && tens <= 9) { twentyToHundred(number - (thousands * 1000) - (hundreds * 100)); } else { nameDigit(ones); } } } else if (number >= 100000 && number < 1000000) // 100000 - Million { int first_digit = number / 100000; int second_digit = (number - (100000 * first_digit)) / 10000; int third_digit = (number - (100000 * first_digit) - (10000 * second_digit)) / 1000; int hundreds = (number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit)) / 100; int tens = (number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds)) / 10; int ones = (number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds) - (10 * tens)); nameDigit(first_digit); cout << " Hundred "; if (second_digit == 0) { nameDigit(third_digit); cout << " Thousand "; } else if (second_digit == 1) { lessThanTwenty((number - (100000 * first_digit)) / 1000); cout << " Thousand "; } else { twentyToHundred((number - (100000 * first_digit)) / 1000); cout << " Thousand "; } if (hundreds != 0) { nameDigit(hundreds); cout << " Hundred "; if (tens == 1) { lessThanTwenty(number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds)); } else if (tens > 1 && tens <= 9) { twentyToHundred(number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds)); } else { nameDigit(ones); } } else { if (tens == 1) { lessThanTwenty(number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds)); } else if (tens > 1 && tens <= 9) { twentyToHundred(number - (100000 * first_digit) - (10000 * second_digit) - (1000 * third_digit) - (100 * hundreds)); } else { nameDigit(ones); } } } } unsigned GetNumberOfDigits(int i) { return i > 0 ? (int) log10 ((double) i) + 1 : 1; } int nameDigit (int number) { switch (number) { 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 10: cout << "Ten"; break; } } int lessThanTwenty (int number) { switch (number) { case 11: cout << "Eleven"; break; case 12: cout << "Twelve"; break; case 13: cout << "Thirteen"; break; case 14: cout << "Fourteen"; break; case 15: cout << "Fifteen"; break; case 16: cout << "Sixteen"; break; case 17: cout << "Seventeen"; break; case 18: cout << "Eighteen"; break; case 19: cout << "Nineteen"; break; } } int twentyToHundred (int number) { int first_digit = number / 10; int second_digit = number % 10; switch (first_digit) { 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 << "Ninety "; break; } nameDigit (second_digit); }
Last edited by treebeard; 07-12-2015 at 05:45 PM.
- use <cmath> instead of math.h
- declare your variable "number" inside main
- your code handles numbers beyond 9999 EDIT: sorry I misread, you're going to millions
- you get the number length but don't use it
- your LessThanTwenty logic wrongly includes 20
- your logic contains redundancies, why does twentyToHundred check if number > 20, when the previous if statements guarantee this?
- your namedigit function should skip the whole switch block if the input is zero, more efficient
- you should have a naming function for hundreds too: it just calls the other functions
example sketch:
Code:if (number < -9999 && number > 9999) {cout<<"\nNumber exceeds bounds\n";} if (number < 0) {number *= -1; cout<<"minus ";} if (number == 0) {cout<<"zero";} else if (number < 11) //1-10, namedigit() else if (number < 20) //11-19, nameteen() else if (number < 100) //20-99, namedeca() else if (number < 1000) //100-999, namecent() //where namecent is just: namecent(int number) { int first_digit = number / 100; namedigit(first_digit); cout<<" hundred"; int rest = number - (first_digit*100) if (rest < 11){namedigit(rest)} else if (rest < 20){nameteen(rest)} else {namedeca(rest)} } else if (number < 10000) //namethousands(), similar to namecent
Last edited by jiggunjer; 07-13-2015 at 04:04 AM. Reason: I shouldn't post half asleept...
Hi,
I've just had a go at this problem as well and would appreciate any feedback on my code. Here is the original question along with some tips provided by the author of the book:
And here is my solution...1. Implement the source code that turns numbers into English text for numbers between -999,999 and 999,999. (Hint: You might also be able to take advantage of the fact that the integer data type will truncate decimal points. Also, remember that your algorithm doesn't have to work for all numbers - only numbers with six digits or less.)
...
1) Break the number up into chunks of three digits
2) For each three-digit chunks, compute the text; append the magnitude of that chunk; append the chunks together
3) To compute the text of a three-digit chunk, compute the number of hundreds, and convert that one-digit number to text, and add "hundreds", appending the text of the two-digit chunk
4) To compute the text of a two-digit chunk, if it's less than 20, look it up; if it's greater than 20, compute the number of tens, and look up the word, and append the text of the one-digit number
5) To deal with the numbers from 1 to 19, well, we'll just have to hard-code those directly into the program - there's no algorithm to solve that. Not that I can see anyway!
...not all of the details are fully specified, but now you have enough of an outline...
Code:#include <iostream> using namespace std; int getDigits(int n); string computeSixDigitN(int n); string computeFiveDigitN(int n); string computeFourDigitN(int n); string computeThreeDigitN(int n); string computeTwoDigitN(int n); string convertToText(int n); int main() { int number; string text; // Get a number between the given range cout << "Please enter a number between -999999 and 999999: "; cin >> number; // If the number is negative, append - to text and make number positive if(number < 0) { number *= -1; text = "Minus "; } // Get the number of digits within the number int digits = getDigits(number); // Convert the number to English text switch(digits) { case 6: text += computeSixDigitN(number); break; case 5: text += computeFiveDigitN(number); break; case 4: text += computeFourDigitN(number); break; case 3: text += computeThreeDigitN(number); break; case 2: text += computeTwoDigitN(number); break; case 1: text += convertToText(number); break; } // Print the text cout << endl << text << endl; // Pause the program cin.ignore(256, '\n'); cout << endl << "Hit return to end..."; cin.get(); } // Returns the number of digits within a number int getDigits(int n) { int digits; if((n / 100000) != 0) digits = 6; else if((n / 10000) != 0) digits = 5; else if((n / 1000) != 0) digits = 4; else if((n / 100) != 0) digits = 3; else if((n / 10) != 0) digits = 2; else digits = 1; return digits; } string computeSixDigitN(int n) { string text; // Compute first three digits int firstDigit = (n / 100000); int secondDigit = (n - (firstDigit * 100000)) / 10000; int thirdDigit = (n - (firstDigit * 100000) - (secondDigit * 10000)) / 1000; text = convertToText(firstDigit) + " Hundred "; text += computeTwoDigitN((secondDigit * 10) + thirdDigit) + " Thousand "; // Compute last three digits int lastThreeDigits = (n - (firstDigit * 100000) - (secondDigit * 10000) - (thirdDigit * 1000)); text += computeThreeDigitN(lastThreeDigits); return text; } string computeFiveDigitN(int n) { string text; // Compute first two digits int firstDigit = (n / 10000); int secondDigit = (n - (firstDigit * 10000)) / 1000; text = computeTwoDigitN((firstDigit * 10) + secondDigit) + " Thousand "; // Compute last three digits int lastThreeDigits = (n - (firstDigit * 10000) - (secondDigit * 1000)); text += computeThreeDigitN(lastThreeDigits); return text; } string computeFourDigitN(int n) { string text; // Compute first digit int firstDigit = (n / 1000); text = convertToText(firstDigit) + " Thousand "; // Compute last three digits int lastThreeDigits = (n - (firstDigit * 1000)); text += computeThreeDigitN(lastThreeDigits); return text; } string computeThreeDigitN(int n) { string text; int firstDigit = (n / 100); int lastTwoDigits = (n - (firstDigit * 100)); text = convertToText(firstDigit) + " Hundred "; text += computeTwoDigitN(lastTwoDigits); return text; } string computeTwoDigitN(int n) { string text; if(n < 20) { text = convertToText(n); } else { int firstDigit = n / 10; switch(firstDigit) { case 2: text = "Twenty "; break; case 3: text = "Thirty "; break; case 4: text = "Forty "; break; case 5: text = "Fifty "; break; case 6: text = "Sixty "; break; case 7: text = "Seventy "; break; case 8: text = "Eighty "; break; case 9: text = "Ninety "; break; } // Append the last digit int finalDigit = n - (firstDigit *= 10); if(finalDigit != 0) text += convertToText(finalDigit); } return text; } string convertToText(int n) { string text; switch(n) { case 0: text = "Zero"; break; case 1: text = "One"; break; case 2: text = "Two"; break; case 3: text = "Three"; break; case 4: text = "Four"; break; case 5: text = "Five"; break; case 6: text = "Six"; break; case 7: text = "Seven"; break; case 8: text = "Eight"; break; case 9: text = "Nine"; break; case 10: text = "Ten"; break; case 11: text = "Eleven"; break; case 12: text = "Twelve"; break; case 13: text = "Thirteen"; break; case 14: text = "Fourteen"; break; case 15: text = "Fifteen"; break; case 16: text = "Sixteen"; break; case 17: text = "Seventeen"; break; case 18: text = "Eighteen"; break; case 19: text = "Nineteen"; break; } return text; }
Good design. Only thing I can remark is that it lets the user input a 7+ digit number.
Thanks
Actually, the design is overcomplicated. It you really want to see how bad it is, try extending it to convert any 64-bit int to words.
There's no need for the case statements since arrays would work better.
There's no need to break the number down into 6 digits, 5 digits, etc. Imagine extending this to 19 digits (necessary for 64-bit ints).
The key insight here is to first write a function that will convert any 3 digit number to words (ignoring negative numbers, which would have been converted to positive by that point). Then you just have to write a function that will convert each 3-digit chunk of the number using the previous function and add the correct magnitude (e.g., million) after it.
How would you split the number into 3 digit chunks?
I can think of two ways:
I like the recursive function better.Code:#include <iostream> void chunkify(long long n) { long long x = 1000000000000000000; while (x > 0) { std::cout << (n / x % 1000) << '\n'; x /= 1000; } } void chunkify2(long long n) { if (n >= 1000) chunkify2(n / 1000); std::cout << (n % 1000) << '\n'; } int main() { long long n = 9223372036854775807; // std::numeric_limits<long long>::max() chunkify(n); std::cout << "-----\n"; chunkify2(n); return 0; }