Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This project deals with Structures and arrays. I am having problems with my code

ID: 3826606 • Letter: T

Question

This project deals with Structures and arrays. I am having problems with my code, it does not want to print out my course numbers and test grades. The code it self works put the output file doesn't print them i belive it has something to do with the getlines for the courses and test.

this is my input:

Jill Smith
A8049558
601 University Dr, San Marcos, TX 78666
(643)-366-346
342-34-3453
21
3
CS1319
90.1
92.3
86.4
87.6
94.1
CS1428
80.1
98.6
80.9
95.3
100.0
CS2308
37.0
52.3
45.4
57.5
52.5
Steve Left
A9845788
500 spring Dr, San Marcos, TX 78666
(210)-765-5686
765-35-2345
22
4
CS1319
97.1
87.3
78.4
89.6
89.7
CS1428
99.1
89.2
89.3
98.4
100.0
CS2308
100.0
98.9
78.6
78.6
98.5
Matt Mills
A3548754
609 Marcos Dr, San Marcos, TX 78666
(210)-264-7547
898-78-3422
18
1
CS1319
78.9
69.8
85.8
58.8
58.8
CS1428
56.9
99.9
87.7
97.7
89.9
CS2308
99.9
99.9
99.9
100.0
99.9

My Code

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//defining structure with having arrays
struct StudentData {
//Define Arrays
string nonNumeric[6][9];
int numeric[6][2];
double numeric2[6][3][6];
char letterGrade[6][3];
} data;


int students;
//Define Floating point variables.
const double A_Score = 90;
const double B_Score = 80;
const double C_Score = 70;
const double D_Score = 60;
//open fins.
ifstream fin;
ofstream fout;
int inputData();
void outputData();
void validateData(string);
void validateData(int);
void letGrade(StudentData Info);
void writetofin(StudentData Info);
void letGrade(StudentData Info,int j)
//loop for courses
{

for(int k = 0; k < 3; k++)
{
fin.ignore();
getline(fin, Info.nonNumeric[j][6+k]);
if(Info.nonNumeric[j][6+k].length() > 11)
{
fout << "Course number was too long, skipping to next class!" << endl;
continue;
}
Info.numeric2[j][k][5] = 0;
double percent = .1;
//loop five tests
for(int l = 0; l < 5; l++)
{
fin >> Info.numeric2[j][k][l];
if(Info.numeric2[j][k][l] < 1.00 || Info.numeric2[j][k][l] > 100.00)
{
fout << "Test score was not in valid range" << endl;
break;
}

//calculate grade
Info.numeric2[j][k][5] = Info.numeric2[j][k][5] + (Info.numeric2[j][k][l] * percent);
percent += .05;

}
if(Info.numeric2[j][k][5] >= A_Score)
{
Info.letterGrade[j][k] = 'A';
}
else if(Info.numeric2[j][k][5] >= B_Score)
{
Info.letterGrade[j][k] = 'B';
}
else if(Info.numeric2[j][k][5] >= C_Score)
{
Info.letterGrade[j][k] = 'C';
}
else if(Info.numeric2[j][k][5] >= D_Score)
{
Info.letterGrade[j][k] = 'D';
}
else
{
Info.letterGrade[j][k] = 'F';
}
}
}


void validateData(string nonNumeric[6][9],int j)
{
nonNumeric[j][0] = "Students Grade Sheet, Department of Computer Science, Texas State University";
getline(fin, nonNumeric[j][1]);
//checking whether the students name is within a reasonable range.
if(nonNumeric[j][1].length() > 256)
{
fout << "Student Name was too long" << endl;
}

getline(fin, nonNumeric[j][2]);
//checking whether the students address is in reasonable range.
if(nonNumeric[j][2].length() > 10)
{
fout << "Student ID was not in the valid range" << endl;
}
//fout << nonNumeric[j][2]<<endl;
getline(fin, nonNumeric[j][3]);
if(nonNumeric[j][3].length() > 256)
{
fout << "Student address was too long," << endl;
}
//fout << nonNumeric[j][3]<<endl;
getline(fin, nonNumeric[j][4]);
//used 16 to allow different country telephone numbers
//checking whether the telephone number is within a reasonable range.
if(nonNumeric[j][4].length() > 16)
{
fout << "Student telephone number was too long" << endl;
}
//fout << nonNumeric[j][4]<<endl;
getline(fin, nonNumeric[j][5]);
//11 is the max social number.
//checking whether the social is within a reasonable range.
if(nonNumeric[j][5].length() > 11)
{
fout << "Student social was too long" << endl;
}

}

