I have this code, which is basically me trying to learn c++, and I can't figure out why I keep getting the two errors
error: request for member 'length' in 'cInputChar', which is of non-class type 'char [0]'
and
error: invalid conversion from 'char*' to 'char'
I think it has something to do with the way I am declaring the char variable cInputChar
. The problem is definitely associated with the getChar
function.
My code is below:
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;
}