鱼C论坛

 找回密码
 立即注册
查看: 4151|回复: 3

C++ CStudent std1[i](num,name,score);//此处怎么解决才能编译成功

[复制链接]
发表于 2013-4-12 18:28:53 | 显示全部楼层 |阅读模式
10鱼币
#include<iostream>
#include<string>
using namespace std;

class CStudent
{
private:
        int m_num;
        string m_name;
        double m_score;
public:
        CStudent(int num,string name,double score);
        void show();
        void modify(double score);
       
};

CStudent::CStudent(int num,string name,double score)
{
        m_num=num;
        m_name=name;
        m_score=score;
}

void CStudent::show()
{
        cout<<"ID号:"<<m_num<<endl;
        cout<<"姓名:"<<m_name<<endl;
        cout<<"分数:"<<m_score<<endl;
}

void CStudent::modify(double score)
{
        m_score=score;
}

int main()
{
        int total;
        cout<<"请输入初始化的人数:"<<endl;
        cin>>total;
        for(int i=1;i<=total;i++)
        {
                int num;string name; double score;
                cout<<"请输入ID号:";
                cin>>num;
                cout<<"\n请输入姓名:";
                cin>>name;
                cout<<"\n请输入分数:";
                cin>>score;
                CStudent std1[i](num,name,score);//此处怎么解决才能编译成功
        }
        do
        {
                int jflag;
                cout<<"***********************"<<endl;
                cout<<"*0----------------退出*"<<endl;
                cout<<"*1----------------显示*"<<endl;
                cout<<"*2----------------修改*"<<endl;
                cout<<"***********************"<<endl;
                cout<<"请输入0-2:";
                cin>>jflag;
       
                switch(jflag)
                {
                case 0:
                        cout<<"\n程序退出"<<endl;                break;
                case 1:
                        cout<<"请输入需要显示的id:";
                        int  nflag;
                        cin>>nflag;
                        std1[nflag].show();                                break;
                case 2:
                        int  idflag;double srflag;
                        cout<<"\n请输入需要修改的ID:";
                        cin>>idflag;
                        cout<<"\n请输入需要修改的分数:";
                        cin>>srflag;
                        std1[srflag].modify(srflag);                break;
                default:
                        cout<<"\n输入信息错误,请重新输入:"; break;
        }while(jflag);
                return  0;
}

最佳答案

查看完整内容

这是我在楼主的基础上盖的代码,仅供参考。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-4-12 18:28:54 | 显示全部楼层
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;

  4. class CStudent
  5. {
  6. private:
  7.         int m_num;
  8.         string m_name;
  9.         double m_score;
  10. public:
  11.         CStudent(void);
  12.         void input_student(int num,string name,double score);
  13.         void show(void);
  14.         void modify(double score);

  15. };

  16. CStudent::CStudent(void)
  17. {
  18.     m_num = 0;
  19.     m_name = "\0";
  20.     m_score = 0;
  21. }

  22. void CStudent::input_student(int num,string name,double score)
  23. {
  24.         m_num=num;
  25.         m_name=name;
  26.         m_score=score;
  27. }

  28. void CStudent::show(void)
  29. {
  30.         cout<<"ID号:"<<m_num<<endl;
  31.         cout<<"姓名:"<<m_name<<endl;
  32.         cout<<"分数:"<<m_score<<endl;
  33. }

  34. void CStudent::modify(double score)
  35. {
  36.         m_score=score;
  37. }

  38. int main(void)
  39. {
  40.         int total;
  41.         int jflag;
  42.         CStudent *std = NULL;
  43.         cout<<"请输入初始化的人数:"<<endl;
  44.         cin>>total;
  45.         std = new CStudent [total];
  46.         for(int i=0;i<total;i++)
  47.         {
  48.                 int num;string name; double score;
  49.                 cout<<"请输入ID号:";
  50.                 cin>>num;
  51.                 cout<<"\n请输入姓名:";
  52.                 cin>>name;
  53.                 cout<<"\n请输入分数:";
  54.                 cin>>score;
  55.                // CStudent std1[i](num,name,score);//此处怎么解决才能编译成功
  56.                std[i].input_student(num, name, score);
  57.         }
  58.         do
  59.         {

  60.                 cout<<"***********************"<<endl;
  61.                 cout<<"*0----------------退出*"<<endl;
  62.                 cout<<"*1----------------显示*"<<endl;
  63.                 cout<<"*2----------------修改*"<<endl;
  64.                 cout<<"***********************"<<endl;
  65.                 cout<<"请输入0-2:";
  66.                 cin>>jflag;

  67.                 switch(jflag)
  68.                 {
  69.                 case 0:
  70.                         cout<<"\n程序退出"<<endl;                break;
  71.                 case 1:
  72.                         cout<<"请输入需要显示的id:";
  73.                         int  nflag;
  74.                         cin>>nflag;
  75.                         std[nflag-1].show();       break;
  76.                 case 2:
  77.                         int  idflag;double srflag;
  78.                         cout<<"\n请输入需要修改的ID:";
  79.                         cin>>idflag;
  80.                         cout<<"\n请输入需要修改的分数:";
  81.                         cin>>srflag;
  82.                         std[idflag-1].modify(srflag);   break;
  83.                 default:
  84.                         cout<<"\n输入信息错误,请重新输入:"; break;
  85.             }
  86.         }while(jflag);

  87.     return 0;

  88. }

复制代码
这是我在楼主的基础上盖的代码,仅供参考。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-4-12 21:49:31 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-4-12 23:10:08 | 显示全部楼层
嗯!是哦!谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-3 15:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表