1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Begginer C++ Error

Discussion in 'C++' started by Vip3r, Aug 5, 2008.

  1. Vip3r

    Vip3r New Member

    Joined:
    Aug 5, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Dont have one
    Location:
    New jersey
    Hey guys, im just learning C++ and i really want to learn it, but im having some compiling errors and i've looked around and i dont know what else to do. So i was hoping one of you guys could help me out.

    so this is my code:

    Code:
    #include <iostream>
    
    int main()
    {
       int integer1, integer2, sum;
    
       std::cout << "Enter first integer\n";
       std::cin >> integer1;
       std::cout << "Enter second integer\n";
       std::cin >> integer2;
       sum = integer1 + integer2
       std::cout << "Sum is " << sum << std::endl;
    
       return 0;
    }
    
    Its pretty simple enough, and i know what its supposed to do, but its not becuase its not compiling.

    These are the errors i get when i compile and try to run the file:

    Code:
    c:\users\bioshock\documents\c++\untitled1.cpp: In function `int main()':
    c:\users\bioshock\documents\c++\untitled1.cpp:12: use of namespace `std' as expression
    c:\users\bioshock\documents\c++\untitled1.cpp:12: parse error before `::'
    
    Im not really sure what that meant, so any help would be nice.

    Thanks.
     
  2. luckylucy

    luckylucy New Member

    Joined:
    Aug 5, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    try removing all the std keywords before cin and cout.. tell if it works..
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Having the thread in the right forum helps. Moved to C-C++ forum
     
  4. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    put the semicolon
    sum = integer1 + integer2;
     
    Last edited: Aug 5, 2008
  5. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    first sol :
    Code:
    #include <iostream.h>
    int main()
    {
       int integer1, integer2, sum;
    
       cout << "Enter first integer\n";
       cin >> integer1;
       cout << "Enter second integer\n";
       cin >> integer2;
       sum = integer1 + integer2;
       cout<<"Sum is " <<sum<< endl;
       return 0;
    }
    

    second solution:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int integer1, integer2, sum;
    
       std::cout << "Enter first integer\n";
       std::cin >> integer1;
       std::cout << "Enter second integer\n";
       std::cin >> integer2;
       sum = integer1 + integer2;
       std::cout<< "Sum is " << sum << endl;
       return 0;
    }
    
     
  6. Vip3r

    Vip3r New Member

    Joined:
    Aug 5, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Dont have one
    Location:
    New jersey
    Oh thanks for the reply's and sorry about having it in the wrong section. I put it in there becuase i thought you couldnt ask questions in the regular programming sections.
     
  7. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    You are not suppose to use iostream.h in C++. If you dont want to use std:: again and again then this would be a better solutio

    Code:
    using std::cout;
    using std::endl;
    
     
  8. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    your statement std::cout << "Enter first integer\n"; is wrong. Because the keyword ::
    is the scope resolution key word. here is incorrect.
    you should remove it from here.
     
  9. zamjad

    zamjad New Member

    Joined:
    Oct 7, 2008
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    Please dont spread wrong information. First :: is not a keyword it is a scope resolution operator. Second first verify your post with any good C++ compiler, if you dont have access to any then check your code with online C++ compiler from here

    http://www.comeaucomputing.com/tryitout/

    And here is from latest draft of C++ standard

    Section 3.3.5.2

     
  10. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    yes, you are right zamjad. I have write it by mistake. Yes it is not keyword it is scope resolution operator.
    sorry for that mistake.
     

Share This Page