samedi 2 février 2019

Get data of an array from main function to another function

I have a problem when calling userInput() that it doesn't give the correct arr[num].getCorrectAnswer() from main, that would be 2 (the output is always 50720). I will create more questions so I want to use userInput() for all questions.

#include "Question.h"
#include <iostream>

using namespace std;

void userInput(int);

int main()
{
Question arr[10];
arr[0].setQuestion("Q1- The expression !(!3%2||4%2) is evaluated to:       ");
arr[0].setAnswer1("1. False ");
arr[0].setAnswer2("2. True");
arr[0].setAnswer3("3. Syntax error");
arr[0].setAnswer4("4. 2");
arr[0].setCorrect(2);

cout << arr[0].getQuestion() << endl;
cout << arr[0].getAnswer1() << endl;
cout << arr[0].getAnswer2() << endl;
cout << arr[0].getAnswer3() << endl;
cout << arr[0].getAnswer4() << endl;

userInput(0);

void userInput(int num)
{
Question arr[num];
int userInput;

cout << "Enter your answer: " << endl;
cin >> userInput;

if (userInput != arr[num].getCorrectAnswer())
{
    cout << "Wrong answer!\n";
    cout << "The correct answer is: " << arr[num].getCorrectAnswer() << endl;
}
else
    cout << "Congratulations, your answer is correct!" << endl;
}

There's no problem with .h or .cpp file, so I decided not to put in here so its more organized, but if you want I will.

Aucun commentaire:

Enregistrer un commentaire