Thread: C++ proggie syntax problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    C++ proggie syntax problem

    Hi there,

    I am reading and entering the code from O'Reilly's Practical C++ Programming book. my g++ is complaining of syntax errors in example 7-1 from chapter 1. Might somebody explain why I am getting these errors.

    Here are the errors:

    --- snip ---

    % g++ -g -Wall -o calc.exe calc.cpp
    calc.cpp: In function `int main()':
    calc.cpp:18: warning: suggest parentheses around assignment used as truth value
    calc.cpp:19: syntax error before `+='
    % g++ -v
    Using builtin specs.
    gcc version 2.95.4 20020320 [FreeBSD]
    %

    --- snip ---


    Code:
    #include <iostream>
    
    int   result;    // the result of the calculations
    char  oper_char; // operator the user specified
    int   value;      // value specified after the operator
    
    int main()
    {
      result = 0; // initialize the result
    
      // Loop forever ( or till we hit the break statement)
      while (true) {
        std::cout << "Result: " << result << '\n';
    
        std::cout << "Enter operator and number: ";
        std::cin >> oper_char >> value;
    
        if (oper_char = '+') {
          return += value;
        } else {
          std::cout << " Unknown operator " << oper_char << '\n';
        }
      }
      return (0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Look up the difference between = and ==.

    2) In your opinon what does this line do:

    return += value;

    When you write:
    Code:
    int num = 10;
    num += 1;
    that is short hand for:
    Code:
    int num = 10;
    num = num + 1;
    Last edited by 7stud; 02-01-2006 at 10:57 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (oper_char = '+') {
    Well = is assignment so hopefully it's a typo and should be ==:
    if (oper_char == '+') {

    > return += value;
    This should probably be:
    result += value;
    return is a keyword is C/C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  2. Syntax problem
    By newbiec++ in forum C++ Programming
    Replies: 6
    Last Post: 08-26-2005, 08:26 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 02:54 AM