Program Error and I am unable to identify where?

closed account (Sh59216C)

int main()
{
get_questions();
play();
system ("pause");
return 0;
}

//***************************************************************
// USER-DEFINED FUNCTIONS' DEFINITIONS
//****************************************************************

void get_questions()
{
ifstream inputQuestions;
inputQuestions.open ("question_bank.txt");
int i;

for (i=0; i<1; i++)
{
getline(inputQuestions, question[0]);
inputQuestions >> correctAnswer[0];
}
inputQuestions.close();
}

void play ()
{
int i;
char ans;
for (i=0; i<1; i++)
{
cout << question[i] << correctAnswer[i];
cout << "Please enter the best answer available." << endl;
cin >> ans;
if (ans = 'a','b','c','d')
toupper(ans);
if (ans == correctAnswer[i])
cout << "You are correct." << endl;
else
cout << "Wrong answer." << endl;
}
}


Even though my answer is correct, it still comes up with "Wrong answer." output. I have used cout << question[i] << correctAnswer [i] to check the input is correct.
In future please use Code tags around your code to make it easier to read.

Your problem is at if (ans = 'a','b','c','d')

The first problem is that = is used for directly assigning values, == is to compare 2 values. In this case you need to use == or else ans is always going to be equal to a.

The second problem is you can't compare them all at the same time. You need to do
if (ans == 'a' || ans == 'b' || ans == 'c') etc
Last edited on
closed account (Sh59216C)
Thank you very much for your help
Topic archived. No new replies allowed.