|
폴더안의 txt파일에서 불러와 출력하는데요..
쓰레기값이 들어갑니다...
ㅠ_ㅠ뭐 잘못된것이 있나요?
<txt파일>
Smith 23 21 20
Carpenter 25 30 20
Stevens 30 25 20
John 15 20 15
-------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Student
{
string name;
int midterm;
int final;
int homework;
int total;
char grade;
};
void score(Student&, const string, const int, const int, const int);
void print(Student[], const int);
int main()
{
int countStd;
ifstream fin;
fin.open("scores.txt");
if(fin==NULL)
cout<<"Error";
fin>>countStd;
Student *myStd = new Student[countStd];
for(int i=0; i<countStd; i++)
{
string name;
int midterm, final, homework;
fin>>name>>midterm>>final>>homework;
score(myStd[i], name, midterm, final, homework);
}
print(myStd, countStd);
delete[] myStd;
return 0;
}
void score(Student& s, const string name, const int s1, const int s2, const int s3)
{
s.name = name;
s.midterm = s1;
s.final = s2;
s.homework = s3;
s.total = s1+s2+s3;
switch((int)s.total / 10)
{
case 10: case 9: s.grade='A';
break;
case 8: s.grade='B';
break;
case 7: s.grade='C';
break;
case 6: s.grade='D';
break;
default:
s.grade='F';
}
}
void print(Student s[],const int count)
{
cout<<"이름\t"<<"\t중간"<<"\t기말"<<"\t과제"<<"\t총점"<<"\t등급\n";
cout<<"****************************************************************************\n";
for(int i=0; i<count; i++)
{
cout<<s[i].name<<"\t"<<s[i].midterm<<"\t"<<s[i].final<<"\t"<<s[i].homework<<"\t"<<s[i].total<<"\t"<<s[i].grade<<endl;
}
}
|