score.txt 파일을 읽어서 아래의 조건을 만족하면서 합계와 평균을 구하는 프로그램을 작성하세요.
조건 1. student클래스를 정의하세요.
2. 멤버변수는 학번, 이름, 성적
3. 객체배열을 사용하세요.
4. while()문과 을 사용하세요.
5. 설정자, 접근자 등도 정의하세요.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
//2. 멤버변수는 학번, 이름, 성적
string number;
string name;
int score;
public:
//5. 설정자, 접근자 등도 정의
void setStudent(string number, string name, int score)
{
this->number = number;
this->name = name;
this->score = score;
}
string getNumber()
{
return number;
}
string getName()
{
return name;
}
int getScore()
{
return score;
}
};
int main()
{
// 3. 객체배열을 사용하세요.
Student student[100];
int count = 0;//읽어온 데이터 개수
ifstream ifs("score.txt");
while (1)
{
string number;
string name;
int score;
ifs >> number >> name >> score;//파일에서 읽기
if (ifs.fail())
break;
student[count++].setStudent(number, name, score);//객체에 저장
}
ifs.close();
//읽어온 데이터 화면에 출력
for (int i = 0; i < count; i++)
{
cout << student[i].getNumber() << " - "
<< student[i].getName() << " - "
<< student[i].getScore() << endl;
}
return 0;
}
|
cs |
'C++' 카테고리의 다른 글
[C++] C++ ESPRESSO 중간 점검 1-2 (0) | 2022.03.27 |
---|---|
[C++] C++ ESPRESSO 중간 점검 1-1 (0) | 2022.03.26 |
[C++] 도형 클래스 (0) | 2022.03.24 |
[C++] 날짜 클래스 (0) | 2022.03.23 |
[C++] 스마트폰 클래스 (0) | 2022.03.22 |