Need help!

This is a discussion on Need help! within the C++ Programming forums, part of the General Programming Boards category; Salut! i am a newbie in c++ programming and as a beginner i am in trouble.. i wrote a simple ...

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Question Need help!

    Salut! i am a newbie in c++ programming and as a beginner i am in trouble.. i wrote a simple program in c++ for adding 2 numbers:
    Code:
    #include <iostream>
    
    int main()
    
      {
        
       int n1, n2, sum;
       sum  =  n1 + n2;
       cout << "Give the first number:" << endl;
       cin    >> n1;
       cout << "Give the second number:" << endl;
       cin    >> n2;
       cout  << "The result is...:" sum;
       
       return 0;
    
      }
    what's wrong with that code..? i am using Dev C++ v4 compiler.when i debug it, it show me no errors but when i execute it, first ask me for the 2 numbers and then terminated without the result..any help??
    Last edited by Salem; 09-27-2004 at 06:35 AM. Reason: Remember your code tags in future - read the "read before posting" post

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    you sum n1 and n2 before you get the data from the cin.

    initialize the variables otherwise they contain random garbage
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    You may need to flush othe ostream buffer try.
    cout << "The result is...:" << sum << endl;
    and like he said do the calculation after you get the numbers.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    cout, cin, and endl are in the std namespace, and you haven't specified that they are. There are two ways to do this. The first way is to specify it manually with the scope resolution operator( :: ) like so:

    Code:
    std::cout <<"whatever" <<std::endl;
    The second(and usually preferred) method is to use the entire std namespace with a using statement:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( )
    {
    	cout <<"whatever" <<endl;
    }
    The using statement prevents you from having to put std:: in front of the objects

  5. #5
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Code:
     #include <iostream>
     
     using namespace std;
     
     int main()
       {
     
        int n1, n2, sum;
        cout << "Give the first number:" << endl;
        cin	>> n1;
        cout << "Give the second number:" << endl;
        cin	>> n2;
     
        sum  =  n1 + n2;
     
        cout  << "The result is...:" << sum << endl;
     
        return 0;
       }
    The namespace issue has been explained. The reason why you need to do sum= n1 +n2 after the values have been read from the user is, that C or C++ are no logical languages. sum= n1 +n2 is not a definition but a command that is only performed once with the result saved to sum. So you have to do things in the right order.
    You also forgot a "<<" operator between "The result is...:" and sum.

  6. #6
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    Thanks for the help guys..but when i am trying to run the program, it terminates without giving the result..? only asks for the 2 numbers...
    i am using Dev-C++ compiler v5

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That's because after the result is printed the program exits and the console is closed. If you have the header conio.h (non-standard header) you could put getch(); at the end to wait for a key to be pressed, or maybe use std::cin with a dummy variable again.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Gimme a F
    Gimme a A
    Gimme a Q
    What have you got?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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.

Popular pages Recent additions subscribe to a feed

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