Thread: Need help!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Registered User
    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.

  3. #3
    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

Popular pages Recent additions subscribe to a feed