954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

New to C++, need help

hi im new to c++ (started today) and im reading a book about c++ (called C++ Programming A)

Well i started to read and learned the first code and wrote it in Microsoft Visual C++ and i get a error (error C2065: 'cout' : undeclared identifier) i dont know why cause ive written exactly what it says in the book, and can post the code to

# include
int main ()
{
int tal1=5, tal2=11, produkt;
produkt=tal1*tal2;
cout << "Produkten är" << produkt;
}


one more question what am i supposed to write in () after int main.?

Thanks in forehand ( and by the way a great forum)

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

Try declaring product on a new line. For your second question, nothing goes in the () after main. You only use them when working in functions and passing variables.

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

ya im gona start c++ soon, cause every one tells me to, but ill promise you 1 thing. I know nothing. Im starting from scratch, but what should i already know. Html, perl ect. cause i dont know either. I was recomendd to learn perl first, but then c++ from another guy, then Qbasic from another. IM SO CONFUSED!. lol.

whats C/c++ for
Whats Qbasic or basic for
whats Perl for
whats open gl for
whats DirectX for

Sphyenx
Posting Whiz
366 posts since Aug 2004
Reputation Points: 13
Solved Threads: 3
 

type after the include line :
using namespace std;

that is because cout defined in this namesoace that is all

stg110
Newbie Poster
11 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

stg110 thanks for the answer but i dont fully understand when i should use std; and :

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

alwayes when use c++ u must add this line to make use of the new functionalty in c++ u need not add this line if u will write a normal c code

stg110
Newbie Poster
11 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

ok i know why to use it but not when cant u write it in the code i wrote or something.?

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

as a role when u write a c++ code type it after finishing the include lines
in other words u alwayes u can(or u must) type it

stg110
Newbie Poster
11 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

i wrote the thing after but it still doesnt work or is it wrong again?


# include
int main()
{
int tal1, tal2, produkt;
tal1=5;
tal2=11;
produkt=tal1*tal2;
cout <<"Produkten är" << produkt;
using namespace std;
}
(or should i write "using namespace std;" here)?

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

ok i fixed it now but got one more question (heres the final code i used) when i click run the program it opens but it closes itself in about 0.01 sec after it have opened. whats the wrong now with the code, and i dont get any errors when compiling.

# include
int main()
{
using namespace std;
int tal1, tal2, produkt;
tal1=5;
tal2=11;
produkt=tal1*tal2;
cout <<"Produkten är" << produkt;
}

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

ok i fixed it now but got one more question (heres the final code i used) when i click run the program it opens but it closes itself in about 0.01 sec after it have opened. whats the wrong now with the code, and i dont get any errors when compiling.

# include int main() { using namespace std; int tal1, tal2, produkt; tal1=5; tal2=11; produkt=tal1*tal2; cout <<"Produkten är" << produkt; }

I am a total newbie to c++ myself however i THINK its something like:

# include <iostream> 
using namespace std;
int main()
{ 
int tal1, tal2, produkt;
tal1=5;
tal2=11;
produkt=tal1*tal2;
cout <<"Produkten är" << produkt; 
cin >> mong;
return 0;
}


Basically it ends because it reached the end of the program, if you add an input to the end then the program isnt finished till you do the input.
I have not tested this and have no idea if it works, like i said im a c++ newbie too :o

YoungCoder

YoungCoder
Junior Poster
193 posts since Aug 2004
Reputation Points: 13
Solved Threads: 2
 

YoungCoder i compiled the code and it didnt go it was cin that was the error but i erased that line ( cin >> mong;) then it worked like mine it opened and closed.

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

ok i got it to work now the final code was


# include
using namespace std;
int main()
{
int tal1, tal2, produkt;
tal1=5;
tal2=11;
produkt=tal1*tal2;
cout <<"Produkten is " << produkt;
}

and i compiled it in Visual C++ now and it worked.

RobinA
Newbie Poster
9 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

Other than using :-
#include
using namespace std;

you can also try to replace it with this:-
#include

But it is adviceable to use the former as it is of the latest format.
Good Luck!

Pikachu
Newbie Poster
19 posts since Aug 2004
Reputation Points: 11
Solved Threads: 0
 

Yea,using #include is now the latest standard.

namespaces are something like a sealed off area of code,out side which you can use the same names with out conflicting with the name in the namespace scope.

You can also access namespace vars like namespace::std, std::cout

Have fun,ALSO learn to use the Standard Template Library (STL)

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You