If i have the following code:
How can i use cout to display this information?Code:string clientName = "Dan";
string getClientName(void)
{
return clientName
}
p.s. I tried cout << getClientName() << endl; but it didnt work.
Printable View
If i have the following code:
How can i use cout to display this information?Code:string clientName = "Dan";
string getClientName(void)
{
return clientName
}
p.s. I tried cout << getClientName() << endl; but it didnt work.
What does "didn't work" mean? I see nothing wrong with that code, except you are missing a ";" after "return clientname".
When i use this code I receive an error on line 10, the error is 'error C3861: 'getClientName': identifier not found'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;
}
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.Quote:
Originally Posted by CornedBee
How do i use a function declaration?
"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.
or if you define the function above main() then you would not need function declaration.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;
}
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.Quote:
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.Quote:
Originally Posted by Harsh Gupta
Throw away both your book and your lecturer.Quote:
Originally Posted by x-ice
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
http://users.aber.ac.uk/auj/voidmain.shtml
Ok i get your point, i will use int main() insteadQuote:
Originally Posted by CornedBee