0

我检查了我所有的字符串和数组。在我看来没有什么不对。我的包含语句可能是问题所在。也许我错过了一个。我是一个新手程序员。所以我不知道为什么这段代码不起作用。它看起来不错,应该绝对有效。

#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`
4

1 回答 1

3

这可能是罪魁祸首:

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

你确定i*2不是 >=line.size()吗?

顺便说一句,这段代码看起来很狡猾:

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

也许您想对switch自身使用和初始化响应..为什么?

编辑:你的while(myfile.good()) 那可能会执行 100 次并且你一直在增加x 在你的代码中很难说......你有很多“狡猾”的行:P

于 2012-10-14T20:15:34.737 回答