0

I've checked all my strings and arrays. Nothing looks wrong to me. My include statements may be the problem. Perhaps I am missing one. I'm am a novice programmer. So I am clueless as to why this code will not work. It looks right and should absolutely work.

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<string>
using namespace std;

int main()
{
        int i;
        char response;
        char answers[20];
        char uresponse[20];
        int score=0;


        srand(time(0));
        int test_num = rand() % 2+1;
//        cout << test_num << endl;
        if (test_num == 1)
        {
                string line;
                ifstream myfile ("form1.txt");
                getline (myfile,line);
//                cout<< line<<endl;
                for (int i = 0; i <= 19; i++)
                {
                        answers[i] = line[i*2];
//                        cout<<answers[i]<<endl;
                }
                getline (myfile,line);

                  if ( myfile.is_open())
                  {
                          int x = 0;
                        while ( myfile.good() )
                        {
                          char answers[20];
                              string line;
                                getline (myfile,line);
//                                cout<< line << endl;
                                if( line == "")
                                {
                                        cout << "Enter answer: ";
                                        cin >> response;
                                        if(response == 'a', 'A' || response == 'b', 'B' || response == 'c', 'C' || response == 'd', 'D')
                                        {
                                                response = response;
                                        }
                                        else
                                        {
                                                cout << "ERROR. You must enter A, B, C, or D. Please re-enter: ";
                                                cin >> response;
                                        }
                                        response = toupper(response);
                                        uresponse[x] = response;
                                        x++;
                                }
                        }
//                      cout << uresponse <<endl;
//                      system("pause");
                 }
                  else
  {
          myfile.close();
  }
        }
        else
                {
                string line;
                ifstream myfile ("form2.txt");
                getline (myfile,line);
 //               cout<< line<<endl;
                for (int i = 0; i <= 19; i++)
                {
                        answers[i] = line[i*2];
//                        cout<<answers[i]<<endl;
                }
                getline (myfile,line);

                  if ( myfile.is_open())
                  {
                        int x = 0;
                        while ( myfile.good() )
                        {
                          char answers[20];
                              string line;
                                getline (myfile,line);
 //                               cout << line << endl;
                                if( line == "")
                                {
                                        cout << "Enter answer:";
                                        cin >> response;
                                        if(response == 'a', 'A' || response == 'b', 'B' || response == 'c', 'C' || response == 'd', 'D')
                                        {
                                                response = response;
                                        }
                                        else
                                        {
                                                cout << "ERROR:You must enter A, B, C, or D. Please re-enter:";
                                                cin >> response;
                                        }
                                        response = toupper(response);
                                        uresponse[x] = response;
                                        x++;
                                }
                        }
//                      cout<<uresponse<<endl;
//                    system("pause");
                 }
                  else
  {
          myfile.close();
  }
        }

        for (int j=0; j<20; j++)
        {
                if (answers[j] == uresponse[j])
                        score += 1;
        }

        if(score >15)
        {
                cout << "CONGRATULATIONS! You Passed with a score of ";
                cout << score << endl;
                system("pause");
        }
        else
        {
                cout<< "You Failed with a score of ";
                cout << score << endl;
                system("pause");
        }
        system("pause");
        return 0;
        }`enter code here`
nneonneo
  • 171,345
  • 36
  • 312
  • 383

1 Answers1

3

This could be the culprit:

answers[i] = line[i*2];

Are you certain i*2 is not >= line.size()?

Btw, this code looks dodgy:

if(response == 'a', 'A' || response == 'b', 'B'
 || response == 'c', 'C' || response == 'd', 'D')
{
     response = response   
}

Perhaps you want to use a switch and initializing response to itself.. why?

EDIT: And your while(myfile.good()) That probably executes 100 times and you keep incrementing x It's hard to say in your code.. you have a lot of "dodgy" lines :P

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • I understand how this would pose a problem. Thank you. I corrected this and several other errors(which have nothing to do with the out of range error). But still, "subscript out of range" every time. Also I am using Visual Studio 2012. My lab partner uses a different version, an earlier one. We developed the code in his version. Could this have something to do with it? – user1745480 Oct 14 '12 at 20:27
  • @user1745480 See my updated answer. Looks like debugging is in order. I kindly suggest you get a debugger and learn to use it :) – Lews Therin Oct 14 '12 at 20:31
  • Thanks. But i warned you I was novice. Anyway what if I added the statement 'x < 127' on line 60 and 'x < 118' on line 108. These numbers are equivalent to the number of lines in each text file I am opening. – user1745480 Oct 14 '12 at 20:41
  • But x would still exceed the size of elements you have for response. To make your life easy use a resizable array. Or a vector. Then you don't have to worry about bounds :) – Lews Therin Oct 14 '12 at 20:44