Home > OS >  string array function with pointers
string array function with pointers

Time:12-02

function.cpp

void enterFactoryNames(string* )
{
    string FacNames;
    for(int i = 1; i <= SIZE; i  )
    {
        cout << "Name of Factory  "<< i << " : ";
        getline(cin, FacNames);
        cout << endl;
    }

main.cpp

int main()
{
    //*****VARIABLE DEFINITIONS*****//
    int years;
    string factoryNames[SIZE];
    string horizontalLine(80,'-');

    cout << endl << endl << horizontalLine << endl;
    cout << horizontalLine << endl;

    //*****GET USER DATA*****/
    enterFactoryNames(factoryNames);
    
    cout << "\nHow many years of data do you have?\n";
    cin >> years;
    
    //make each array dynamically and enter data into the array from the user
    cout << "\n\nPlease enter data for each factory.\n";
    for(int x = 0; x < SIZE; x  )
    {
        cout << horizontalLine << endl;
        cout << "\n\nFACTORY:  " << *(factoryNames x) << endl;
    }
    
    return 0;
}

My issue is that the string values I get from the enterFactoryNames function won't appear when I call it again towards *(factoryNames x).

CodePudding user response:

you should storing the user input in the array. Like this:

void enterFactoryNames(string* strs)
{
    string FacNames;
    for(int i = 1; i <= SIZE; i  )
    {
        cout << "Name of Factory  "<< i << " : ";
        getline(cin, FacNames);
        cout << endl;

        /* this is the store operation! */
        /* `strs` is the `factoryNames` */
        strs[i - 1] = FacNames;
    }

}

CodePudding user response:

Solution

You're not actually saving the result they type in anywhere.

void enterFactoryNames(string* )
{
    string FacNames;
    for(int i = 1; i <= SIZE; i  )
    {
        cout << "Name of Factory  "<< i << " : ";
        getline(cin, FacNames);
        cout << endl;
    }
}

FacNames is a local variable to the enterFactoryNames function that only exists within the body of the function. Specifically between the first { and last } in the above snippet. This is what C calls block scope.

Every iteration of the loop, you call getline and save the result in FacNames overwriting any previous value it had.

At the end of the block in which it is defined, the variable is destroyed and ceases to exist.

To fix this you need to first give a name to the argument that is passed in, such as nameBuffer (currently unnamed and thus inaccessible) like so:

void enterFactoryNames(string* nameBuffer)

This allows use of the name nameBuffer to refer to the argument within the scope of the function.

Next you'll need to actually use it by assigning the value in FacNames after you've read a new one in to the correct index in nameBuffer.

nameBuffer[i - 1] = FacNames

Leaving you with the complete function looking like:

void enterFactoryNames(string* nameBuffer)
{
    string FacNames;
    for(int i = 1; i <= SIZE; i  )
    {
        cout << "Name of Factory  "<< i << " : ";
        getline(cin, FacNames);
        cout << endl;
        nameBuffer[i - 1] = FacNames;
    }

}

That should work as you have written but to be more idiomatic C you can improve this a bit further. I understand this is likely an assignment where you may not be free to change everything, but briefly...

Use [] when accessing an array

A pointer and an array are not the same thing. Due to pointer math you can treat them as nearly the same but you lose a bit of information for the human reader of your code when you do. In your code enterFactoryNames takes a pointer. Are we sure its an array or is it a pointer to a single instance of string? Maybe it's nullptr? The plural in the name makes it seem like more than one factory will be entered but perhaps we'll store them in a single string with some delimiter? Who knows? Use [] when it's an array.

void enterFactoryNames(string[] nameBuffer)

Additionally when you're printing the result out, use [] there as well.

cout << "\n\nFACTORY: " << factoryNames[x] << endl;

Consistent Naming

All of your variables are camelCase except FacNames. Additionally FacNames is plural when it is a single factory name and it is the only variable you decided to shorten for some reason. For consistency and readability I'd rename it to name or perhaps factoryName. This would also give you the opportunity to change nameBuffer to something more descriptive like factoryNames (plural, since it is an array).

Prefer to start loop counters at 0 when indexing into an array

It's more common so others reading your code will not have to think twice or make mistakes if editing the code within the loop. It also moves the likely problem of misadding to the UI where it's not dangerous and more obvious than the array itself.

Also prefer to use 'i' when using a generic name.

So at this point we have:

void enterFactoryNames(string[] factoryNames)
{
    string name;
    for(int i = 0; i < SIZE;   i)
    {
        cout << "Name of Factory  "<< (i   1) << " : ";
        getline(cin, name);
        cout << endl;
        factoryNames[i] = name;
    }
}

int main()
{
    //*****VARIABLE DEFINITIONS*****//
    int years;
    string factoryNames[SIZE];
    string horizontalLine(80,'-');**strong text**

    cout << endl << endl << horizontalLine << endl;
    cout << horizontalLine << endl;

    //*****GET USER DATA*****/
    enterFactoryNames(factoryNames);
    
    cout << "\nHow many years of data do you have?\n";
    cin >> years;
    
    //make each array dynamically and enter data into the array from the user
    cout << "\n\nPlease enter data for each factory.\n";
    for(int i = 0; i < SIZE; i  )
    {
        cout << horizontalLine << endl;
        cout << "\n\nFACTORY:  " << factoryNames[i] << endl;
    }
    
    return 0;
}
  • Related