函式.cpp
void enterFactoryNames(string* )
{
string FacNames;
for(int i = 1; i <= SIZE; i )
{
cout << "Name of Factory "<< i << " : ";
getline(cin, FacNames);
cout << endl;
}
主程式
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).
uj5u.com熱心網友回復:
您應該將用戶輸入存盤在陣列中。像這樣:
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;
}
}
uj5u.com熱心網友回復:
解決方案
您實際上并沒有在任何地方保存他們輸入的結果。
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 所說的塊作用域。
回圈的每次迭代,您呼叫getline并保存結果以FacNames覆寫它之前的任何值。
在定義它的塊的末尾,變數被銷毀并不再存在。
要解決此問題,您需要首先為傳入的引數nameBuffer命名,例如(當前未命名,因此無法訪問),如下所示:
void enterFactoryNames(string* nameBuffer)
這允許使用名稱nameBuffer來參考函式范圍內的引數。
接下來,您需要在將FacNames新的 in 讀入正確的索引 in 后,通過分配 in 的值來實際使用它nameBuffer。
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?名稱中的復數形式似乎會輸入多個工廠,但也許我們會將它們存盤在帶有分隔符的單個字串中?誰知道?當它是一個陣列時使用 []。
void enterFactoryNames(string[] nameBuffer)
此外,當您列印結果時,也在那里使用 []。
cout << "\n\nFACTORY: " << factoryNames[x] << endl;
一致命名
所有的變數都駝峰除外FacNames。此外FacNames,當它是單個工廠名稱時是復數,并且它是您出于某種原因決定縮短的唯一變數。出于一致性和可讀性我其重命名為name或可能factoryName。這也將使您有機會更改nameBuffer為更具描述性的內容,例如factoryNames(復數,因為它是一個陣列)。
索引陣列時,更喜歡在 0 處啟動回圈計數器
這更常見,因此在回圈中編輯代碼時,其他閱讀您代碼的人不必三思而后行或犯錯誤。它還將錯誤添加到 UI 的可能問題移到了 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://www.uj5u.com/yidong/371817.html
上一篇:我想制作一個包含字串矩陣的結構
下一篇:沒有值的索引的參考位置
