【C++简单案例解决】1.统计学生成绩

grade.h

#ifndef GRADE_H
#define GRADE_H
#include<string>
#include<vector>
using namespace std;
class grade
{
   public:
      struct student
      {
         string name;
         int score;
      };
      void menu();
      void init();
      void inputScoreAndName();
      void display();
   private:
      vector<struct student> vt;           //struct student
};    
#endif                             //不要忘记

grade.cpp

#include"grade.h"
#include<iostream>
using namespace std;
void grade::init()
{
}

void grade::menu()
{
   int t;
   cout<<"input 1--4 to select program"<<endl;
   cin>>t;
   switch(t)                           // 有括号
   {
   case 1:
      cout<<"input Name and Score"<<endl;
      inputScoreAndName();
      break;
   case 2:
      cout<<"display Name and Score"<<endl;
      display();
      break;
   case 3:
      cout<<"calculate the total Score"<<endl;
      break;
   case 4:
      cout<<"exit System"<<endl;
      break;
   default:
      break;
   }
}

void grade::inputScoreAndName()
{
   string name;
   struct student st;
   int score;
   while(cin)
   {
   cout<<"input name"<<endl;
   cin>>st.name;
   if(!cin)
   return;                          //cin 作为循环判断的条件。注意他的位置是在第一个cin>>之后。
   cout<<"1"<<endl;
   cout<<"input score"<<endl;
   cin>>st.score;
   cout<<"2"<<endl;
   vt.push_back(st);
   }

}

main.cpp

#include<vector>
#include<iostream>
#include<string>
using namespace std;
int main()
{
   grade grd;
  while(1)                    //一直循环 grd.menu(); };

 

转载于:https://www.cnblogs.com/propheteia/archive/2012/08/10/2632768.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值