Begginer C++ Error

Vip3r's Avatar, Join Date: Aug 2008
Light Poster
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.
luckylucy's Avatar, Join Date: Aug 2008
Newbie Member
try removing all the std keywords before cin and cout.. tell if it works..
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Having the thread in the right forum helps. Moved to C-C++ forum
debleena_doll2002's Avatar
Ambitious contributor
put the semicolon
sum = integer1 + integer2;

Last edited by debleena_doll2002; 5Aug2008 at 17:53.. Reason: code block...
debleena_doll2002's Avatar
Ambitious contributor
first sol :
Code: cpp
#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: cpp
#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;
}
Vip3r's Avatar, Join Date: Aug 2008
Light Poster
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.
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Quote:
Originally Posted by debleena_doll2002 View Post
first sol :
Code: cpp
#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: cpp
#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;
}
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;
skp819's Avatar
Contributor
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.
zamjad's Avatar, Join Date: Oct 2008
Go4Expert Member
Quote:
Originally Posted by skp819 View Post
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.
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

Quote:
A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to the name of its namespace or the name of a namespace which nominates the member’s namespace in a using-directive;
see 3.4.3.2.
skp819's Avatar
Contributor
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.