我有這樣的代碼,這基本上是我努力學習C++,我想不通爲什麼我不斷收到兩個錯誤一些指導,請
error: request for member 'length' in 'cInputChar', which is of non-class type 'char [0]'
和
error: invalid conversion from 'char*' to 'char'
我認爲這與我聲明char變量cInputChar
的方式有關。問題肯定與getChar
函數有關。
我的代碼如下:
int getInteger(int& nSeries);
char getChar(char& cSeriesDecision, int nSeries);
int main()
{
int nSeries = 0;
char cSeriesDecision = {0};
getInteger(nSeries);
getChar(cSeriesDecision, nSeries);
return 0;
}
//The function below attempts to get an integer variable without using the '>>' operator.
int getInteger(int& nSeries)
{
//The code below converts the entry from a string to an integer value.
string sEntry;
stringstream ssEntryStream;
while (true)
{
cout << "Please enter a valid series number: ";
getline(cin, sEntry);
stringstream ssEntryStream(sEntry);
//This ensures that the input string can be converted to a number, and that the series number is between 1 and 3.
if(ssEntryStream >> nSeries && nSeries < 4 && nSeries > 0)
{
break;
}
cout << "Invalid series number, please try again." << endl;
}
return nSeries;
}
//This function tries to get a char from the user without using the '>>' operator.
char getChar(char& cSeriesDecision, int nSeries)
{
char cInputChar[0];
while (true)
{
cout << "You entered series number " << nSeries << "/nIs this correct? y/n: ";
cin.getline(cInputChar, 1);
if (cInputChar.length() == 1)
{
cSeriesDecision = cInputChar;
break;
}
cout << "/nPlease enter a valid decision./n";
}
return cSeriesDecision;
}
既然你是自學,試着和_know_,而不是_think_是怎麼回事,即使你不知道如何修復孤立的元素,你仍然可以隔離它...例如,將您的代碼最小化到重現錯誤所需的最低限度! –
請修復您的問題標題,以便描述編程問題。 –
你創建了一個大小爲'0'的char數組,然後你試着把'getline'放進去,然後你試着對它做'.length()'。這些事情都不可能。你正在使用哪本書?哪個標準庫參考? –