DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   c++ compilation error (http://www.daniweb.com/forums/thread102873.html)

limac Dec 29th, 2007 7:48 pm
c++ compilation error
 
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

softwareDragon Dec 29th, 2007 8:38 pm
Re: c++ compilation error
 
Add, Subtract, and etc are strings. You wrote 'Add' it's suppose to be "Add". All strings are denoted with " ", chars are denoted with ' '.

zoner7 Dec 29th, 2007 8:39 pm
Re: c++ compilation error
 
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.

invisal Dec 30th, 2007 4:14 am
Re: c++ compilation error
 
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

rajatC Dec 30th, 2007 5:07 am
Re: c++ compilation error
 
correct ..

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

Rippie Dec 31st, 2007 10:46 am
Re: c++ compilation error
 
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


All times are GMT -4. The time now is 3:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC