command line calculator

Hey everyone, sorry for the multiple post so close to each other, but I can't seem to find the problem with this one. It's a simple add and subtract calculator. I know my code works because I've changed the operation to '+' and '-' and with that single character it works and all, but the issue is when I try figuring out a way to let the user type in "add" or "sub" rather than just typing the symbol + or -.

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
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: " << argv[0] << " [operation] [integer 1] [integer 2]\n";
		cout << "Operation may be " << "\"" << "add" << "\""<< " or " << "\"" << "sub" << "\"\n";
		return 0;
	}
	char operation = argv[1][0];
	int num1 = atoi(argv[2]);
	int num2 = atoi(argv[3]);
	int result;

	if (operation = "add")
	{
		result = num1 + num2;
	}
	else if (operation = "sub")
	{
		result = num1 - num2;
	}
}


When I put the operation "add" or "sub" with quotes it gives me the message:
error: invalid conversion from 'const char*' to 'char'
.
If I change it from double quotes to single quotes so it's 'add' or 'sub' it gives me the message:
warning: multi-character character constant
warning: overflow in implicit constant conversion

I'm so close, but yet so far >:(
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
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: " << argv[0] << " [operation] [integer 1] [integer 2]\n";
		cout << "Operation may be " << "\"" << "add" << "\""<< " or " << "\"" << "sub" << "\"\n";
		return 0;
	}
	const string operation = argv[1];
	int num1 = atoi(argv[2]);
	int num2 = atoi(argv[3]);
	int result;

	if (operation == "add") // NOTE! '=' -> assigns; '==' -> compares
	{
		result = num1 + num2;
	}
	else if (operation == "sub") // NOTE! '=' -> assigns; '==' -> compares
	{
		result = num1 - num2;
	}
}
Not tested but should work
Thanks that works perfectly, I hate how it always ends up being one of those little things that I'm just over looking haha.
Topic archived. No new replies allowed.