Functions that return strings? (help)

This is a discussion on Functions that return strings? (help) within the C++ Programming forums, part of the General Programming Boards category; I'm currently trying to make a program that gets the name of 4 different people, but a function gets the ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    73

    Functions that return strings? (help)

    I'm currently trying to make a program that gets the name of 4 different people, but a function gets the names and returns them... Well, that's where I'm stumped. I'm pretty sure I have to use a global pointer and some how pass the address and recieve the value???

    here's the structure I'm using to hold the name (and other info):
    Code:
    struct stats 
    {
      char name[12]; //person's name in string.
    };
    Then, I have this in the main function to get the names:
    Code:
      stats member[NUMBER_OF_MEMBERS]; //declaration
      
      for (i = 0; i <= NUMBER_OF_MEMBERS; i++) 
        member[i].name = getName(i); //function call...
    NUMBER_OF_MEMBERS is defined under the include statements as 3. Also, the program does more, but I've simplified it for posting purposes.

    So, how do you use a function to return a string? I've searched the boards and online.. but it's hard to search when you have such a specific question...

    Oh yeah, while I'm posting, how do you fill the background of the screen with one color? I know how to fill the background of a character with a color, but not the whole screen... Do I just have to use a loop and fill the screen with "\t"'s to fill it???

    Thanks.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    As it is, you have to use some method of copying the string into your name structure member. If it were simply a char pointer you could allocate memory in the function and return it.
    Code:
    // No error checking done
    char* getName(size_t length)
    {
        char* mem = new char[length];
    
        cout<<"Enter a name: "<<flush;
        cin.getline(mem, length, '\n');
    
        return mem;
    }
    
    ...
    
    member[i].name = getName(12); // Don't forget to delete later
    As it is you need to do something more like this:
    Code:
    void getName(char* mem, size_t length)
    {
        cout<<"Enter a name: "<<flush;
        cin.getline(mem, length, '\n');
    }
    
    ...
    
    getName(member[i].name, 12);
    If you haven't noticed, to return a C string or pass one to a function, you use char*. Of course in C++ it's much better to use the std::string class instead.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Yeah, I'm using C++ to create this program (dev-C++ to be exact). I've been learning and learned most of what I know from a C programming book. I've yet to delve into a lot of the things C++ has to offer.

    Thanks for your help. It worked great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21