What I have to do is listed below. If you look at my code I am wondering why it is not looping through all of the text file. The out put is also listed bellow. As you can see by the out put it just out puts the 1st record and makes all the others 0... Any help would be greatly appreciated!
Out Put:
Is the file good? --> true
Amy Adams
10111
97
86
78
95
I have read 1 records
0
0
0
0
0
I have read 2 records
0
0
0
0
0
I have read 3 records
0
0
0
0
0
I have read 4 records
0
0
0
0
0
I have read 5 records
Create a Static 2D Array on the Stack to hold a set of four test scores for five students.
Create dynamic parallel arrays (on the heap) to hold student names, student id, average score, and a letter grade based on standard grade scale.
Populate the arrays from the file Student Data.txt
Write a program to do the following tasks:
1. Calculate average score for each student.
2. Assign letter grade for each student.
3. Display name, id, scores, average, and grade for each student.
4. calculate the class average score and display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// main.cpp
// Program 8
// Created by William Blake Harp on 7/16/14.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
int size = 0;
int sum_of_test_scores = 0;
double average = 0;
double class_average = 0;
//2D Array.
const int students = 5;
const int test_scores = 4;
int s[students][test_scores];
//Arrays on the heap.
string* arrayNames = nullptr;
arrayNames = new string[size];
int* arrayID = nullptr;
arrayID = new int[size];
int* arrayAverage = nullptr;
arrayAverage = new int[size];
int* arrayLetterGrade = nullptr;
arrayLetterGrade = new int[size];
ifstream inputFile;
// Open the file
inputFile.open("Student Data.txt");
// do an initial test.
cout<<boolalpha;
cout<< "Is the file good? --> "<<inputFile.good()<<endl<<endl;
for( int i(0); i != 5; i++ )
{
string str;
int val;
// read name
getline(inputFile,str);
cout<<str<<endl;
// read student id
inputFile >> val;
cout<<val<<endl;
// read four(4) scores
for( int j(0); j != 4; j++ )
{
inputFile >> val;
cout<<val<<endl;
}
// consume '\n' char
inputFile.ignore();
cout<<"I have read "<<i+1<<" records\n\n";
}// end read loop
// Close the file.
inputFile.close();
// releasing memory block on the heap.
delete [] arrayNames;
arrayNames = 0;
delete [] arrayID;
arrayID = 0;
delete [] arrayAverage;
arrayAverage = 0;
delete [] arrayLetterGrade;
arrayLetterGrade = 0;
return 0;
}// End Code.
|