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;
}我的问题是,当我对*(factoryNames+x)再次调用enterFactoryNames函数时,从该函数获得的字符串值将不会出现。
发布于 2021-12-02 03:07:59
您应该将用户输入存储在数组中。如下所示:
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;
}
}发布于 2021-12-02 04:37:45
解决方案
实际上,您并没有将他们输入的结果保存到任何地方。
void enterFactoryNames(string* )
{
string FacNames;
for(int i = 1; i <= SIZE; i++)
{
cout << "Name of Factory "<< i << " : ";
getline(cin, FacNames);
cout << endl;
}
}FacNames是enterFactoryNames函数的局部变量,它只存在于函数体中。特别是在上述代码片段中的第一个{和最后一个}之间。这就是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接受一个指针。我们确定它是一个数组,还是指向string的单个实例的指针?也许是nullptr?名称中的复数使其看起来将输入多个工厂,但也许我们会将它们存储在带有分隔符的单个字符串中?谁知道呢?Use [] when it's an array.
void enterFactoryNames(string[] nameBuffer)
此外,在打印输出结果时,也要在其中使用[]。
cout << "\n\nFACTORY: " << factoryNames[x] << 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;
}https://stackoverflow.com/questions/70193623
复制相似问题