Create smart calculator.

Hello,
I need to create smart calculator.
For example, i need to find Sum of 7 and "three", compiler must write: 10.
Or "two" plus "two" are equal to 4;
I have code, but need to fix 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
#include <iostream>
#include <vector>

using namespace std;

vector<string> number_str = {"zero", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int get_number()
{
	string get_string;
	cin >> get_string;

	for (int i = 0; i < number_str.size(); i++)
	{
		if (get_string == number_str[i])
		{
			get_string = i;
		}
	}
}

int main()
{

	double a = 0;
	double b = 0; 
	char operation = ' ';

	cin >> a >> b >> operation;

	a = get_number();
	b = get_number();

	switch(operation) 
	{
	case '+': 
	cout << "Sum of " << a << " and " << b << ": " << a + b;
	break;
	case '-': 
	cout << "Difference between " << a << " and " << b << ": " << a - b;
	break;
	case '*':
	cout << "Multiplication of " << a << " and " << b << ": " << a * b; 
	break;
	case '/': 
	cout << "Division of " << a << " and " << b << ": " << a / b;
	break;
        default: cout << "Error in output stream";  
	}
}
vector<string> number_str = {"zero", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
What happened to "one"?



I should change
get_string = i;
to
return i;



1
2
3
	cin >> a >> b >> operation;
	a = get_number();
	b = get_number();

Make your mind up! You input
a, then
b, then
an operation, then
a (again), then
b (again)




I fixed it, but compiler give me: Erron in output stream
Last edited on
sadibekov wrote:
I fixed it, but compiler give me: Erron in output stream

Mmmm, but we don't have access to your computer, so we can't see what you've done.

Please post your revised code BELOW this post.
Sorry,

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
#include <iostream>
#include <vector>

using namespace std;

vector<string> number_str = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int get_number()
{
	string get_string;
	cin >> get_string;

	for (int i = 0; i < number_str.size(); i++)
	{
		if (get_string == number_str[i])
		{
			return i;
		}
	}
}

int main()
{

	double a = get_number();
	double b = get_number(); 
	char operation = ' ';

	cin >> a >> b >> operation;

	switch(operation) 
	{
		case '+': 
			cout << "Sum of " << a << " and " << b << ": " << a + b;
			break;
		case '-': 
			cout << "Difference between " << a << " and " << b << ": " << a - b;
			break;
		case '*':
			 cout << "Multiplication of " << a << " and " << b << ": " << a * b; 
			break;
		case '/': 
			cout << "Division of " << a << " and " << b << ": " << a / b;
			break;
		default: cout << "Error in output stream";  
	}
}
1
2
3
4
5
	double a = get_number();
	double b = get_number(); 
	char operation = ' ';

	cin >> a >> b >> operation;


But now you've switched to:
input a, then
input b, then
set default operation, then
input a (again), then
input b (again), then
input operation




(Also, it would be useful to have a default return value - and probably a warning - if get_number receives a string not matching any of the legitimate choices.)
Topic archived. No new replies allowed.