我无法将我的班级称为主要功能

时间:2014-07-11 02:47:49

标签: c++ function class object main

#include<iostream>  
using namespace std;

class student 
   {

        private:
            char name [30], location[50], ic[20];
            int age;

        public:
            void set_data()
            {
                cout<<"Enter name       :";
                cin.getline(name, 30);
                cout<<"Enter location       :";
                cin.getline(location, 50);
                cout<<"Enter IC     :";
                cin.getline(ic, 20);
                cout<<"Enter age        :";
                cin>>age;
            }

            void display()
           {
               cout<<"Name       :"<<name<<endl;
               cout<<"IC         :"<<ic<<endl;
               cout<<"Location   :"<<location<<endl;
               cout<<"Age        :"<<age<<endl;
           }
    }student s1;

   int main()
{   

    cout<<"--------------------------------"<<endl;
    cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
    cout<<"--------------------------------"<<endl;

    student set_data();


    cout<<"--------------------------------"<<endl;
    cout<<"STUDENT INFORMATION"<<endl;
    cout<<"--------------------------------"<<endl;

    student display();

return 0;
}
伙计们请帮助我......我被困在这里..我需要输出就像这样但我有把问题调成主要功能的问题..我该怎么办?

--------------------------------
WELCOME TO A-PLUS TUITION CENTER
--------------------------------
Enter Name     : Usha Vellappan
Enter Location : Melaka
Enter IC       : 750217016680
Enter age      : 30

--------------------------------
STUDENT INFOMATION
--------------------------------
Name      : Usha Vellappan
IC        : 750217016680
Location  : Melaka
Age       : 30


Press any key to continue

2 个答案:

答案 0 :(得分:2)

考虑到这个例子,你做的事情非常错误......

我修复了代码

#include<iostream>  

使用namespace std;

班级学生    {

    private:
        char name [30], location[50], ic[20];
        int age;

    public:
        void set_data()
        {
            cout<<"Enter name       :";
            cin.getline(name, 30);
            cout<<"Enter location       :";
            cin.getline(location, 50);
            cout<<"Enter IC     :";
            cin.getline(ic, 20);
            cout<<"Enter age        :";
            cin>>age;
        }

        void display()
       {
           cout<<"Name       :"<<name<<endl;
           cout<<"IC         :"<<ic<<endl;
           cout<<"Location   :"<<location<<endl;
           cout<<"Age        :"<<age<<endl;
       }
}s1;

int main() {

cout<<"--------------------------------"<<endl;
cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
cout<<"--------------------------------"<<endl;

s1.set_data();


cout<<"--------------------------------"<<endl;
cout<<"STUDENT INFORMATION"<<endl;
cout<<"--------------------------------"<<endl;

s1.display();

返回0; }

但我还是建议你读一些c ++的书..

答案 1 :(得分:0)

我修改了你的代码。它按预期工作。

#include<iostream>  

using namespace std;

class student {

        private:
            char name [30], location[50], ic[20];
            int age;

        public:
            void set_data()
            {
                cout << "Enter name       : ";
                cin.getline(name, 30);
                cout<<  "Enter location   : ";
                cin.getline(location, 50);
                cout<<  "Enter IC         : ";
                cin.getline(ic, 20);
                cout<<  "Enter age        : ";
                cin>>age;

            }

            void display()
           {
               cout<<"Name       : "<<name<<endl;
               cout<<"IC         : "<<ic<<endl;
               cout<<"Location   : "<<location<<endl;
               cout<<"Age        : "<<age<<endl;
           }
};

int main()
{   

    cout<<"--------------------------------"<<endl;
    cout<<"WELCOME TO A-PLUS TUITION CENTER"<<endl;
    cout<<"--------------------------------"<<endl;

    student s;
    s.set_data();


    cout<<"--------------------------------"<<endl;
    cout<<"STUDENT INFORMATION"<<endl;
    cout<<"--------------------------------"<<endl;

    s.display();

    return 0;
}