Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <conio.h>
- #include <string>
- using namespace std;
- struct studentType
- {
- string studentFname;
- string studentLname;
- int testScore;
- char grade;
- } student[20];
- void OpenFile ()
- {
- studentType StudentList[20];
- ifstream structFile; //opens struct file
- structFile.open("struct.txt");
- if (structFile.is_open())
- {
- cout << "Student File Opened! ";
- }
- else cout << "Unable to open struct file. The location may have changed or the file may be damaged.";
- }
- void assign()
- {
- string studentInfo;
- ifstream file("struct.txt"); //assigns grades to students
- if (file.is_open())
- {
- for (int i = 0; i < 20; i++)
- {
- getline(file, studentInfo, ' ');
- student[i].studentFname = studentInfo;
- getline(file, studentInfo, ',');
- student[i].studentLname = studentInfo;
- getline(file, studentInfo, '\n');
- student[i].testScore = stoi(studentInfo);
- }
- }
- else cout << "Unable to open struct file. The location may have changed or the file may be damaged.";
- cout << "Grades Assigned! "; //displays only if grades are assigned to students
- }
- void grade()
- {
- for (int i = 0; i < 20; i++) //assignes letter grades to number
- {
- if (student[i].testScore < 60)
- {
- student[i].grade = 'F'; //bad grade
- }
- else if (student[i].testScore < 70)
- {
- student[i].grade = 'D';
- }
- else if (student[i].testScore < 80)
- {
- student[i].grade = 'C';
- }
- else if (student[i].testScore < 90)
- {
- student[i].grade = 'B';
- }
- else if (student[i].grade = 'A');
- }
- cout << "Letters assigned!\n\n"; //displays only if letters are assigned to grade
- }
- void DisplayStudents()
- {
- cout << "Last Name, First Name\t Grade" << "\n";
- cout << "\nStudents: \n";
- int highest = 0;
- int spaces;
- for (int i = 0; i < 20; i++)
- {
- if (student[i].studentFname.size() + student[i].studentLname.size() > highest)
- {
- highest = student[i].studentFname.size() + student[i].studentLname.size();
- }
- }
- for (int i = 0; i < 20; i++)
- {
- spaces = (highest - (student[i].studentFname.size() + student[i].studentLname.size()) + 2);
- cout << "\n" << student[i].studentLname << ", " << student[i].studentFname;
- for (int i = 0; i < spaces; i++)
- {
- cout << " ";
- }
- cout << student[i].grade << " - " << student[i].testScore;
- }
- cout << "\n\n";
- }
- int main() //man function
- {
- OpenFile();
- assign();
- grade();
- DisplayStudents();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement