Thread: Newbie, needs lots of help

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Unhappy Newbie, needs lots of help

    I just got into C++ prgramming and am trying to start simple by making a simple console application that takes two integers and adds them. I have no clue what is wrong with this code, although it is probly a simple fix I have no clue what im doin.

    #include <iostream>

    using namespace std;

    int main()
    {
    int firstnumber;
    int secondnumber;
    int answer;
    cout<<"Enter an integer: ";
    cin>>firstnumber;
    cout<<"Enter a second integer: ";
    cin>>secondnumber;
    do
    {
    firstnumber+secondnumber==answer;
    }
    cout<<"The sum is: "<<answer;
    return(0);
    }

    I am using Dev C++ compiler. It says there is a parse error in the bolded line. I dont know where it is, probably becuase I dont know wat a parse error is. Someone please help.

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    There is no need for the do function.
    Code:
      #include <iostream>
    
    using namespace std;
    
    int main()
    {
          
    using namespace std;
    int firstnumber;
    int secondnumber;
    int answer;
    cout<<"Enter an integer: ";
    cin>>firstnumber;
    cout<<"Enter a second integer: ";
    cin>>secondnumber;
    
    firstnumber+secondnumber==answer;
    
    cout <<"The sum is:"<<answer;
    
    return 0;
    }
    Also, when posting code please use the code format. (Just type [ code ] with no spaces and [ / code] no spaces to end).
    Last edited by RealityFusion; 04-21-2004 at 11:18 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    It also helps to look a line or two above the line the compiler tells you to look at:

    firstnumber+secondnumber==answer;

    should be

    firstnumber+secondnumber=answer;

    Difference? "==" is conditional (Example: if(answer==5){..})
    "=" is an assignment (Example: x = 5;)

    sean

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The 'do' statement causes a infinite loop that can be terminated using the 'break' command, or using a 'while' condition. For example:
    Code:
    BOOl bExit=FALSE;
    char chInput;
    do
    {
        cin >> chInput;
    } while (chInput!='q')
    That loop will run until the user enters 'q'.

    EDIT: typo
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even

    answer = firstnumber+secondnumber;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Or even

    answer = firstnumber+secondnumber;
    Lol noone even noticed that obvious error. What have we become?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by bennyandthejets
    The 'do' statement causes a infinite loop that can be terminated using the 'break' command, or using a 'while' condition.
    Untrue.

    The do statement requires a while to terminate the loop. do by itself is not an infinite loop -- it's an error.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Smile

    Thanks for all the help. As you see I still have a lot to learn, and will probably be asking for more help in the future. Thanks again for your quick replys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. Newbie Question
    By ejohns85 in forum C# Programming
    Replies: 1
    Last Post: 12-17-2006, 04:06 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. New to programming: newbie ??'s
    By ndbrown21 in forum C Programming
    Replies: 6
    Last Post: 06-09-2003, 03:16 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM