c++ compilation error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 3
Reputation: limac is an unknown quantity at this point 
Solved Threads: 0
limac limac is offline Offline
Newbie Poster

c++ compilation error

 
0
  #1
Dec 29th, 2007
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:
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main(void)
  9. {
  10. string input = Add;
  11. int a, b;
  12. cout << "What would you like to do? (Add, Subtract, Multiply, Divide) ";
  13. cin >> input;
  14.  
  15. if (Add)
  16. {
  17. cout << "Enter a value: ";
  18. cin >> a;
  19. cout << "Enter another value: ";
  20. cin >> b;
  21. cout << "The sum of the numbers is " << a + b << endl;
  22. }
  23. else if (input = 'Subtract')
  24. {
  25. cout << "Enter a value: ";
  26. cin >> a;
  27. cout << "Enter another value: ";
  28. cin >> b;
  29. cout << "The difference of the two numbers is " << a - b << endl;
  30. }
  31. else if (input = 'Multiply')
  32. {
  33. cout << "Enter a value: ";
  34. cin >> a;
  35. cout << "Enter another value: ";
  36. cin >> b;
  37. cout << "The product of the two numbers is " << a * b << endl;
  38. }
  39. else if (input = 'Divide')
  40. {
  41. cout << "Enter a value: ";
  42. cin >> a;
  43. cout << "Enter another value: ";
  44. cin >> b;
  45. cout << "The quotient of the two numbers is " << a / b << endl;
  46. cout << "And the remainder is " << a % b << endl;
  47. }
  48.  
  49. return 0;
  50. }
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
Last edited by Ancient Dragon; Dec 29th, 2007 at 9:03 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 2
Reputation: softwareDragon is an unknown quantity at this point 
Solved Threads: 0
softwareDragon's Avatar
softwareDragon softwareDragon is offline Offline
Newbie Poster

Re: c++ compilation error

 
0
  #2
Dec 29th, 2007
Add, Subtract, and etc are strings. You wrote 'Add' it's suppose to be "Add". All strings are denoted with " ", chars are denoted with ' '.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: zoner7 is an unknown quantity at this point 
Solved Threads: 4
zoner7 zoner7 is offline Offline
Junior Poster

Re: c++ compilation error

 
0
  #3
Dec 29th, 2007
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.
Last edited by zoner7; Dec 29th, 2007 at 8:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: c++ compilation error

 
0
  #4
Dec 30th, 2007
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)

  1. 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
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: c++ compilation error

 
0
  #5
Dec 30th, 2007
correct ..

you need to specify string in double quotes...and use double equal to(==) sign to check equality in if()
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: Rippie is an unknown quantity at this point 
Solved Threads: 0
Rippie Rippie is offline Offline
Newbie Poster

Re: c++ compilation error

 
0
  #6
Dec 31st, 2007
The following is pretty much useless:
  1. string input = Add;

Change it to the following to just declare the var.
  1. 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:
  1. if (!strcmp(input.c_str(), "Add"))
  2. {
  3. // do your code to add the values.
  4. }
  5. else if (!strcmp(input.c_str(), "Subtract"))
  6. {
  7. // do your code to subtract the values.
  8. }

Hope this helps.

Kind regards,
Erik
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC