|
-
Oct 11th, 2006, 07:23 PM
#1
Thread Starter
Fanatic Member
Using cout to display function return output
If i have the following code:
Code:
string clientName = "Dan";
string getClientName(void)
{
return clientName
}
How can i use cout to display this information?
p.s. I tried cout << getClientName() << endl; but it didnt work.
Last edited by x-ice; Oct 12th, 2006 at 05:12 PM.
-
Oct 12th, 2006, 06:55 AM
#2
Re: Using cout to display function return output
What does "didn't work" mean? I see nothing wrong with that code, except you are missing a ";" after "return clientname".
-
Oct 12th, 2006, 11:09 AM
#3
Thread Starter
Fanatic Member
Re: Using cout to display function return output
Code:
#include <iostream>
#include <string>
using namespace std;
string clientName = "Dan";
void main(void)
{
cout << getClientName() << endl; //This is Line 10
}
string getClientName()
{
return clientName;
}
When i use this code I receive an error on line 10, the error is 'error C3861: 'getClientName': identifier not found'
-
Oct 12th, 2006, 11:19 AM
#4
Re: Using cout to display function return output
It doesn't know of the function getClientName() at the point you call it. You need a function declaration.
And main returns int, not void.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 12th, 2006, 04:11 PM
#5
Thread Starter
Fanatic Member
Re: Using cout to display function return output
 Originally Posted by CornedBee
It doesn't know of the function getClientName() at the point you call it. You need a function declaration.
And main returns int, not void.
According to my book (Thinking in C++) and my programming lecturer main can return void.
How do i use a function declaration?
-
Oct 12th, 2006, 04:27 PM
#6
Re: Using cout to display function return output
"Main returning void" were supported by older version of C++ compilers. If you are using ANSI compliant C++ compilers then Main would not return void. Atleast GCC and Borland compilers do not return void types.
Function declaration is done above main() just like you have declared the string variable clientName.
Code:
#include <iostream>
#include <string>
using namespace std;
string getClientName();
string clientName = "Dan";
void main(void)
{
cout << getClientName() << endl; //This is Line 10
}
string getClientName()
{
return clientName;
}
or if you define the function above main() then you would not need function declaration.
Code:
#include <iostream>
#include <string>
using namespace std;
string clientName = "Dan";
string getClientName()
{
return clientName;
}
int main(void)
{
cout << getClientName() << endl; //This is Line 10
cin.get();
return 0;
}
-
Oct 12th, 2006, 04:38 PM
#7
Thread Starter
Fanatic Member
Re: Using cout to display function return output
 Originally Posted by Harsh Gupta
"Main returning void" were supported by older version of C++ compilers. If you are using ANSI compliant C++ compilers then Main would not return void. Atleast GCC and Borland compilers do not return void types.
Function declaration is done above main() just like you have declared the string variable clientName.
Code:
#include <iostream>
#include <string>
using namespace std;
string getClientName();
string clientName = "Dan";
void main(void)
{
cout << getClientName() << endl; //This is Line 10
}
string getClientName()
{
return clientName;
}
or if you define the function above main() then you would not need function declaration.
Code:
#include <iostream>
#include <string>
using namespace std;
string clientName = "Dan";
string getClientName()
{
return clientName;
}
int main(void)
{
cout << getClientName() << endl; //This is Line 10
cin.get();
return 0;
}
I am using Visual Studio 2005, so whatever compiler comes with VC++ 2005.
-
Oct 12th, 2006, 04:44 PM
#8
Re: Using cout to display function return output
Sorry, I am not sure about MS based C++ compilers.
So, is it still not working??
-
Oct 12th, 2006, 05:12 PM
#9
Thread Starter
Fanatic Member
Re: Using cout to display function return output
 Originally Posted by Harsh Gupta
Sorry, I am not sure about MS based C++ compilers.
So, is it still not working??
I have made the changes you said about, and its works. Thanks for your help.
-
Oct 12th, 2006, 07:21 PM
#10
Re: Using cout to display function return output
 Originally Posted by x-ice
According to my book (Thinking in C++) and my programming lecturer main can return void.
Throw away both your book and your lecturer.
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
http://users.aber.ac.uk/auj/voidmain.shtml
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 13th, 2006, 07:42 AM
#11
Thread Starter
Fanatic Member
Re: Using cout to display function return output
 Originally Posted by CornedBee
Ok i get your point, i will use int main() instead
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|