基于用户输入的C++对象动态初始化

基于用户输入的C++对象动态初始化,c++,database,class,object,C++,Database,Class,Object,嘿,抱歉刚才的问题 好的,我的项目是用c语言为一所大学创建并运行一个数据库++ 我必须使用USN(一个唯一的学生编号)来访问数据库: 因此,我编写了以下程序: #include<iostream> # include<conio.h> #include<iomanip> #include<string.h> #include<stdlib.h> int checkinarray(char[],char*); using namespa

嘿,抱歉刚才的问题

好的,我的项目是用c语言为一所大学创建并运行一个数据库++

我必须使用USN(一个唯一的学生编号)来访问数据库:

因此,我编写了以下程序:

#include<iostream>
# include<conio.h>
#include<iomanip>
#include<string.h>
#include<stdlib.h>

int checkinarray(char[],char*);
using namespace std;

class student
{

private :

    int sem;
    float cgpa;
    char password[11];
    char passwordtrial[11];
    void readdata();
    void checkpassword();
    void createpassword();

public :
    char name[50];
    int roll;
    void printdata();
    char USN[11];
    static int number;
    void opendatabase();
    void firsttime();
public:
    student(char n[50]="NONE")
    {
        number++;
        roll=number;
        cout<<endl<<"New record under the name of "<<n<<" has been created !"<<endl;
        cout<<"Roll number set for new student as : "<<roll<<endl;
        cout<<"Use this Roll number for further usage"<<endl<<endl;
    };

};

void student::opendatabase()
{
    int ch;
    cout<<"Enter your name:"<<endl;
    cin>>name;
    cout<<"Enter your password"<<endl;
    cin>>passwordtrial;
    if(!strcmp(passwordtrial,password))
    {
        cout<<"Do you want to read or write";
        cin>>ch;
        switch(ch)
        {
        case 0 :
            readdata();
            break;

        case 1 :
            printdata();
            break;
        }

    }
    else
        cout<<"Try Again";

};
void student::readdata()
{
    cout <<endl<<"Enter the name of the student : ";
    cin >> name;
    cout <<endl<<"Enter the semester of the student : ";
    cin >> sem;
    cout <<endl<<" Enter the cgpa of the student : ";
    cin >> cgpa;
};

void student :: printdata()
{
    cout << "The name is : " << name << endl;
    cout << "The sem is : " << sem << endl;
    cout << "The roll is : " << roll << endl;
    cout << "The cgpa is : " << cgpa << endl;

}
void student::firsttime()
{
    cout<<"Enter your name :";
    cin>>name;
    cout<<"Hey "<<name<<" Welcome to DBMS "<<endl;
    createpassword();
};

void student::createpassword()
{
    cout<<"Please enter your 6 character password.. :  ";
    cin>>password;
    cout<<"Please Input your Data here.... :" ;
    readdata();
};
int student::number=0;
int main()
{
    enum status {existing,newacc,exit};
    enum functi{read,update};
    char entry1[40];
    char entry[10];
    char usn[15];
    char  a[1000][15];
    char n[40];
    int i,j=0,k;
    int s=2;
    cout<<endl<<"WELCOME TO COLLEGE NAME"<<endl<<"Press enter to access Database...";
    getch();
    cout<<endl<<"Welcome to the Main Page of our Database : "<<endl;
    while(1)
    {//User option
        cout<<endl<<"Do you want to access an old entry : "<<endl<<"OR"<<"Create a NEW entry : ";
        cin>>entry1;
        if(!strcmp(entry1,"old"))
            s=existing;
        else if(!strcmp(entry1,"new"))
            s=newacc;
        else
            s=exit;
        switch(s)
        {

            case existing:
                            {
                                i=1;
                                break;
                            }

            case newacc:
                            {   i=1;
                                cout<<endl<<"Enter your usn : "<<endl;
                                cin>>usn;
                                strcpy(a[j],usn);
                                j++;
                                strcpy(n,usn);
                                cout<<n;
                                student usn(n);
                                usn.firsttime();     //Start here!! use i to go to next loop or stay in this loop,,change name entry to usn
                                break;
                            }


            default :{cout<<"Error Input";i=0;break;}
        }

         if(i)
            continue;

            cout<<endl<<"What do u want to do??"<<endl<<"Read Entries "<<endl<<"Update entries";
            cin>>entry;
            if(!strcmp(entry,"read"))
                s=read;
            else if(!strcmp(entry,"update"))
                s=update;
            else
                s=exit;
            cout<<endl<<"Enter your usn : "<<endl;
            cin>>usn;
            if(checkinarray(a[15],usn))
            {
               switch(s)
                {

                        case read:{
                                        usn.printdata();
                                        break;
                                  }


                        case update:{
                                        usn.firsttime();
                                        break;
                                   }


                        default :
                                    cout<<"Are you sure you want to exit?"<<endl<<"Press 0 to exit"<<endl<<"to back to menu press 1";
                                    cin>>k;
                                    break;

                        }
                if(!k)
                    break;

            }

            else cout<<"Invalid Roll number try again!";
    }


}