void validateData(int numeric[6][2],int j)
{
fin >> numeric[j][0];
//checking whether age is between 1 and 90
if(numeric[j][0] > 90 || numeric[j][0] < 1)
{
fout << "Age was not in the valid range "<<numeric[j][0] << endl;
}
//fout << numeric[j][0]<<endl;
fin >> numeric[j][1];
//checking whether number of years is between 1 and 90
if(numeric[j][1] > 90 || numeric[j][1] < 1)
{
fout << "Number of years at Texas state was not in the valid range" << endl;
}
//fout << numeric[j][1]<<endl;
}


int inputData()
{
//reading from the input fin.
fin.open("input.txt");
if(!fin)
{
cout << "Error in opening fin: input.txt" << endl;
return -1;
}
return 0;
}
void outputData()
{
fout.open("output.txt"); //Printing to the output fin
}

void writetofin(StudentData Info)
{
for(int i = 0; i <students; i++)
{
fout << setw(32) << Info.nonNumeric[i][0] << endl;
fout << setw(32) << "Name of Student:" << " " << Info.nonNumeric[i][1] << endl;
fout << setw(32) << "Student ID:" << " " << Info.nonNumeric[i][2] << endl;
fout << setw(32) << "Age:" << " " << Info.numeric[i][0] << endl;
fout << setw(32) << "Address:" << " " << Info.nonNumeric[i][3] << endl;
fout << setw(32) << "Number of years at Texas State:" << " " << Info.numeric[i][1] << endl;
fout << setw(32) << "Telephone Number:" << " " << Info.nonNumeric[i][4] << endl;
fout << setw(32) << "Student Social Security #:" << " " << Info.nonNumeric[i][5] << endl << endl;
for(int k = 0; k < 3; k++)
{
fout << setw(32) << "Course#:" << " " << Info.nonNumeric[i][6+k] << endl;
for(int j = 0; j < 5; j++)
{
fout << setw(30) << "Test#" << j + 1 << ":" << " " << Info.numeric2[i][k][j] << endl;
}
fout << fixed << setw(32) << "Numerical Grade:" << " " << Info.numeric2[i][k][5] << endl;
fout << setw(32) << "Letter Grade:" << " " << Info.letterGrade[i][k] << endl;
if(Info.numeric2[i][k][5] >= A_Score)
{
fout << setw(32) << "Commend Note:" << " " << "Congratulations! You have an A!" << endl;
}
else if(Info.numeric2[i][k][5] < D_Score)
{
fout << setw(32) << "Warning Note:" << " " << "You Should Study! You are below a 70 grade average!" << endl;
}
fout << endl << endl;
}
}
fin.close();
fout.close();
}

int main()
{
//get number of students
cout << "Enter the number of Students: ";
cin >> students;
//loop for student information
while(students >= 6 || students <= 0)
{
cout << "Incorrect range of students, please enter a number between 1 - 5" << endl;
cin >> students;
}
inputData();
outputData();
for(int j=0;j<students;j++){
if(j>0)
fin.ignore();

validateData(data.nonNumeric, j);
validateData(data.numeric, j);
letGrade(data, j);
}
writetofin(data);
return 0;
}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//defining structure with having arrays
struct StudentData {
//Define Arrays
string nonNumeric[6][9];
int numeric[6][2];
double numeric2[6][3][6];
char letterGrade[6][3];
} data;

int students;
//Define Floating point variables.
const double A_Score = 90;
const double B_Score = 80;
const double C_Score = 70;
const double D_Score = 60;

//open fins.
ifstream fin;
ofstream fout;

int inputData();
void outputData();
void validateData(string);
void validateData(int);
void letGrade(StudentData Info);
void writetofin(StudentData Info);

///////// Pass by reference here... Otherwise it just becomes a local variable
void letGrade(StudentData &Info, int j) {
//loop for courses
for (int k = 0; k < 3; k++) {
fin.ignore();
getline(fin, Info.nonNumeric[j][6 + k]);
       cout << "Y1: " << Info.nonNumeric[j][6 + k] << endl;
if (Info.nonNumeric[j][6 + k].length() > 11) {
fout << "Course number was too long, skipping to next class!" << endl;
cout << "Course number was too long, skipping to next class!" << endl;
// continue;.. if your course no is long.. you are basically skipping all the fin statements which are inside loop
           // below this line..
           // You need to read them.. You may not want to store their values
}
      
Info.numeric2[j][k][5] = 0;
double percent = .1;
//loop five tests
for (int l = 0; l < 5; l++) {
fin >> Info.numeric2[j][k][l];
cout << "Y2: " << Info.numeric2[j][k][l] << endl;
if (Info.numeric2[j][k][l] < 1.00 || Info.numeric2[j][k][l] > 100.00) {
fout << "Test score was not in valid range" << endl;
cout << "Test score was not in valid range" << endl;
continue; // breaking here.. means.. the fin>> inside this loop will not execute anymore..
}
//calculate grade
Info.numeric2[j][k][5] = Info.numeric2[j][k][5] + (Info.numeric2[j][k][l] * percent);
percent += .05;
}
if (Info.numeric2[j][k][5] >= A_Score) {
Info.letterGrade[j][k] = 'A';
} else if (Info.numeric2[j][k][5] >= B_Score) {
Info.letterGrade[j][k] = 'B';
} else if (Info.numeric2[j][k][5] >= C_Score) {
Info.letterGrade[j][k] = 'C';
} else if (Info.numeric2[j][k][5] >= D_Score) {
Info.letterGrade[j][k] = 'D';
} else {
Info.letterGrade[j][k] = 'F';
}
}
}

