【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(); };