Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Begginer C++ Error (http://www.go4expert.com/forums/begginer-cpp-error-t12705/)

Vip3r 5Aug2008 10:12

Begginer C++ Error
 
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 5Aug2008 12:12

Re: Begginer C++ Error
 
try removing all the std keywords before cin and cout.. tell if it works..

shabbir 5Aug2008 12:22

Re: Begginer C++ Error
 
Having the thread in the right forum helps. Moved to C-C++ forum

debleena_doll2002 5Aug2008 17:49

Re: Begginer C++ Error
 
put the semicolon
sum = integer1 + integer2;

debleena_doll2002 5Aug2008 17:50

Re: Begginer C++ Error
 
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 5Aug2008 23:55

Re: Begginer C++ Error
 
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 20Dec2008 01:10

Re: Begginer C++ Error
 
Quote:

Originally Posted by debleena_doll2002 (Post 33322)
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 1Jan2009 15:53

Re: Begginer C++ Error
 
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 1Jan2009 23:34

Re: Begginer C++ Error
 
Quote:

Originally Posted by skp819 (Post 40977)
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 2Jan2009 13:09

Re: Begginer C++ Error
 
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.


All times are GMT +5.5. The time now is 06:59.