1. ホーム
  2. c++

[解決済み] "ISO C++はポインタと整数の比較を禁止しています "のデバッグ方法

2022-01-30 23:11:49

質問

まだポインターについて勉強していないので、完全に解決する方法がわかりません。私はC++の初心者の学生で、建設的なフィードバックを求めています。

initializersのエラーを解決する方法が全くわかりません。ポインタと整数の比較をどうすればいいのかわかりません。 [-fpermissive]; . 完全に途方に暮れています。戻り値の型をすべて整数に戻せばいいのでしょうか?そして29:46のエラーは、何が足りないのか、どこに";"を入れればいいのか、さっぱりわかりません。

#include <iostream>
using namespace std;

int main() {
  // Correct Answers for exam
  const int numberOfAnswers = 10;
  char correctAnswers[numberOfAnswers] = {"A", "D", "B", "B", "C",
                                          "B", "A", "B", "C", "D"};

  // Variables
  int answersRight, answersWrong, passingScore, failingScore;
  answersRight = 0;
  answersWrong = 0;
  passingScore = 8;
  failingScore = 7;
  char userInput;

  // loop for exam questions
  for (int counter = 1; counter <= 10; ++counter) {
    cout << "Enter your answer to question" << counter << "." << endl;
    cin >> userInput;
    // Comparing correct answers and tracking right/wrong answers.
    if (userInput == "A", "B", "C", "D") {
      if (userInput == correctAnswers[counter]) {
        answersRight++;
      }
    } else
      (userInput != "A", "B", "C", "D") {
        cout << "Invalid response. You have one more try to answer question "
             << counter << "." << endl;
        cin >> userInput;
      }
    if (userInput == "A", "B", "C", "D") {
      if (userInput = correctAnswers[counter]) {
        answersRight++;
      }
    } else {
      answersWrong++;
    }
  }

  // Display results
  if (answersRight >= passingScore) {
    cout << "Congratulations! You have passed this exam!" << endl;
    cout << "You have answered " << answersRight << " questions right and "
         << answersWrong << " questions wrong." << endl;
  } else {
    cout << "You have failed the exam. Try again to pass!" << endl;
    cout << "You have answered " << answersRight << " questions right and "
         << answersWrong << " questions wrong." << endl;
  }

  return 0;
}

以下は、私が得たエラーです。

main.cpp: In function ‘int main()’:
main.cpp:8:82: error: too many initializers for ‘char [11]’
     char correctAnswers[numberOfAnswers]={"A","D","B","B","C","B","A","B","C","D"};
                                                                                  ^
main.cpp:24:27: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
             if(userInput=="A","B","C","D"){
                           ^~~
main.cpp:29:30: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
             }else(userInput!="A","B","C","D"){
                              ^~~
main.cpp:29:46: error: expected ‘;’ before ‘{’ token
             }else(userInput!="A","B","C","D"){
                                              ^
main.cpp:34:31: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
                 if(userInput=="A","B","C","D"){
                               ^~~

解決方法は?

1つ目は、エラーでマークされた行のすべてで、次のような問題があります。 " というのは、実際には ' . 二重引用符は文字列リテラルを作成しますが、実際に必要なのは文字リテラルです。

第二に、カンマ演算子はあなたが考えているような働きはしません。たとえば、次のようなものが必要だ。

    if (userInput == 'A' ||
        userInput == 'B' ||
        userInput == 'C' ||
        userInput == 'D') {