Convert words to its corresponding number

I have to convert spelled out numbers (i.e.: seven //input 7 //output)
I had a previous question of converting the number to a word and came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include "std_lib_facilities.h"
  int main () {
  char digit;
  vector<string>word = {"zero", ... "nine"}; //everything is typed out here 
  cout<< "Enter a number 0-9: ";
  while(cin >> digit)
  {
    digit-='0';
    if(digit<word.size()) {
       cout << word[digit] << " ";
     }
   }
   return 0;
  }


How can I change this so I input the numbers spelled out and get an output of the number itself?
Last edited on
You could use a map.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
   string word;
   map<string,int> digit = { {"zero", 0}, {"one",1}, {"two",2}, {"three",3}, {"four",4}, {"five",5}, {"six",6}, {"seven",7}, {"eight",8}, {"nine",9} };

   cout << "Enter a number (zero-nine):\n";
   while( cin >> word )
   {
     if ( digit.count( word ) ) cout << digit[word] << '\n';
   }
}


Enter a number (zero-nine):
three
3
five
5

Last edited on
std::distance allows you to continue using the std::vector<std::string> already constructed. Also suggest std::tolower the input string throughout so that valid numbers are not rejected because of upper-case entries:
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
#include <iostream>
#include <vector>
#include <string>
#include <iterator>//std::distance
#include <algorithm>//std::find
#include <cctype>//std::tolower

const static std::vector <std::string> word {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main ()
{
    std::cout << "spell out your number \n";
    std::string number{};
    getline(std::cin, number);
    for (auto& elem : number)elem = std::tolower(elem);//lowercase string throughout
    auto itr = std::find(word.cbegin(), word.cend(), number);
    if (itr != word.cend())
    {
        std::cout << "you've entered the number: " << std::distance(word.cbegin(), itr) << "\n";
    }
    else
    {
        std::cout << "number not found \n";
    }
}
Thank you very much for the help!

Is there a way that I am able to make the program more simple? I like both methods, and its interesting to see the different way people do things. I don't want to stray away from using a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   string numstring;
   vector<string> word = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

   cout << "Enter a number as a string (zero - nine):" << endl;
   while( cin >> numstring )
   {
      for ( int i = 0; i < word.size(); i++ )
      {
         if ( word[i] == numstring ) 
         {
            cout << i << endl;
            break;
         }
      }
   }
}


I would do as @GunnerFunner suggests and make sure that any entered string is filtered to lower case, otherwise it won't match your vector values. That is over to you.

A more advanced problem for you would be to write a program to do the name/digit conversions up to 100 (or higher) without having a huge vector. Then you get to realise how inconsistent the English number naming system is (although not as bad as French).
Last edited on
Thank you!!:)

Much appreciated for the help!

Not that advanced yet, but I hope to be one day.
Topic archived. No new replies allowed.