Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //including libraries
- #include <iomanip>
- #include <fstream>
- #include <iostream>
- using namespace std;
- void function_inputData();
- int main(){
- int num_students;
- const double GRADE_A = 90.0;
- const double GRADE_B = 80.0;
- const double GRADE_C = 70.0;
- const double GRADE_D = 60.0;
- const int STUDENT_COUNT = 3;
- const int STRING_COUNT = 9;
- const int INT_COUNT = 2;
- const int COURSE_COUNT = 3;
- const int GRADE_COUNT = 6;
- const string WARN = "Warning: Your grade is low and needs improvment";
- const string COMMENDATION = "Commendation: Congratulations, Your grade is excellent!";
- const double WARN_NUM = 70.0;
- const double COMMENDATION_NUM = 95.0;
- const string FILE_ERROR = "Error: File is not reading.";
- const string INV_NUM = "Error: invalid number, an integer must be between 1 and 100";
- const string INV_FLOAT = "Error: invalid input, floating point must be between 1.0 and 100.0";
- const string INV_STRING = "Error: string must be longer than 0 spaces";
- const string ARRAY_ERROR = "Error: Array out of bounds";
- // Get input for number of students in class
- do {
- cout << "Please input the number of students in the class " << endl;
- cin >> num_students;
- if (num_students < STUDENT_COUNT)
- cout << "Number of students must be greater than 3" << endl;
- } while (num_students < STUDENT_COUNT);
- //Declare files
- ifstream file_input;
- ofstream file_output;
- //open input file
- file_input.open("Project4_David_Janicki_Input.txt");
- if (file_input.fail()){
- cout << FILE_ERROR << endl;
- return 0;
- }
- //create student arrays
- string nonNumeric [num_students][STRING_COUNT];
- int numeric1 [num_students][INT_COUNT];
- double numeric2 [STUDENT_COUNT][COURSE_COUNT][GRADE_COUNT];
- char letterGrade[STUDENT_COUNT][COURSE_COUNT];
- // outer loop for students
- for (int i=0; i<num_students; i++){
- //check for error
- if (i < STUDENT_COUNT) {
- cout << ARRAY_ERROR << endl;
- // break;
- }
- // getting input from file and saving in arrays
- //student name
- getline(file_input, nonNumeric[i][0]);
- if (nonNumeric[i][0].length() <= 0){
- cout << INV_STRING << endl;
- continue;
- }
- // student id
- getline(file_input, nonNumeric[i][1]);
- if (nonNumeric[i][1].length() <=0){
- cout << INV_STRING << endl;
- continue;
- }
- //student address
- getline(file_input, nonNumeric[i][2]);
- if (nonNumeric[i][2].length() <=0){
- cout << INV_STRING << endl;
- continue;
- }
- //student phone number
- getline(file_input, nonNumeric[i][3]);
- if (nonNumeric[i][3].length() <=0){
- cout << INV_STRING << endl;
- continue;
- }
- //student social
- getline(file_input, nonNumeric[i][4]);
- if (nonNumeric[i][4].length() <=0){
- cout << INV_STRING << endl;
- continue;
- }
- //student age
- file_input >> numeric1[i][0];
- file_input.ignore();
- if (numeric1[i][0] < 1 || numeric1[i][0] > 100){
- cout << INV_NUM << endl;
- // break;
- }
- // years at txstate
- file_input >> numeric1[i][1];
- file_input.ignore();
- if (numeric1[i][1] < 1 || numeric1[i][1] > 100){
- cout << INV_NUM << endl;
- // break;
- }
- //class loop
- for (int n=0; n < COURSE_COUNT; n++){
- //check for error
- if (5+n > STRING_COUNT){
- cout << ARRAY_ERROR << endl;
- //break;
- }
- //course number
- getline(file_input, nonNumeric[i][5+n]);
- if (nonNumeric[i][5+n].length() <= 0){
- cout << INV_STRING << endl;
- continue;
- }
- numeric2[i][n][5] = 0.0;
- for (int x=0; x<5; x++){
- //check for error
- if (x > GRADE_COUNT){
- cout << ARRAY_ERROR << endl;
- // break;
- }
- //exam score
- file_input >> numeric2[i][n][x];
- file_input.ignore();
- if (numeric2[i][n][x] <1.0 || numeric2[i][n][x] > 100.0){
- cout << INV_FLOAT << endl;
- // break;
- }
- //compute grade #
- switch (x) {
- case 0:
- numeric2[i][n][5] += numeric2[i][n][x] * .1;
- break;
- case 1:
- case 2:
- numeric2[i][n][5] += numeric2[i][n][x] * .15;
- break;
- case 3:
- numeric2[i][n][5] += numeric2[i][n][x] * .2;
- break;
- case 4:
- numeric2[i][n][5] += numeric2[i][n][x] * .4;
- break;
- }
- }
- if (numeric2[i][n][5] >= GRADE_A){
- letterGrade[i][n] = 'A';
- }else if (numeric2[i][n][5] >= GRADE_B) {
- letterGrade[i][n] = 'B';
- }else if (numeric2[i][n][5] >= GRADE_C) {
- letterGrade[i][n] = 'C';
- }else if (numeric2[i][n][5] >= GRADE_D) {
- letterGrade[i][n] = 'C';
- } else {
- letterGrade[i][n] = 'F';}
- }
- }
- //close input file
- file_input.close();
- //open output file
- file_output.open("Project4_David_Janicki_Output.txt");
- // student loop
- for (int i=0; i<num_students; i++){
- //check for error
- if (i < STUDENT_COUNT) {
- cout << ARRAY_ERROR << endl;
- // break;
- }
- //write information to file
- file_output << "============================================================================================" << endl;
- file_output << setw(46) << "Student Grade Sheet" << endl;
- file_output << "============================================================================================" << endl;
- file_output << setw(33) << "Name of Student: \t" << nonNumeric[i][0] << endl;
- file_output << setw(33) << "Student ID: \t" << nonNumeric[i][1] << endl;
- file_output << setw(33) << "Student Address: \t" << nonNumeric[i][2] << endl;
- file_output << setw(33) << "Student Telephone Number: \t" << nonNumeric[i][3] << endl;
- file_output << setw(33) << "Student Social Security # \t" << nonNumeric[i][4] << endl;
- file_output << setw(33) << "Student Age: \t" << numeric1[i][0] << endl;
- file_output << setw(33) << "number of years at Texas State: \t" << numeric1[i][1] << endl;
- //course loop
- for (int n=0; n < COURSE_COUNT; n++){
- //check for error
- if (5+n > STRING_COUNT){
- cout << ARRAY_ERROR << endl;
- // break;
- }
- //write course # to file
- file_output << endl << setw(33) << "Course Number \t" << nonNumeric[i][5+n] << endl;
- file_output << "--------------------------------------------------------------------------------------------" << endl;
- //exam loop
- for (int x=0; x<5; x++){
- //check for error
- if (x > GRADE_COUNT){
- cout << ARRAY_ERROR << endl;
- // break;
- }
- file_output << setw(33) << "Exam #" << x+1 << ": \t" << fixed << showpoint <<
- setprecision(2) << numeric2[i][n][x] << endl;
- }
- //write grades to file
- file_output << setw(28) << "Numerical Grade in " << nonNumeric[i][5+n] <<
- ": \t" << fixed << showpoint << setprecision(2) << numeric2[i][n][5] << endl;
- file_output << setw(28) << "Letter Grade in " << nonNumeric[i][5+n] <<
- ": \t" << letterGrade[i][n] << endl;
- //write warning or commendation
- if (numeric2[i][n][5] < WARN_NUM)
- file_output << setw(37) << "Comment: \t" << WARN << endl;
- else if (numeric2[i][n][5] >= COMMENDATION_NUM)
- file_output << setw(37) << "Comment: \t" << COMMENDATION << endl;
- }
- //file_output << endl << endl;
- }
- //close file
- file_output.close();
- cout << "Data written to file" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement