Thread: char function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    char function

    hey,
    This should be pretty simple:

    I need to make a function which returns a person's name from a file

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100])
    {
    char r[20];
    ifstream file(def);
    file.getline(r, 20);
    return r[0];
    }
    
    
    int main()
    {
    char name[20];
    char def[100] = "c:\\test.txt";
    
    cout << loadName(def);
    cin.get();
    }
    What ends up happening is, I have to return the first letter of the person's name, I can't just go
    return r;

    How would I be able to return a string as opposed to just one char

    Thanks in advanced

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well of course since you are using C++, you could be using STL strings - which make life a lot easier. But obviously you're learning with C-style strings, so we'll keep using them. When you're more comfortable with C++, you might want to have a look at STL strings.

    The best way to return a string is to return a pointer to the first element of the array.

    Code:
    char *pointer = &r[0];
    return pointer;
    If you haven't learned about pointers yet, now might be a good time to do so - try the tutorial on this site. They may not make sense at first, but stick with it - they're useful, no matter how pointless (no pun intended) they seem at first.

    When your other function gets this pointer it can use it to get the string. You can also just pass cout the pointer and it'll print the string to the screen.

    I can't just go
    return r;
    Actually, you can and should - that code would actually return a pointer to the first element of the array! There's your little-known C++ fact of the day.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might be able to try strncpy(), but I'm not very familiar with the C functions so I cant say for sure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    ok,
    I learned pointers.
    I still have the same problem though.
    I can only return one array
    so I used this code:

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100])
    {
    char r[20];
    ifstream file(def);
    file.getline(r, 20);
    char *pointer = &r[0];
    return *pointer;
    }
    
    int main()
    {
    
    def[100] = "c:\\test.txt";
    
    
    cout << loadName(def);
    cin.get();
    }
    as you can see this only returns first letter. I need to return the entire string. Thanks

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char loadName(char def[100], char name[])
    {
    ifstream file(def);
    if (!file.is_open())
    {
       cout << "Unable to open file: " << def << endl;
       exit(1);
    }
    file.getline(name, 30);
    }
    
    int main()
    {
    
    char def[100] = "c:\\test.txt";
    char name[30];
    
    loadName(def, name);
    cout << name << endl;
    cin.get();
    }
    Last edited by swoopy; 07-02-2005 at 11:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM