Writing out numbers in spelled out format

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
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.
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
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?
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
Now should I also include the ones like "Three" or "THREE"?

Wouldn't it be easier to take number_alpha and convert it to lower case (before line 26)?
Um...

        if (number_alpha == spelled_numbers[ 0 ])
        {
                cout << "
0 \n";
        }
You can convert to lowercase like this

1
2
3
4
5
#include <algorithm>

string s = "THREE";
transform(s.begin(), s.end(), s.begin(), tolower);
define in your code a map of pair of unit , 10s , 100s , etc.
then for instance

1
2
3
dictionary_unit[2] = "TWO";
dictionary_tens[2] = "TWENTY";
dictionary_hundreds[2] = "TWO" + "HUNDRED";


so if I have 222 , I call a parser function like so
1
2
3
4
5
6
string SpelledNumber = ParseNumber(2,2,2);

string ParseNumber(int unity.int ten,int hundred)
{
   return dictionary_hundreds[hundred] + dictionnary_tens[ten] + dictionary_unit[unit];
}


hope that helped.
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.
Topic archived. No new replies allowed.