May 22, 2016 at 10:18pm UTC
I have to write a program that takes a number from 0 to 4 in spelled out format and converts it into the numerical form. And I have to print out "Not a number I know" if the user enters garbage input.
For the number to word conversion code, would it be okay if I just hard-coded it since it's small numbers, or is there an algorithm for automating it? And as for the garbage input detection code, would it be good to use a stringstream to check if the input is a number or if it's all words?
Edit: Right now, this is what I've got:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// chap3_ex9.cpp : Defines the entry point for the console application.
// Osman Zakir
// 5 / 23 / 2016
// Programming: Principles and Practice using C++ 2nd Edition, Chapter 3 Exercise 9
// This program takes spelled-out numbers as input from the user and changes them into
// numerical format numbers. "four" woul become '4'
#include "std_lib_facilities.h"
string get_input();
void convert_to_number(const string& number_alpha);
int main()
{
string spelled_number = get_input();
convert_to_number(spelled_number);
keep_window_open();
}
string get_input()
{
cout << "Enter a spelled out number: " ;
string number_alpha; // "alpha" indicates "alphabetical format"
cin >> number_alpha;
cin.ignore(32767, '\n' );
return number_alpha;
}
void convert_to_number(const string& number_alpha)
{
vector<string> spelled_numbers{ "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" ,
"eight" , "nine" };
if (number_alpha == "zero" )
{
cout << "0\n" ;
}
else if (number_alpha == "one" )
{
cout << "1\n" ;
}
else if (number_alpha == "two" )
{
cout << "2\n" ;
}
else if (number_alpha == "three" )
{
cout << "3\n" ;
}
else if (number_alpha == "four" )
{
cout << "4\n" ;
}
vector<string>::iterator it;
it = find(spelled_numbers.begin(), spelled_numbers.end(), number_alpha);
if (it == spelled_numbers.end())
{
cout << "not a number I know\n" ;
}
}
Help me out here (if I could've done this better), please and thank you.
Last edited on May 22, 2016 at 10:56pm UTC
May 23, 2016 at 4:22am UTC
Hard-coding a lookup table is about the only way to do it.
A couple of observations:
You have a nice lookup table there on line 31 that you completely ignore on lines 33-52.
Once you have the index into the table of the number, you can cout it.
What if the user enters "Two" or "THREE"?
Line 25: #include <limits>
and cin.ignore(numeric_limits<streamsize>::max(), '\n' );
Yes, I know it is more verbose. Sorry.
Hope this helps.
May 23, 2016 at 12:07pm UTC
Yeah, I guess I should've indexed the vector in lines 33 through 52. I'll fix that now. As for "Two" or "THREE", I'm pretty sure those will be rejected because of the vector<string>::iterator it on find(). If the word the user types in isn't in the vector, it'll be rejected.
Should I make it accept the uppercase versions and capitalized versions, too? If so, what would be a good way to do it?
Anyway, this is what I have right now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// chap3_ex9.cpp : Defines the entry point for the console application.
// Osman Zakir
// 5 / 23 / 2016
// Programming: Principles and Practice using C++ 2nd Edition, Chapter 3 Exercise 9
// This program takes spelled-out numbers as input from the user and changes them into
// numerical format numbers. "four" would become '4'
#include "std_lib_facilities.h"
string get_input();
void convert_to_number(const string& number_alpha);
int main()
{
string spelled_number = get_input();
convert_to_number(spelled_number);
keep_window_open();
}
string get_input()
{
cout << "Enter a spelled out number (from \"zero\" to \"four\"): " ;
string number_alpha; // "alpha" indicates "alphabetical format"
cin >> number_alpha;
cin.ignore(32767, '\n' );
return number_alpha;
}
void convert_to_number(const string& number_alpha)
{
vector<string> spelled_numbers{ "zero" , "one" , "two" , "three" , "four" };
if (number_alpha == spelled_numbers[0])
{
cout << spelled_numbers[0] << "\n" ;
}
else if (number_alpha == spelled_numbers[1])
{
cout << spelled_numbers[1] << "\n" ;
}
else if (number_alpha == spelled_numbers[2])
{
cout << spelled_numbers[2] << "\n" ;
}
else if (number_alpha == spelled_numbers[3])
{
cout << spelled_numbers[3] << "\n" ;
}
else if (number_alpha == spelled_numbers[4])
{
cout << spelled_numbers[4] << "\n" ;
}
vector<string>::iterator it;
it = find(spelled_numbers.begin(), spelled_numbers.end(), number_alpha);
if (it == spelled_numbers.end())
{
cout << "not a number I know\n" ;
}
}
Last edited on May 23, 2016 at 12:24pm UTC
May 23, 2016 at 1:07pm UTC
Um, you're going to have to think about that a bit more. (Especially as your program now does not do what the original correctly did.)
Hint: what is the relationship between the index into your array (spelled_numbers[] ) and the words in the array?
May 23, 2016 at 2:08pm UTC
Yeah, it doesn't change it to the numerical form, does it? I'll try to fix it.
Edit: Alright, got it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// chap3_ex9.cpp : Defines the entry point for the console application.
// Osman Zakir
// 5 / 23 / 2016
// Programming: Principles and Practice using C++ 2nd Edition, Chapter 3 Exercise 9
// This program takes spelled-out numbers as input from the user and changes them into
// numerical format numbers. "four" would become '4'
#include "std_lib_facilities.h"
string get_input();
void convert_to_number(const string& number_alpha);
int main()
{
string spelled_number = get_input();
convert_to_number(spelled_number);
keep_window_open();
}
string get_input()
{
cout << "Enter a spelled out number (from \"zero\" to \"four\"): " ;
string number_alpha; // "alpha" indicates "alphabetical format"
cin >> number_alpha;
cin.ignore(32767, '\n' );
return number_alpha;
}
void convert_to_number(const string& number_alpha)
{
vector<string> spelled_numbers{ "zero" , "one" , "two" , "three" , "four" };
if (number_alpha == spelled_numbers[0])
{
cout << "0\n" ;
}
else if (number_alpha == spelled_numbers[1])
{
cout << "1\n" ;
}
else if (number_alpha == spelled_numbers[2])
{
cout << "2\n" ;
}
else if (number_alpha == spelled_numbers[3])
{
cout << "3\n" ;
}
else if (number_alpha == spelled_numbers[4])
{
cout << "4\n" ;
}
vector<string>::iterator it;
it = find(spelled_numbers.begin(), spelled_numbers.end(), number_alpha);
if (it == spelled_numbers.end())
{
cout << "not a number I know\n" ;
}
}
Now should I also include the ones like "Three" or "THREE"? I'm thinking I should do that, but I'd have to put them into the vector, right?
Last edited on May 23, 2016 at 2:13pm UTC
May 24, 2016 at 9:54pm UTC
I did it like how Bingocat4 suggested (for the "Three" or "THREE" case). And I already had everything else working correctly by now so it's fine. This one is solved. I'll get back to you when I need to do it for bigger numbers. Thanks for now, guys.