Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone,
i'm a beginner and have a small problem regarding a code.
the compiler flags an error saying:-
VB
error C2065: 'cout' : undeclared identifier


here is the code
C++
char downer(char[13])
{
cout<<"hello";
}

using namespace std;

int main()
{
char ocean[13];
puts(" please enter the name of the ocean ");
cin>>ocean;
downer(ocean);
}


please help me to deal with this error.

here is a link to the whole code( just in case you want to reffer to it )
link[]
Posted
Updated 1-Sep-13 5:22am
v2
Comments
Sergey Alexandrovich Kryukov 1-Sep-13 11:40am    
Wrong. The problem has nothing to do with "functions".
—SA
Philippe Mori 2-Sep-13 11:24am    
If you write a using statement, then it should be done before the function that need it.

The problem has nothing to do with "functions".
You should use #include <iostream> and also using namespace std:
C++
#include <iostream>
using namespace std;

//...

void SomeFunction(/* ... */)
{
    cout << "Now working!";
    //... 
}

//...
You also need to learn how namespaces work, and probably a lot more.

[EDIT #1]

I can see the changes in the question. Still, it need fixes. The code lacks #include <iostream>. Now, the code won't compile, because downer does not return anything. So, change return type to void. And parameter of this function is pointless, removed it from function definition and the call. (In future, you may need char* parameter, to pass a string without specification of its length; usually, in C and C++, null-terminated string is used.)

That's all.

[EDIT #2]

As Stefan_Lang pointed out, there is are considerable benefits of not using "using namespace". You can use fully-qualified names instead. This way excludes name clash, and — very important in this very case — promotes understanding of what really goes on.

The output line shown above will look like
C++
std::cout << "Working without using namespace";


Good luck,
—SA
 
Share this answer
 
v7
Comments
AlwaysLearningNewStuff 1-Sep-13 16:28pm    
He did use it, see the link in his post at the bottom.
Sergey Alexandrovich Kryukov 1-Sep-13 21:35pm    
Who cares what's written there? This is all what's required. This "using" appeared after I put this answer.
—SA
AlwaysLearningNewStuff 1-Sep-13 23:21pm    
I believe you, but the link at the bottom of the page shows his full code, and it is slightly different, and included both statements( include<iostream> and using namespace std).

I thought that it was his original one, but maybe he edited it in the meantime, according to your instructions.

Never mind.

Regards.
Sergey Alexandrovich Kryukov 2-Sep-13 0:33am    
Not exactly. The code still wrong, and it would not compile. Look, this is lame stuff which does not deserve mentioning, but I answered how to make the code work, please see after [EDIT].
—SA
AlwaysLearningNewStuff 2-Sep-13 1:07am    
You are right, there is no need to discuss about this.

The OP has enough answers to correct his solution, and that is what the purpose of this section is all about.

Maybe there was a misunderstanding from my side, since I saw the question later, but there is no need to debate about such a pointless thing.

Thank you for reply, and I apologize for any misunderstanding that might have happened.

Regards.
I have rewritten your code, so you can see your mistakes.

Before that, a small set of explanations, since I believe, based on your code, that you are familiar with C, so these explanations might help you:

Let us start with your function.

Since you just want to write something to the console, using cout, you do not need the return value ( see the comments in the code, char is changed in void ).

Also, I have changed cout statement to output the ocean string, that you have passed to the function, see the code.

You have passed the string to the function downer in a wrong way, I have changed that too.

Forget about puts, use cout instead, I have changed that in my code too.

Instead of float, I have used double as a type for your variables with decimal points, see the code.

I have added return statement at the very end of the code, this signals that main exited without error.

It seems to me that you know C and try to learn C++. I hope that this code will help you, if you need anything just post a comment, and I will help you.

Finally, the code:

C++
#include<iostream>
     
using namespace std;
   
void downer( char* test) // <-- this is how you pass a string to a function
{
        // endl just tells cout to go to new line, it is like '\n' in C

	cout<<"hello kaka" << endl << test<< endl;
}
     
int main()
{
	char ocean[13]; 

	int boats,fiiden;

        // use double instead of float for decimal numbers

	double poden=11.29387,inden=12.989,atden=10.28172,arden=9.192772;
        
	cout << " this program calculates the number of fish in any specified ocean " << endl;

        // no need for puts, use cout instead

	cout << " please enter the name of the ocean " << endl;
        
	cin >> ocean;
        
	downer( ocean );

        return 0; // this indicates that main exited successfully without errors!
}
 
Share this answer
 
v3
Comments
A.U.K 7-Sep-13 9:44am    
( if it is okay for me to call you "bro" for "brother" )
thanks a lot bro,
you see this solution has helped me a lot in my learning process,
by the way, i'm just a begginer and i don't know c :-p, i just jumped to c++ and thus have no knowledge regarding pointers and all that complicated stuff :-(
although i am still learning, i like to try new stuff, so i end up typing code which requires skills of high callibre.
anyways i just wanted to tell u thanks for the help because no one has ever corrected me that way, not even my computer science teacher!!!
so i was actually hoping that we could be friends on FB or G+( that is if you don't mind )
and get to know each other:-)
please let me know:-)
A.U.K 7-Sep-13 9:57am    
hello bro,
it's me again, i just read all the comments for the soltion:-)
although i din't understand 70% of that stuff since it's of high level,
i do understand that you guys tried to help me in all possible ways in which u can.
thank you for your valuble contribution:-)
AlwaysLearningNewStuff 8-Sep-13 22:31pm    
It would not be productive to do further explanations here, you have my e-mail in my comment, so send me your questions, and I will try to help you.

Regards.
Because you forgot to include the header file...
Try:
C++
#include <iostream>
 
Share this answer
 
v2
Comments
A.U.K 1-Sep-13 11:35am    
hello sir,
i have already included the headeer file,
please check the link to verify :-)
Mohibur Rashid 2-Sep-13 0:41am    
either you use std namespace. i.e.
using namespace std;
or
write like this
std::cout<<"whatever";

try to understand c++ namespace
A.U.K 7-Sep-13 9:46am    
thank you sir
"cout" uses a library which is called iostream. so you have to use its namespace.


#include <iostream>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900