void validateData(string nonNumeric[6][9], int j) {
nonNumeric[j][0] = "Students Grade Sheet, Department of Computer Science, Texas State University";
getline(fin, nonNumeric[j][1]);
   cout << "Y3: " << nonNumeric[j][1] << endl;
//checking whether the students name is within a reasonable range.
if (nonNumeric[j][1].length() > 256) {
fout << "Student Name was too long" << endl;
}
getline(fin, nonNumeric[j][2]);
//checking whether the students address is in reasonable range.
if (nonNumeric[j][2].length() > 10) {
fout << "Student ID was not in the valid range" << endl;
}
//fout << nonNumeric[j][2]<<endl;
getline(fin, nonNumeric[j][3]);
if (nonNumeric[j][3].length() > 256) {
fout << "Student address was too long," << endl;
}
//fout << nonNumeric[j][3]<<endl;
getline(fin, nonNumeric[j][4]);
//used 16 to allow different country telephone numbers
//checking whether the telephone number is within a reasonable range.
if (nonNumeric[j][4].length() > 16) {
fout << "Student telephone number was too long" << endl;
}
//fout << nonNumeric[j][4]<<endl;
getline(fin, nonNumeric[j][5]);
   cout << "Y3: " << nonNumeric[j][5] << endl;
//11 is the max social number.
//checking whether the social is within a reasonable range.
if (nonNumeric[j][5].length() > 11) {
fout << "Student social was too long" << endl;
}
}

void validateData(int numeric[6][2], int j) {
fin >> numeric[j][0];
   cout << "Y2: " << numeric[j][0] << endl;
//checking whether age is between 1 and 90
if (numeric[j][0] > 90 || numeric[j][0] < 1) {
fout << "Age was not in the valid range " << numeric[j][0] << endl;
}
//fout << numeric[j][0]<<endl;
fin >> numeric[j][1];
   cout << "Y2: " << numeric[j][1] << endl;
//checking whether number of years is between 1 and 90
if (numeric[j][1] > 90 || numeric[j][1] < 1) {
fout << "Number of years at Texas state was not in the valid range" << endl;
}
//fout << numeric[j][1]<<endl;
}

int inputData() {
//reading from the input fin.
fin.open("input.txt");
if (!fin) {
cout << "Error in opening fin: input.txt" << endl;
return -1;
}
return 0;
}

void outputData() {
fout.open("output.txt"); //Printing to the output fin
}

void writetofin(StudentData Info) {
for (int i = 0; i < students; i++) {
fout << setw(32) << Info.nonNumeric[i][0] << endl;
fout << setw(32) << "Name of Student:" << " " << Info.nonNumeric[i][1] << endl;
fout << setw(32) << "Student ID:" << " " << Info.nonNumeric[i][2] << endl;
fout << setw(32) << "Age:" << " " << Info.numeric[i][0] << endl;
fout << setw(32) << "Address:" << " " << Info.nonNumeric[i][3] << endl;
fout << setw(32) << "Number of years at Texas State:" << " " << Info.numeric[i][1] << endl;
fout << setw(32) << "Telephone Number:" << " " << Info.nonNumeric[i][4] << endl;
fout << setw(32) << "Student Social Security #:" << " " << Info.nonNumeric[i][5] << endl << endl;
for (int k = 0; k < 3; k++) {
fout << setw(32) << "Course#:" << " " << Info.nonNumeric[i][6 + k] << endl;
for (int j = 0; j < 5; j++) {
fout << setw(30) << "Test#" << j + 1 << ":" << " " << Info.numeric2[i][k][j] << endl;
}
fout << fixed << setw(32) << "Numerical Grade:" << " " << Info.numeric2[i][k][5] << endl;
fout << setw(32) << "Letter Grade:" << " " << Info.letterGrade[i][k] << endl;
if (Info.numeric2[i][k][5] >= A_Score) {
fout << setw(32) << "Commend Note:" << " " << "Congratulations! You have an A!" << endl;
} else if (Info.numeric2[i][k][5] < D_Score) {
fout << setw(32) << "Warning Note:" << " " << "You Should Study! You are below a 70 grade average!" << endl;
}
fout << endl << endl;
}
}
fin.close();
fout.close();
}

