如何使用C ++创建测验数据库

时间:2016-12-18 17:24:59

标签: c++

/ 这只是构成数据库的代码的一部分。将在同一主要功能中生成对问题的随机访问。这段代码有什么问题?请看一看并提供帮助 /

#include<iostream>
#include<cstring>

class ques{
void in_data(char qu[500],char p[25],char q[25],char r[25],char s[25],char      ans1)
        {
            std::strcpy(question,qu);
            std::strcpy(a,p);
            std::strcpy(b,q);
            std::strcpy(c,r);
            std::strcpy(d,s);
            ans=ans1;

        }

};

int main()
{

    ques q[2];
    q[0].in_data("what is 2+2","alpha","beta","gamma","delta","d");
    q[1].in_data("choose a","a","b","c","d","a");

return 0;
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在许多错误:

1- make in_data() public可以从外部调用它,就像在你的main中一样,否则你得到编译时错误accessing private data记住一个类的成员默认为private而struct是public < / p>

2-声明成员数据:问题,a,b,......您正在使用它们而不声明它们。

3-你声明in_data将一个字符作为第六个参数,同时在main中传递一个const字符串:

q[0].in_data("what is 2+2","alpha","beta","gamma","delta","d"); // "d" is a constant character string not just a single character so change it to 'd'
q[1].in_data("choose a","a","b","c","d","a"); // look at in_data how was defined.

您的代码将如下所示:

#include<iostream>
#include <string>
using namespace std;

class ques
{
    public: // make in_data public to use from outside
        void in_data(char qu[500], char p[25], char q[25], char r[25], char s[25], char ans1) // so pass eg: 'a' not "a"
        {
            strcpy(question, qu);
            strcpy(a, p);
            strcpy(b, q);
            strcpy(c, r);
            strcpy(d, s);
            ans = ans1;
        }
    private:
        char question[500];
        char a[25];
        char b[25];
        char c[25];
        char d[25];
        char ans;
};

int main()
{

    ques q[2];
    q[0].in_data("what is 2+2","alpha","beta","gamma","delta",'d');
    q[1].in_data("choose a","a","b","c","d",'a');

    return 0;
}
  • 最后,为什么不使用class string,只要它是代码中必须的而不是使用字符数组?