Hi, im pretty new to c++ and i have created the program below, and for some reason it sets 1 or 2 arrays right then the rest just 0's, when it uses the same code, and the data is getting threw to the calls etc... i have no idea why it doesnt set the arrays.

I have lots of testing that outputs whats going on ... and you can see that it should set the data cause it sets 1 or 2 before it...

So if im doing something wrong (which i probably am, as i said im new) i would greatly appreciate it.

Get the grades.txt data below and make it a txt file then run the code.

When u run the program you can just enter:
2
grades.txt

Grades.txt data below:


000101 73
000102 56.5
000135 88.3
123456 67.3
990001 64.5
990002 46
000103 57
101011 58
010303 59
592101 60
414332 68
432131 69
145234 70
546525 71.7

//grades.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// Instead of making variables of how many students per grade, average and stand deviation
// per grade, i will just make 3 arrays and store them from 0=HD - 4=F
// When i had aStuGrades as aStuGrades[4] it created an error for the data that was 
// in the 4th section (aStuGrades[4]) and changing it to 5 seemed to fix it.
int aStuGrades[5];  // Store how many students per grade in array
float aAv[5];       // Store the average of each grade in array
float aStndDev[5];  // Store the standard deviation of each grade in array

string aGrades[]={"H","D","C","P","F"};

string aHStuID[299];
float aHMark[299];

string aDStuID[299];
float aDMark[299];

string aCStuID[299];
float aCMark[299];

string aPStuID[299];
float aPMark[299];

string aFStuID[299];
float aFMark[299];

void CalculateGrade(string iStuID, float fStuMark){
    int iGrade;
    iGrade = 0;
    if (fStuMark >= 85) {
        aHStuID[aStuGrades[iGrade]] = iStuID;
        aHMark[aStuGrades[iGrade]] = fStuMark;
        iGrade = 0;
    }else if ((fStuMark >= 75) && (fStuMark < 85)) {
        aDStuID[aStuGrades[iGrade]] = iStuID;
        aDMark[aStuGrades[iGrade]] = fStuMark;
        iGrade = 1;
    }else if ((fStuMark >= 65) && (fStuMark < 75)) {
        aCStuID[aStuGrades[iGrade]] = iStuID;
        aCMark[aStuGrades[iGrade]] = fStuMark;
        iGrade = 2;
    }else if ((fStuMark >= 50) && (fStuMark < 65)) {
        aPStuID[aStuGrades[iGrade]] = iStuID;
        aPMark[aStuGrades[iGrade]] = fStuMark;
        iGrade = 3;
    }else if (fStuMark < 50) {
        aFStuID[aStuGrades[iGrade]] = iStuID;
        aFMark[aStuGrades[iGrade]] = fStuMark;
        iGrade = 4;
    }
    aAv[iGrade] = aAv[iGrade] + fStuMark;
    aStuGrades[iGrade] = aStuGrades[iGrade] + 1;
    cout << aGrades[iGrade] << " " << aStuGrades[iGrade] << " id " << iStuID << " m " << fStuMark << endl;
}

int ReadFromFile(const char * argv){
    int fSuccess;
    int i = 0;
    string fFile;
    fFile = argv;
    cout << "Reading data from " << fFile << endl;
    ifstream inFile;
    inFile.open(argv);
    string iStuID;
    float fStuMark;
    if (!inFile) {
        fSuccess = 0;
    }else{
        fSuccess = 1;
        // Set arrays values to 0
        for(int x=0;x<=4;x++){
            aStuGrades[x] = 0;
            aAv[x] = 0;
            aStndDev[x] = 0;
        }
        while (inFile >> iStuID >> fStuMark) {
            CalculateGrade(iStuID, fStuMark);
        }
        inFile.close();
    }
    return fSuccess;   
}

