the If loop is not recognizing my variable?

Im not even sure how to say this, this is a very simple 'quiz' that i have as assignment, Read the questions from a file, read the answers from a file,and compare the input answers with the file answers, i Read the answers from 'quizzanswers' and i assign them to answer[] array, which you can probably notice that i also do a cout << array[0] << array[1] ect ect, to check if the answers are assigned properly, and yes they are,i mean,it shows so when the code runs, but when im comparing if answer_int[3] = 2(which is the input answer from user) is equal with the answer[3] (which is 2) , it tells me that its wrong, can anyone give me some hints or some explanation to what im missing here? :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
  string q_line,a_line,answer_string;
  string answer_int[3],answer[3];
  int points = 0;


  ifstream myfile ("quizzquestions.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,q_line) )
    {
      cout << q_line << '\n';
    }
    myfile.close();
  }else cout << "Unable to open file";

    ifstream  myfile2 ("quizzanswers.txt");
  if (myfile2.is_open())
  {
    for(int i = 0 ;i < 4 ; i++)
    {
       getline (myfile2,q_line);
       answer[i] = q_line ;
    }
    cout << answer[0] << " // " << answer[1] << " // " << answer[2] << " // " << answer[3] << endl;
    myfile2.close();
  }else cout << "Unable to open file";

  cout << "Enter your Answer for Question 1(5 points): " << endl;
  cin >> answer_int[0];
  cout << "Enter your answer for Question 2(5 points): " << endl;
  cin >> answer_int[1];
  cout << "Enter your answer for Question 3(5 points): " << endl;
  cin >> answer_int[2];
  cout << "Enter your answer for question 4(5 points): " << endl;
  cin >> answer_int[3];

  cout << "Evaluating your final score........" << endl;
  cout << " " << endl;


      if(answer_int[0] == answer[0])
  {
      cout << " You have answered the first question correctly, +5 points for you." << endl;
      points = points + 5;
  }else
  {
      cout << " You have answered the first question incorrectly,no + points for you." << endl;
              do{
            cout << "You can try again to answer the first question correctly" << endl;
            cin >> answer_int[0];
        }while(!(answer_int[0] == answer[0]));
        points = points + 5;
  }
    if(answer_int[1] == answer[1])
    {
        cout << " You have answered the second question correctly, +5 points for you." << endl;
        points = points + 5;
    }else
    {
        cout << " You have answered the second question incorrectly, no + points for you." << endl;
                do{
            cout << " You can try again to answer the second question correctly: " << endl;
            cin >> answer_int[1];
        }while(!(answer_int[1] == answer[1]));
        points = points + 5;
    }
    if(answer_int[2] == answer[2])
    {
        cout << " You have answered the third question correctly, +5 points for you." << endl;
        points = points + 5;
    }else
    {
        cout << " You have answered the third question incorrectly, no + points for you." << endl;
                do{
            cout << "You can try again to answer the third question correctly" << endl;
            cin >> answer_int[2];
        }while(!(answer_int[2] == answer[2]));
        points = points + 5;
    }
    if(answer_int[3] == answer[3])
    {
        cout << " You have answered the fourth question correctly, +5 points for you." << endl;
        points = points + 5;
    }else
    {
        cout << " You have answered the fourth question incorrectly, no + points for you." << endl;
       do{
           cout << "You can try again to answer the fourth question correctly" << endl;
            cin >> answer_int[3];
        }while(!(answer_int[3] == answer[3]));
    }

    cout << " =============================================" << endl;

    cout << " Final Score = " << points << " !!" << endl;
  return 0;
}
Last edited on
At line 9 you define answer and answer_int as arrays with 3 elements, but you use then as though they had 4 (elements 0-3). Just change line 9 to string answer_int[4],answer[4];

It seems to me that your program will always generate the final score since the user is prompted for the answer to each question until they get it right.

Lines 48-99 contain a whole lot of repetition. See if you can create a function to do it and then call the function 4 times. Look at each case and see what's different. Those differences can become the parameters to the function.
Yes sir, the text given to me requested that I have to allow correction of any incorrect answers, thanks so much for the clarification on Line 9, i have yet to understand better how coding works, but im trying :D, as for lines 48-99, i shall definitively look into making a function for that part, because honestly, this was my first try at the assignment and i was just trying to see if i could get a final 'production' properly :) , thanks again!

The final edit before submission.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Global Variables
string answer_int[4],answer[4];
int total_points = 0;

void GetScore()
{
    cout << "Enter your Answer for Question 1(5 points): " << endl;
    cin >> answer_int[0];
    cout << "Enter your answer for Question 2(5 points): " << endl;
    cin >> answer_int[1];
    cout << "Enter your answer for Question 3(5 points): " << endl;
    cin >> answer_int[2];
    cout << "Enter your answer for question 4(5 points): " << endl;
    cin >> answer_int[3];

    cout << "Evaluating your final score........" << endl;
    cout << " " << endl;
}

int CheckAnswers(int a,int b)
{
    string statement_success[4],statement_repeat[4],statement_fail[4];
    statement_success[0] = "You have answered the first question correctly, +5 points for you." ;
    statement_success[1] = "You have answered the second question correctly, +5 points for you." ;
    statement_success[2] = "You have answered the third question correctly, +5 points for you." ;
    statement_success[3] = "You have answered the fourth question correctly, +5 points for you." ;
    statement_repeat[0] = "You can try again to answer the first question correctly: " ;
    statement_repeat[1] = "You can try again to answer the second question correctly: " ;
    statement_repeat[2] = "You can try again to answer the third question correctly: " ;
    statement_repeat[3] = "You can try again to answer the fourth question correctly: " ;
    statement_fail[0] = "You have answered the first question incorrectly,no + points for you." ;
    statement_fail[1] = "You have answered the second question incorrectly,no + points for you." ;
    statement_fail[2] = "You have answered the third question incorrectly,no + points for you." ;
    statement_fail[3] = "You have answered the fourth question incorrectly,no + points for you." ;
    if(answer_int[a] == answer[b] )
  {
      cout << statement_success[a] << endl;
      total_points = total_points + 5;
  }else
  {
      cout << statement_fail[a] << endl;
              do{
            cout << statement_repeat[a] << endl;
            cin >> answer_int[a];
        }while(!(answer_int[a] == answer[b]));
        total_points = total_points + 5;
  }


}

int main () {
  string q_line,a_line;

  ifstream myfile ("quizzquestions.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,q_line) )
    {
      cout << q_line << '\n';
    }
    myfile.close();
  }else cout << "Unable to open file";

    ifstream  myfile2 ("quizzanswers.txt");
  if (myfile2.is_open())
  {
    for(int i = 0 ;i < 4 ; i++)
    {
       getline (myfile2,q_line);
       answer[i] = q_line ;
    }
    //Printing out the answers for testing purposes.
    cout << answer[0] << " // " << answer[1] << " // " << answer[2] << " // " << answer[3] << endl;
    myfile2.close();
  }else cout << "Unable to open file";

  GetScore();
  CheckAnswers(0,0);
  CheckAnswers(1,1);
  CheckAnswers(2,2);
  CheckAnswers(3,3);

    cout << " ==================================" << endl;
    cout << " Final Score = " << total_points << " !!" << endl;
    cout << " ==================================" << endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.