#include<iostream> #include<string> #include<cstring> #include<stdio.h> using namespace std; class Stu{ private: string name; int age; string id; int s[4]; public: Stu(string &na,int ag,string &i,int *sa) { name=na; age=ag; id=i; for(int i=0;i<4;i++) s[i]=sa[i]; } int avg() { int sum; for(int i=0;i<4;i++) sum+=s[i]; return sum/4; } void print() { cout<<name<<","<<age<<","<<id<<","<<avg()<<endl; } }; int main() { string name; int age=0; string id; int sc[4]; getline(cin,name,','); scanf("%d,",&age); getline(cin,id,','); for(int i=0;i<4;i++) { scanf("%d,",&sc[i]); } // cout<<name<<","<<age<<endl; Stu sa( name,age,id,sc); sa.print(); }
具体用法查查吧~~~
cin.getline还不会用
不过c语言里的getline,get函数还是可以的~~