int main(int argc, char *argv[]){
    if (argc == 2){
       if (ReadFromFile(argv[1]) == 0){
           cout << "Unable to open " << argv[1] << endl;
           system("pause");
           return 0;
       }
    }else{
        int iEntData;
        const char * c_str();
        string sFiletoRead;
        string iStuID;
        float fStuMark;
        cout << "How would you like to enter data [1/2] ?\n\t1 - Manual input\n\t2 - From data file" << endl;
        cin >> iEntData;
        switch (iEntData) {
            case 1: 
                cout << "Manual Entry:\nPlease enter Student ID then mark as shown below\n-> 123456 99.9\nCTRL + Z to finish." << endl;
                // Set arrays values to 0
                for(int x=0;x<=4;x++){
                    aStuGrades[x] = 0;
                    aAv[x] = 0;
                    aStndDev[x] = 0;
                }
                while (!cin.eof()){
                    cin >> iStuID >> fStuMark;
                    if (!cin.eof()){
                        CalculateGrade(iStuID, fStuMark);
                    }
                }
            break;
            case 2:  
                cout << "Please enter the file you would like to read" << endl;
                cin >> sFiletoRead;
                if (ReadFromFile(sFiletoRead.c_str()) == 0){
                    cout << "Unable to open " << sFiletoRead << endl;
                    system("pause");
                    return 0;
                }
            break;
            default:
                cout << "Invalid integer entered\n";
                system("pause");
                return 0;
            break;
        }
    }
    double tmp;
    for(int x=0;x<=4;x++){
        if (aStuGrades[x] != 0){
            aAv[x] = aAv[x] / aStuGrades[x];
            for(int i=0;i<aStuGrades[x];i++){
                switch (x) {
                    case 0: // HD
                        aStndDev[x] = aStndDev[x] + pow((aHMark[i] - aAv[x]),2);
                        cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aHStuID[i] << " m " << aHMark[i] << " av " << aAv[x] << endl;
                    break;
                    case 1: // D
                        aStndDev[x] = aStndDev[x] + pow((aDMark[i] - aAv[x]),2);
                        cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aDStuID[i] << " m " << aDMark[i] << " av " << aAv[x] << endl;
                    break;
                    case 2: // C
                        aStndDev[x] = aStndDev[x] + pow((aCMark[i] - aAv[x]),2);
                        cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aCStuID[i] << " m " << aCMark[i] << " av " << aAv[x] << endl;
                    break;
                    case 3: // P
                        aStndDev[x] = aStndDev[x] + pow((aPMark[i] - aAv[x]),2);
                        cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aPStuID[i] << " m " << aPMark[i] << " av " << aAv[x] << endl;
                    break;
                    case 4: // F
                        aStndDev[x] = aStndDev[x] + pow((aFMark[i] - aAv[x]),2);
                        cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aFStuID[i] << " m " << aFMark[i] << " av " << aAv[x] << endl;
                    break;
                }
            }
            tmp = aStndDev[x] / aStuGrades[x];
            //aStndDev[x] = pow(tmp,0.5);
            cout << aStuGrades[x] << " students " << aGrades[x] << " av: " << aAv[x] << " sd: " << aStndDev[x] << " . " << tmp << endl;
        }
    }
    system("pause");
    return 0;
}

This is the output once u run the program:

Below is setting the arrays and you can see the data is getting through (student id and student mark), and it is calculating them because of the grade (H, D, C, P, F)... so i dont know whats wrong
C	1	id	000101	m	73
P	1	id	000102	m	56.5
H	1	id	000135	m	88.3
C	2	id	123456	m	67.3
P	2	id	990001	m	64.5
F	1	id	990002	m	46
P	3	id	000103	m	57
P	4	id	101011	m	58
P	5	id	010303	m	59
P	6	id	592101	m	60
C	3	id	414332	m	68
C	4	id	432131	m	69
C	5	id	145234	m	70
C	6	id	546525	m	71.7

This is the output of the actual arrays
and u can see for each array 1 or 2 work and the rest the data isnt set.
H	sd	0	i	0	id	000135	m	88.3	av	88.3
1 students H av: 88.3 sd: 0 . 0
C	sd	10.0278	i	0	id	000101	m	73	av	69.8333
C	sd	13.5122	i	1	id	546525	m	71.7	av	69.8333
C	sd	4890.21	i	2	id		m	0	av	69.8333
C	sd	9766.9	i	3	id		m	0	av	69.8333
C	sd	14643.6	i	4	id		m	0	av	69.8333
C	sd	19520.3	i	5	id		m	0	av	69.8333
6 students C av: 69.8333 sd: 19520.3 . 3253.38
P	sd	7.11112	i	0	id	000102	m	56.5	av	59.1667
P	sd	7.80556	i	1	id	592101	m	60	av	59.1667
P	sd	3508.5	i	2	id		m	0	av	59.1667
P	sd	7009.19	i	3	id		m	0	av	59.1667
P	sd	10509.9	i	4	id		m	0	av	59.1667
P	sd	14010.6	i	5	id		m	0	av	59.1667
6 students P av: 59.1667 sd: 14010.6 . 2335.1
F	sd	2116	i	0	id		m	0	av	46
1 students F av: 46 sd:	2116 . 2116

its alright i fixed it ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.