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 <iostream>
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)

Recommended Answers

All 14 Replies

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.

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

type after the include line :
using namespace std;

that is because cout defined in this namesoace that is all

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

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

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

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

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


# include <iostream>
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)?

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 <iostream>
int main()
{
using namespace std;
int tal1, tal2, produkt;
tal1=5;
tal2=11;
produkt=tal1*tal2;
cout <<"Produkten är" << produkt;
}

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 <iostream>
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 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.

ok i got it to work now the final code was


# include <iostream>
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.

Other than using :-
#include <iostream>
using namespace std;

you can also try to replace it with this:-
#include <iostream.h>

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

Yea,using #include <iostream> 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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.