简体   繁体   中英

How to debug “ISO C++ forbids comparison between pointer and integer”

I haven't learned about pointers yet so I don't know how to solve the issue entirely. I'm a beginner C++ student seeking any constructive feedback.

I have no idea how to solve my initializers error. I don't know how to work around the comparison between pointer and integer [-fpermissive]; . I'm at a complete loss, should I just turn my return types back all to integers? I don't think my array elements will work then? and for error 29:46, I have no idea what I'm missing or where I need to place a ";".

#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;
}

Down below are the errors I got.

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"){
                               ^~~

You have two problems - first, in all of those lines marked with errors, where you have " you actually want ' . The double quotes create string literals, whereas what you actually want are character literals.

Second, the comma operator doesn't work the way you seem to think it does. You need, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM