mercredi 13 mai 2020

Please help, I have no idea how to fix my errors

Someone please help me understand the issues I have with my programming. I haven't learned about pointers yet so I don't know how to solve the issue entirely. I'm a beginner c++ student so any constructive feedback is very much welcomed!

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

Aucun commentaire:

Enregistrer un commentaire