int checkinarray(char a[][15],char b[])
{
    int len;
    //Finding the length of the string :
    len=(sizeof(a)/sizeof(a[0]));
    //Checking Conditions for roll number:
    for(int k=0;k<len;k++)
    {
        if(strcmp(a[k],b))
            return 1;//stringcompare!!

    }
    return 0;
}
好的,当我运行这个程序时,我得到以下错误:

请求“usn”中的成员“printdata”,该成员为非类类型“char[15]” 及

请求“usn”中的成员“firsttime”,该成员为非类类型“char[15]”


因此,请通过建议基于用户输入创建和调用对象的不同方法来帮助我克服此错误。OP的问题可以简化为以下示例:

#include<iostream>
#include<string.h>

using namespace std;

class student
{
public:
    student(char n[50])
    {
    }
};
int main()
{
    char usn[15];
    char n[40];
    {
        cin >> usn;
        strcpy(n, usn);
        student usn(n);
    }
    usn.printdata();
}
是main中的自动变量。它仅在main中可见,并将在main结束时过期

是main中匿名块中的自动变量。它仅在此块中可见,并将在块结束时过期

注释此块以更好地解释正在发生的事情,我们得到

{
    cin >> usn; // uses char usn[15];
    strcpy(n, usn);
    student usn(n); // declares a new variable named usn that replaces char usn[15];for the remainder of this block
} // student usn(n); ends right here and is destroyed.
usn.printdata(); //uses char usn[15]; which has no printdata method.
那么我们如何解决这个问题呢

学生usnn;必须有更广泛的范围。 这两个变量中的一个必须更改名称,因为一旦学生usnn;范围更广,会与char usn发生冲突[15]; 让我们试一试

int main()
{
    char usn[15];
    char n[40];
    student stud(n);
    {
        cin >> usn;
        strcpy(n, usn);
    }
    stud.printdata();
}
这是不可能的,因为n中没有数据可以用来制作stud。至少在这个最小的例子中不是这样

int main()
{
    char usn[15];
    char n[40];
    student * stud;
    {
        cin >> usn;
        strcpy(n, usn);
        stud = new student(n);
    }
    stud->printdata();
    delete stud;
}
通过动态分配螺柱来解决此问题,使其不再受大括号的限制。不幸的是,这确实带来了一些额外的内存管理问题。必须删除螺柱。添加了一个delete,但是如果printdata抛出异常并删除stud怎么办;跳过了吗

int main()
{
    char usn[15];
    char n[40];
    std::unique_ptr<student> stud;
    {
        cin >> usn;
        strcpy(n, usn);
        stud.reset(new student(n));
    }
    stud->printdata();
}

你能更好地解释你想要达到的目标吗?以及switch中的s。请将您的代码缩减为。这一点上的所有答案都是基于猜测。我对前面的问题感到抱歉…第一次为什么对学生对象和字符数组使用相同的标识符?尝试使用:学生新闻学生;然后是新闻学生。第一次@VijayKalmath另外,我希望您使用的是多个头文件和正文文件,否则理解起来会很混乱。
int main()
{
    char usn[15];
    char n[40];
    student * stud;
    {
        cin >> usn;
        strcpy(n, usn);
        stud = new student(n);
    }
    stud->printdata();
    delete stud;
}
int main()
{
    char usn[15];
    char n[40];
    std::unique_ptr<student> stud;
    {
        cin >> usn;
        strcpy(n, usn);
        stud.reset(new student(n));
    }
    stud->printdata();
}
int main()
{
    char usn[15];
    char n[40];
    std::vector<student> studs;
    {
        cin >> usn;
        strcpy(n, usn);
        studs.emplace_back(n); // or studs.push_back(student(n));
    }
    for (student & stud: studs)
    {
        stud.printdata();
    }
}