Newbie, needs lots of help

This is a discussion on Newbie, needs lots of help within the C++ Programming forums, part of the General Programming Boards category; I just got into C++ prgramming and am trying to start simple by making a simple console application that takes ...

  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-22-2004 at 12:18 AM.
    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
    benforbes@optusnet.com.au
    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
    32,832
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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?
    benforbes@optusnet.com.au
    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,161
    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, 07: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, 04:16 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21