-6

私はこのコードを持っていますが、これは基本的に私がc ++を学ぼうとしているのですが、なぜ2つのエラーが発生し続けるのか理解できません

エラー:非クラスタイプの「char[0]」である「cInputChar」のメンバー「length」の要求

エラー:「char*」から「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;
}
4

1 に答える 1

2
 char cInputChar[0];

サイズの配列が本当に必要0ですか? 0C++ ではサイズの配列を使用できません。それは単に合法ではありません。

次のようなものが必要です:

#define MAX_SIZE 256

char cInputChar[MAX_SIZE];

std::stringCスタイルの文字配列の代わりに単純に使用する方が良いです.


コメントの議論から:

@Inafune:良い本を手に取ってください。コードをコンパイルするためだけに構文を追加したり削除したりするだけでは、プログラミング言語を学ぶことはできません。コードの背後にある目的を理解せずに、コードを 1 行も書かないでください。

于 2013-01-09T15:22:54.317 に答える