help with word input

i currently use visual studio 2019 compiler and i want to be able to input a word as a answer to std::cin. for example a calculator that you can say "division" into to input field which the code will then see and use the proper math function.
This tutorial will explain how to read user input from the command line:

http://www.cplusplus.com/doc/tutorial/basic_io/

And there's nothing Windows-specific about this question. You'd be better off moving it to the Beginners forum.
You mean something like this for math functions?

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
60
#include <iostream>
#include <algorithm>
#include <string>

double getNum(const std::string& promp) {
	double no {};

	while ((std::cout << promp) && !(std::cin >> no)) {
		std::cout << "Invalid number\n";
		std::cin.clear();
		std::cin.ignore(1000, '\n');
	}

	return no;
}

int main()
{
	enum class Opers { Quit = 0, Add, Subtract, Multiply, Divide };

	struct s_ops {
		std::string name;
		Opers op;
	};

	const s_ops Ops[] {{"quit", Opers::Quit}, { "add", Opers::Add }, {"subtract", Opers::Subtract}, {"multiply", Opers::Multiply}, {"divide", Opers::Divide}};

	double next {}, sum {getNum("Enter first number: ")};
	std::string oper;
	auto fnd {std::begin(Ops)};

	do {
		do {
			std::cout << "Enter operator as a word (";
			for (const auto& o : Ops)
				std::cout << o.name << ' ';

			std::cout << "): ";
			std::cin >> oper;

			fnd = std::find_if(std::begin(Ops), std::end(Ops), [&oper](const auto& i) {return i.name == oper; });
			if (fnd != std::end(Ops)) break;
			else
				std::cout << "Invalid operator name entered\n";

		} while (true);

		if (fnd->op != Opers::Quit) {
			next = getNum("Enter next number: ");

			switch (fnd->op) {
				case Opers::Add: sum += next; break;
				case Opers::Subtract: sum -= next; break;
				case Opers::Multiply: sum *= next; break;
				case Opers::Divide: sum /= next; break;
			}
			std::cout << "Result is " << sum << '\n';
		} else break;
	} while (true);
}

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
#include <iostream>
#include <string>
#include <cctype>
#include <set>
using namespace std;

string tolower( string s ){ for ( char &c : s ) c = tolower( c );   return s; }

int main()
{
   const set<string> ops{ "add", "subtract", "multiply", "divide" };
   double a, b, result;
   string op;
   cout << "Enter calculation as A op B, where op is one of add, subtract, multiply, divide: ";
   cin >> a >> op >> b;
   if ( ops.find( tolower( op ) ) == ops.end() )
   {
      cout << "Invalid operation\n";
   }
   else
   {
      switch( tolower( op[0] ) )
      {
         case 'a':   result = a + b;   break;
         case 's':   result = a - b;   break;
         case 'm':   result = a * b;   break;
         case 'd':   result = a / b;   break;
      }
      cout << result << '\n';
   }
}


Enter calculation as A op B, where op is one of add, subtract, multiply, divide: 56.9 divide 3.2
17.7812
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 <string>
#include <cctype>
#include <map>
using namespace std;

string tolower( string s ){ for ( char &c : s ) c = tolower( c );   return s; }

int main()
{
   #define FN []( double a, double b )
   map<string,double (*)( double, double )> ops{
      { "add"     , FN{ return a + b; } },
      { "subtract", FN{ return a - b; } },
      { "multiply", FN{ return a * b; } },
      { "divide"  , FN{ return a / b; } } };

   double a, b;
   string op;
   cout << "Enter calculation as A op B, where op is one of add, subtract, multiply, divide: ";
   cin >> a >> op >> b;
   op = tolower( op );
   if ( ops.count( op ) ) cout << ops[op]( a, b ) << '\n';
   else                   cout << "Invalid operation\n";
}

Enter calculation as A op B, where op is one of add, subtract, multiply, divide: 12 divide 7
1.71429
Last edited on
Topic archived. No new replies allowed.