Search code examples
c++stringc-stringsstrcmp

How to return a C-String from a function and display it


I can't get my program to display the whole c-string, it only displays the first letter! I am trying to write a function that asks the user to input 2 c-strings. I then have a separate function to determine which c-sting is greater alphabetically, and another to display. Every time it displays the c-strings it only displays the first letter though. I can't figure out what I'm doing wrong. I need it to display the whole c-string.

Here is the function that asks the user for two c-strings:

const char *getData(char *c, char *d)
{
    cout << "Enter the first c-string: ";
    cin.ignore();
    cin.getline(c, 50);
    cout << "Enter the second c-string: ";
    cin.getline(d, 50);
    return c, d;
}

Here is the function that compares the c-strings

const char findBigger(char *c, char *d)
{
    if (strcmp(c, d) == 0)
        return *c;
    else if (strcmp(c, d) < 0)
        return *c;
    else if (strcmp(c, d) > 0)
        return *d;
}

And here is the function that displays the inputs and the greater c-string

void displayBigger(const char *c, const char *d, const char *maxChar)
{
    cout << "The first c-string is " << c << endl;
    cout << "The second char is " << d << endl;
    cout << "The bigger value is " << maxChar << endl;
    cout << "__________________________________________" << endl;
}

In main() I have

char first[50],
second[50]'
maxArray[50];
getData(first, second);
*maxArray = findBigger(first, second);
displayBigger(*first, *second, *maxArray);

The output of the program is:

Enter the first c-string: apple pie
Enter the second c-string: peach pie
The first char is a
The second char is p
The bigger value is a
__________________________________________

How do I get it to print the whole c-string? It should display "apple pie" and "peach pie"


Solution

  • You have several issues with your code, not withstanding the obvious one of not using std::string.

    Issue 1:

    const char findBigger(char *c, char *d)
    {
        if (strcmp(c, d) == 0)
            return *c;
        else if (strcmp(c, d) < 0)
            return *c;
        else if (strcmp(c, d) > 0)
            return *d;
    }
    
    1. This function should return const char *, not const char.
    2. You are needlessly calling strcmp 3 times with the same set of arguments. All you need is to call strcmp once and test that value:

    The fixed code could look like this:

    const char* findBigger(const char *c, const char *d)
    {
        if ( strcmp(c, d) <= 0 )
            return c;
        return d;
    }
    

    Note that the arguments are const char *, since you are not changing the data contained within the C-strings.


    Issue 2:

    char first[50],
    second[50]'
    maxArray[50];
    getData(first, second);
    *maxArray = findBigger(first, second);
    displayBigger(*first, *second, *maxArray);
    
    1. The calling function (assuming it is main) declared maxArray as an array of char, but should have been declared as a const char *.

    2. The call to displayBigger should have pointers passed to it.

    Here is the fixed version:

    int main()
    {
        char first[50],
        second[50];
        
        getData(first, second);
        const char *maxArray = findBigger(first, second);
        displayBigger(first, second, maxArray);
    }
    

    Issue 3:

    const char *getData(char *c, char *d)
    {
        cout << "Enter the first c-string: ";
        cin.ignore();
        cin.getline(c, 50);
        cout << "Enter the second c-string: ";
        cin.getline(d, 50);
        return c, d;
    }
    

    The getData does not need to return const char *, since the arguments are pointers to char buffers that will be filled in. Then this line:

    return c, d;

    does not return two values, only d due to the comma operator. This becomes a moot point, since it is not necessary once the function is declared as void.

    The fixed version:

    void getData(char *c, char *d)
    {
        cout << "Enter the first c-string: ";
        cin.ignore();
        cin.getline(c, 50);
        cout << "Enter the second c-string: ";
        cin.getline(d, 50);
    }