int main() {
//get number of students
cout << "Enter the number of Students: ";
cin >> students;
//loop for student information
while (students >= 6 || students <= 0) {
cout << "Incorrect range of students, please enter a number between 1 - 5" << endl;
cin >> students;
}
inputData();
outputData();
for (int j = 0; j < students; j++) {
/* why are you putting this line.. there is no blank line after each student
       if (j > 0)
fin.ignore(); */
if (j > 0)
fin.ignore();
validateData(data.nonNumeric, j);
validateData(data.numeric, j);
letGrade(data, j);
}
  
cout << "Af: ";
cout << data.numeric2[0][0][0] << endl;
  
writetofin(data);
return 0;
}


I have put the comments.. basically you are missing two thing here.. One is pass by reference to function, where you calculate the marks.. Next is you are doing validation on lengths of input.. If validtion fails you are putting a break.. Which means if you are trying to read the file in the loop for 3 times, and validation fails on 1st read.. You are not reading those 2 next entries.. Now those two reads will happen in some other method.. Like if you break while reading the marks1.. marks2 and marks3 may be read in the function which is intended for reading the name... Hence use a continue statement not break.. For error case.. put a boolean.. if boolean shows error, you should read from file.. so that file pointer moves ahead.. but should not store those erroneous values in your array.

Hope it helps..



Output:

Students Grade Sheet, Department of Computer Science, Texas State University
Name of Student:   Jill Smith
Student ID:   A8049558
Age:   21
Address:   601 University Dr, San Marcos, TX 78666
Number of years at Texas State:   3
Telephone Number:   (643)-366-346
Student Social Security #:   342-34-3453

Course#: CS1319
Test#1:   90.1
Test#2:   92.3
Test#3:   86.4
Test#4:   87.6
Test#5:   94.1
Numerical Grade:   90.265000
Letter Grade:   A
Commend Note:   Congratulations! You have an A!


Course#: CS1428
Test#1:   80.100000
Test#2:   98.600000
Test#3:   80.900000
Test#4:   95.300000
Test#5:   100.000000
Numerical Grade:   92.805000
Letter Grade:   A
Commend Note:   Congratulations! You have an A!


Course#: CS2308
Test#1:   37.000000
Test#2:   52.300000
Test#3:   45.400000
Test#4:   57.500000
Test#5:   52.500000
Numerical Grade:   50.750000
Letter Grade:   F
Warning Note:   You Should Study! You are below a 70 grade average!


Students Grade Sheet, Department of Computer Science, Texas State University
Name of Student:   Steve Left
Student ID:   A9845788
Age:   22
Address:   500 spring Dr, San Marcos, TX 78666
Number of years at Texas State:   4
Telephone Number:   (210)-765-5686
Student Social Security #:   765-35-2345

Course#: CS1319
Test#1:   97.100000
Test#2:   87.300000
Test#3:   78.400000
Test#4:   89.600000
Test#5:   89.700000
Numerical Grade:   87.795000
Letter Grade:   B


Course#: CS1428
Test#1:   99.100000
Test#2:   89.200000
Test#3:   89.300000
Test#4:   98.400000
Test#5:   100.000000
Numerical Grade:   95.750000
Letter Grade:   A
Commend Note:   Congratulations! You have an A!


Course#: CS2308
Test#1:   100.000000
Test#2:   98.900000
Test#3:   78.600000
Test#4:   78.600000
Test#5:   98.500000
Numerical Grade:   89.755000
Letter Grade:   B


Students Grade Sheet, Department of Computer Science, Texas State University
Name of Student:   Matt Mills
Student ID:   A3548754
Age:   18
Address:   609 Marcos Dr, San Marcos, TX 78666
Number of years at Texas State:   1
Telephone Number:   (210)-264-7547
Student Social Security #:   898-78-3422

Course#: CS1319
Test#1:   78.900000
Test#2:   69.800000
Test#3:   85.800000
Test#4:   58.800000
Test#5:   58.800000
Numerical Grade:   67.860000
Letter Grade:   D


Course#: CS1428
Test#1:   56.900000
Test#2:   99.900000
Test#3:   87.700000
Test#4:   97.700000
Test#5:   89.900000
Numerical Grade:   89.610000
Letter Grade:   B


Course#: CS2308
Test#1:   99.900000
Test#2:   99.900000
Test#3:   99.900000
Test#4:   100.000000
Test#5:   99.900000
Numerical Grade:   99.925000
Letter Grade:   A
Commend Note:   Congratulations! You have an A!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote