hi,

i am basically a newbie to C++ and tried to make this simple math program but came acroos this error and don't know how to resolve it:

#include <iostream>
#include <cmath>
#include <string>


using namespace std;

int main(void)
{
	string input = Add;
	int a, b;
	cout << "What would you like to do? (Add, Subtract, Multiply, Divide) ";
	cin >> input;

	if (Add)
	{
		cout << "Enter a value: ";
		cin >> a;
		cout << "Enter another value: ";
		cin >> b;
		cout << "The sum of the numbers is " << a + b << endl;
	}
	else if  (input = 'Subtract')
	{
		cout << "Enter a value: ";
		cin >> a;
		cout << "Enter another value: ";
		cin >> b;
		cout << "The difference of the two numbers is " << a - b << endl;
	}
	else if  (input = 'Multiply')
	{
		cout << "Enter a value: ";
		cin >> a;
		cout << "Enter another value: ";
		cin >> b;
		cout << "The product of the two numbers is " << a * b << endl;
	}
	else if  (input = 'Divide')
	{
		cout << "Enter a value: ";
		cin >> a;
		cout << "Enter another value: ";
		cin >> b;
		cout << "The quotient of the two numbers is " << a / b << endl;
		cout << "And the remainder is " << a % b << endl;
	}
	
	return 0;
}

the error is saying:

limac@limac-kubuntu:~/Desktop$ g++ -o math math.cpp
math.cpp:15:14: warning: multi-character character constant
math.cpp:23:20: warning: character constant too long for its type
math.cpp:31:20: warning: character constant too long for its type
math.cpp:39:20: warning: character constant too long for its type
math.cpp: In function ‘int main()’:
math.cpp:10: error: ‘Add’ was not declared in this scope
math.cpp:15: warning: overflow in implicit constant conversion
math.cpp:15: error: could not convert ‘input.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](100)’ to ‘bool’
math.cpp:23: warning: overflow in implicit constant conversion
math.cpp:23: error: could not convert ‘input.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](116)’ to ‘bool’
math.cpp:31: warning: overflow in implicit constant conversion
math.cpp:31: error: could not convert ‘input.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](121)’ to ‘bool’
math.cpp:39: warning: overflow in implicit constant conversion
math.cpp:39: error: could not convert ‘input.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](101)’ to ‘bool’
limac@limac-kubuntu:~/Desktop$

if anyone can identify the error and inform me, I'd really appreciate that!

Thx

Add, Subtract, and etc are strings. You wrote 'Add' it's suppose to be "Add". All strings are denoted with " ", chars are denoted with ' '.

There are a few issues. In the line, "string input = Add;", I don't think you need the Add there. All you need to do is initialize the string input.

The line if(Add) needs to be changed to "if(input == "Add").

Additionally, every if line needs to use ==, not =.

Lastly, all you need to do is use double quotes instead of single ones.

string input = Add;

Add is undeclare variable. I know you want to assign string of "Add" to input, so you change it to "Add". (Remember "...." is string, without those double quote, it is a variable)

if (Add)

What kind of condition is that? What is Add?

else if (input = 'Subtract')
  • = is assign operator, and == is equalition operator.
  • '' single quote is for single character, "" double quote use for string

correct ..

you need to specify string in double quotes...and use double equal to(==) sign to check equality in if()

The following is pretty much useless:

string input = Add;

Change it to the following to just declare the var.

string input;

The entered value (Add, Subtract, Multiply, Divide) will be placed in the var input.
This means, you want to check which value was entered.
You could use the following to check it:

if (!strcmp(input.c_str(), "Add"))
{
   // do your code to add the values.
}
else if (!strcmp(input.c_str(), "Subtract"))
{
   // do your code to subtract the values.
}

Hope this helps.

Kind regards,
Erik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.