Results 1 to 11 of 11

Thread: Using cout to display function return output

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

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

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

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

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    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'

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  5. #5

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Using cout to display function return output

    Quote 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?

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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;
    }
    Show Appreciation. Rate Posts.

  7. #7

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Using cout to display function return output

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

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Using cout to display function return output

    Sorry, I am not sure about MS based C++ compilers.

    So, is it still not working??
    Show Appreciation. Rate Posts.

  9. #9

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Using cout to display function return output

    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.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Using cout to display function return output

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

  11. #11

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Using cout to display function return output

    Quote 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
  •  



Click Here to Expand Forum to Full Width