木石_cr 2016-08-23 22:53 采纳率: 100%
浏览 1231
已采纳

【小白一枚,请大神赐教】这个C++代码怎么改,vs2015中只有两个警告但是运行失败

代码如下:

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

#define MaxQuestions 100//最大问题数量
#define MaxLinesPerQuestion 100//每个问题的最多行数
#define MaxAnswersPerQuestion 10//每个问题最多答案个数
#define EndMarker "------"//末尾分割线


struct answerT
{
    string ans;     //答案
    int nextq;      //该答案所对应的下一题的编号
};

struct questionT
{
    int nQ; //问题编号
    string qText[MaxLinesPerQuestion + 1];//问题文本
    answerT answers[MaxAnswersPerQuestion];//当前问题的答案
};

struct courseDB
{
    string title;//标题
    questionT *questions[MaxQuestions + 1];//问题
};
//初始化

//函数声明

courseDB *ReadDataBase(courseDB *);//读入数据
/*
bool ReadOneQuestion(ifstream &infile, courseDB *course);//读取一个问题
void ReadQuestionText(ifstream &infile96, questionT *q);//读取问题文本
void ReadAnswers(ifstream &infile, questionT *q);//读取该问题的答案
void ProcessCourse(courseDB *course);
*/
void AskQuestion(questionT *q);//提问
int  FindAnswer(string ans, questionT *q);//寻找答案

int main()
{
    system("color 2e");  //设置颜色
    courseDB *read = new courseDB();
    *ReadDataBase(read);//读入数据
    AskQuestion(read->questions[1]);
    delete read;
    system("pause");
}

courseDB *ReadDataBase(courseDB *read)
{


    cout << "请输入要读取的文件名:";
    string filename;
    cin >> filename;
    ifstream infile(filename, ios::in);
    if (!infile)
    {
        cerr << "open error!" << endl;
        exit(1);
    }
    string Line[MaxLinesPerQuestion+1][MaxQuestions ];
    getline(infile, Line[0][0]);
    read->title = Line[0][0];
    cout << read->title << endl;//标题
    for (int i = 1; i <= MaxQuestions; i++)
    {
        int j = 1;
        for (j=1; j <= MaxLinesPerQuestion + 1; j++)
        {
            getline(infile, Line[i][j]);
            stringstream ss;
            ss << Line[i][j];
            ss >> read->questions[i]->nQ;
            if (Line[i][j] == EndMarker)
            {
                for (int k = j + 1; k <= MaxAnswersPerQuestion; k++)
                {
                    getline(infile, Line[i][k]);
                    string a, b; 
                    for (unsigned int m = 0; m < sizeof(Line[i][k]) - k; m++)
                    {
                        a[m] = Line[i][k][m];
                        if (Line[i][k][m] == ':')
                        {
                             continue;
                        }
                        b[m] = Line[i][k][m];
                        ss <<b;
                        ss >> read->questions[i]->answers[k-j-1].nextq;
                        read->questions[i]->answers[k-j-1].ans = a;
                    }
                }
                break;
            }
        }

        if (Line[i][j] == "\n\r")continue;//若读入的行为空白行,则跳出本次循环,读入下一题
    }

    return 0;
}

void AskQuestion(questionT *q)
{
    cout << q->qText << endl;
    cout << "Please enter your answer:";
    string  answer;
    cin >> answer;
    FindAnswer(answer, q);

}

int  FindAnswer(string ans, questionT *q)
{
    questionT p;
    for (int i = 0; i < MaxAnswersPerQuestion; i++)
    {
        if (ans == q->answers[i].ans)
        {
            AskQuestion(&p + q->answers[i].nextq);
        }
        else cout << "Enter error, please re-enter:";
        string answer;
        FindAnswer(answer, q);
    }
    return 0;
}

错误列表:
图片说明

ps:有人给出了指出 string Line[MaxLinesPerQuestion+1][MaxQuestions ];太大了建议改成动态内存分配,但经修改测试后仍无效,希望能有人给出可行的具体的解决方案

  • 写回答

8条回答 默认 最新

  • devmiao 2016-08-23 23:28
    关注

    string Line[MaxLinesPerQuestion+1][MaxQuestions ];
    这个太大了,改成动态分配。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog