【问题标题】:string array function with pointers带指针的字符串数组函数
【发布时间】:2022-01-08 14:24:45
【问题描述】:

函数.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;
}

我的问题是我从enterFactoryNames 函数获得的字符串值在我再次向*(factoryNames+x) 调用时不会出现。

【问题讨论】:

  • 您实际上并未将用户输入存储在数组中。
  • 那么我将如何创建它?

标签: c++ arrays string pointers


【解决方案1】:

您应该将用户输入存储在数组中。像这样:

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;
    }

}

【讨论】:

    【解决方案2】:

    解决方案

    您实际上并没有保存他们在任何地方输入的结果。

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

    FacNamesenterFactoryNames 函数的局部变量,仅存在于函数体内。特别是在上述 sn-p 中的第一个 { 和最后一个 } 之间。这就是 C++ 所称的 block scope

    循环的每次迭代,您调用getline 并将结果保存在FacNames 中,覆盖它之前的任何值。

    在定义它的块的末尾,变量被销毁并不再存在。

    要解决此问题,您需要首先为传入的参数命名,例如 nameBuffer(当前未命名,因此无法访问),如下所示:

    void enterFactoryNames(string* nameBuffer)
    

    这允许使用名称nameBuffer 来引用函数范围内的参数。

    接下来,您需要在将新值读入nameBuffer 中的正确索引后,通过分配FacNames 中的值来实际使用它。

    nameBuffer[i - 1] = FacNames
    

    给你留下完整的功能看起来像:

    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;
        }
    
    }
    

    这应该可以像您所写的那样工作,但要更惯用 C++,您可以进一步改进它。我知道这很可能是一项任务,您可能无法随意更改所有内容,但简要...

    访问数组时使用[]

    指针和数组是一回事。由于指针数学,您可以将它们视为几乎相同,但是当您这样做时,您会为代码的人类读者丢失一些信息。在您的代码中 enterFactoryNames 需要一个指针。我们确定它是一个数组还是指向单个字符串实例的指针?也许是nullptr?名称中的复数看起来像是要输入多个工厂,但也许我们会将它们存储在带有一些分隔符的单个字符串中?谁知道? Use [] when it's an array.

    void enterFactoryNames(string[] nameBuffer)

    另外,当您打印结果时,也可以在此处使用 []。

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

    一致的命名

    FacNames 外,您的所有变量都是camelCase。此外,FacNames 是单个工厂名称时的复数形式,并且它是您出于某种原因决定缩短的唯一变量。为了一致性和可读性,我将它重命名为name 或者factoryName。这也让您有机会将 nameBuffer 更改为更具描述性的内容,例如 factoryNames(复数,因为它是一个数组)。

    当索引到数组时,最好从 0 开始循环计数器

    这种情况更为常见,因此阅读您的代码的其他人在循环内编辑代码时不必三思而后行或犯错误。它还将可能的错误添加问题转移到不危险且比数组本身更明显的 UI。

    在使用通用名称时也更喜欢使用“i”。

    所以此时我们有:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多