I am trying to print out 1-999 in English format using words.
Example:
Input: 563
Output: Five Hundred Sixty Three
I made a function for 1 through 9, 10 through 19, and the numbers ending in 0 up until 99 (20, 30, 40, 50 ... 90), and I have a separate function for 100.
I have multiple functions, and then one main()
function. In the last function, I have called all the previous functions. Also, I don't have a returning value for my functions.
When run, there is an error:
signal: segmentation fault (core dumped)
Can someone please help me fix this? And explain how to do this?
This is my code:
#include <iostream>
using namespace std;
string digit_name(int digit) {
// defines 1 through 9
if (digit == 1){
return "One ";
}
if (digit == 2){
return "Two ";
}
if (digit == 3){
return "Three ";
}
if (digit == 4){
return "Four ";
}
if (digit == 5){
return "Five ";
}
if (digit == 6){
return "Six ";
}
if (digit == 7){
return "Seven ";
}
if (digit == 8){
return "Eight ";
}
if (digit == 9){
return "Nine";
}
}
string teen_name(int number){
// defines 10 through 19
if (number == 10){
return "Ten ";
}
if (number == 11){
return "Eleven ";
}
if (number == 12){
return "Twelve ";
}
if (number == 13){
return "Thirteen ";
}
if (number == 14){
return "Fourteen";
}
if (number == 15){
return "Fifteen";
}
if (number == 16){
return "Sixteen";
}
if (number == 17){
return "Seventeen";
}
if (number == 18){
return "Eighteen";
}
if (number == 19){
return "Nineteen";
}
}
string teens_name (int number){
// defines 20 through 90 (20, 30, 40 ... 90)
if (number == 20){
return "Twenty ";
}
if (number == 30){
return "Thirty ";
}
if (number == 40){
return "Forty ";
}
if (number == 50){
return "Fifty ";
}
if (number == 60){
return "Sixty ";
}
if (number == 70){
return "Seventy ";
}
if (number == 80){
return "Eighty ";
}
if (number == 90){
return "Ninty ";
}
}
string hun_name (int number){
// defines 100
return "Hundred ";
}
string define (int number){
// defines what it will output based on the input of the user
// if 1 through 9, output the name (ex: One)
if (number > 0 && number < 9){
cout << digit_name(number);
}
// if 10 through 19, output name (ex: Seventeen)
if (number >> 9 && number << 20){
cout << teen_name(number);
}
// if number divides by 100, then it will print digit_name, then Hundred, and then the teens_name
if (number/100){
cout << digit_name(number) << "Hundered " << teens_name(number) << digit_name(number);
}
// if number divides by 10 evenly, then it is one of the teens (ex: Sixty)
if (number/10 == 0){
cout << teens_name(number);
}
// else, it is a number from 21-99, so it will print teens_name and then digit_name
else{
cout << teens_name(number) << digit_name(number);
}
}
// main function - approved by Ms. B
int main() {
int num;
cout << "Please enter a number from 1 - 999: ";
cin >> num;
define (num);
}
CodePudding user response:
Your are not validating whether the user actually enters a number
between 1..999. So any number outside of that range will cause your logic to fail.
Your define()
function is declared as returning a string
(BTW, you are missing #include <string>
), however it does not actually return
anything. In fact, most of your functions have this same problem, in that they are declared as returning a string
but not all of their code paths actually lead to a return
statement. That is undefined behavior.
Also, define()
doesn't correctly handle the cases where number >= 9
. 9 is skipped entirely, and you are not dividing up multi-digit numbers at all, you are just passing the whole number
as-is to every function, which then leads your functions to paths that are missing return
statements, causing undefined behavior all over the place.
Also, define()
is using bit-shift operators >>
and <<
in its 2nd if
statement, instead of using comparison operators >
and <
. And, it is checking for "divides by 10 evenly" incorrectly in its 4th if
statement, it would need to use the %
modulus operator rather than the /
division operator. However, there is a better way to write your if
statements without using any division in them.
With that said, try something more like this:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// defines 1 through 9
string digit_name(int number){
static const char* words[] = {
"One ",
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine "
};
return words[number-1];
}
// defines 11 through 19
string teen_name(int number){
static const char* words[] = {
"Eleven ",
"Twelve ",
"Thirteen ",
"Fourteen ",
"Fifteen ",
"Sixteen ",
"Seventeen ",
"Eighteen ",
"Nineteen "
};
return words[number-11];
}
// defines 10 through 90 (20, 30, 40 ... 90)
string tens_name (int number){
static const char* words[] = {
"Ten ",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninty "
};
return words[(number/10)-1];
}
// defines 100 through 900 (200, 300, 400 ... 900)
string hun_name (int number){
return digit_name(number / 100) "Hundred ";
}
string define (int number){
// defines what it will output based on the input of the user
string result;
// if number divides by 100, then it is one of the hundreds (ex: Two Hundred)
if (number >= 100){
result = hun_name(number);
number %= 100;
}
// if number divides by 10...
if (number >= 10){
// if number is 11 through 19, output name (ex: Seventeen)
if (number > 10 && number < 20){
result = teen_name(number);
number = 0;
}
else{
// number is one of the tens (ex: Sixty)
result = tens_name(number);
number %= 10;
}
}
// if number is 1 through 9, output the name (ex: One)
if (number > 0){
result = digit_name(number);
}
return result;
}
// main function - approved by Ms. B
int main() {
int num;
do{
cout << "Please enter a number from 1 - 999: ";
if (cin >> num){
if ((num >= 1) && (num <= 999))
break;
}
else{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid input, try again!\n";
}
while (true);
cout << define(num);
}
Alternatively, you could have each function calculate its own portion of the original number
and output the appropriate word(s), if any. That way, define()
can just call all of the functions unconditionally and only the relevant words will be returned, eg:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// defines 1 through 9
string digit_name(int number){
static const char* words[] = {
"One ",
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine "
};
// if number has 1 through 9, output the name (ex: One)
number %= 10;
if (number){
return words[number-1];
}
return "";
}
// defines 11 through 19
string teen_name(int number){
static const char* words[] = {
"Eleven ",
"Twelve ",
"Thirteen ",
"Fourteen ",
"Fifteen ",
"Sixteen ",
"Seventeen ",
"Eighteen ",
"Nineteen "
};
// if number has 11 through 19, output name (ex: Seventeen)
number %= 100;
if (number > 10 && number < 20){
return words[number-11];
}
return "":
}
// defines 10 through 90 (20, 30, 40 ... 90)
string tens_name (int number){
static const char* words[] = {
"Ten ",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninty "
};
// if number divides by 10 and is not a teen, then it is one of the tens (ex: Sixty)
number %= 100;
if (number && (number < 11 || number > 19)){
return words[(number/10)-1];
}
return "";
}
// defines 100 through 900 (200, 300, 400 ... 900)
string hun_name (int number){
// if number divides by 100, then it is one of the hundreds (ex: Two Hundred)
if (number >= 100) {
return digit_name(number / 100) "Hundred ";
}
return "";
}
string define (int number){
// defines what it will output based on the input of the user
return hun_name(number) tens_name(number) teen_name(number) digit_name(number);
}
// main function - approved by Ms. B
int main() {
int num;
do{
cout << "Please enter a number from 1 - 999: ";
if (cin >> num){
if ((num >= 1) && (num <= 999))
break;
}
else{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid input, try again!\n";
}
while (true);
cout << define(num);
}
CodePudding user response:
I changed my code. This is what I got now:
#include <iostream>
#include <string>
using namespace std;
string digit_name(int digit) {
// defines 1 through 9
if (digit == 1){
return "One ";
}
if (digit == 2){
return "Two ";
}
if (digit == 3){
return "Three ";
}
if (digit == 4){
return "Four ";
}
if (digit == 5){
return "Five ";
}
if (digit == 6){
return "Six ";
}
if (digit == 7){
return "Seven ";
}
if (digit == 8){
return "Eight ";
}
if (digit == 9){
return "Nine";
}
return "";
}
string teen_name(int number){
// defines 10 through 19
if (number == 10){
return "Ten ";
}
if (number == 11){
return "Eleven ";
}
if (number == 12){
return "Twelve ";
}
if (number == 13){
return "Thirteen ";
}
if (number == 14){
return "Fourteen";
}
if (number == 15){
return "Fifteen";
}
if (number == 16){
return "Sixteen";
}
if (number == 17){
return "Seventeen";
}
if (number == 18){
return "Eighteen";
}
if (number == 19){
return "Nineteen";
}
return "";
}
string tens_name (int number){
// defines 20 through 90 (20, 30, 40 ... 90)
if (number >= 90){
return "Ninety ";
}
if (number >= 80){
return "Eighty ";
}
if (number >= 70){
return "Seventy ";
}
if (number >= 60){
return "Sixty ";
}
if (number >= 50){
return "Fifty ";
}
if (number >= 40){
return "Forty ";
}
if (number >= 30){
return "Thirty ";
}
if (number >= 20){
return "Twenty ";
}
return "";
}
// defines 100 through 900 (200, 300, 400 ... 900)
string hun_name (int number){
return digit_name(number / 100) "Hundred ";
}
string define (int number){
// defines what it will output based on the input of the user
string result;
// if number divides by 100, then it is one of the hundreds (ex: Two Hundred)
if (number >= 100){
result = hun_name(number);
number %= 100;
}
// if number divides by 10...
if (number >= 10){
// if number is 11 through 19, output name (ex: Seventeen)
if (number > 10 && number < 20){
result = teen_name(number);
number = 0;
}
else{
// number is one of the tens (ex: Sixty)
result = tens_name(number);
number %= 10;
}
}
// if number is 1 through 9, output the name (ex: One)
if (number > 0){
result = digit_name(number);
}
return result;
}
// main function - approved by Ms. B
int main() {
int num;
cout << "Please enter a number: ";
cin >> num;
cout << define (num) << endl;
return 0;
}