char Being Displayed as Numbers

I'm writing a program where the user inputs four test grades each for three students, the values of which are stored in the array grades[3][4]. These grades are then averaged together (again, in a 2D array) and the letter grade is also calculated and displayed in an array.
I then try to output all of the different values in another array so that they are displayed as one big table, like below.

Student    Test 1  Test 2  Test 3   Test 4    Avg   Grade
Student 1  90      90      100      100       95    A
Student 2  94      90      91       89        91    B
Student 3  64      67      66       63        65    F


Instead, this shows up:

Student      Grade 1       Grade 2     Grade 3      Grade 4     Avg     Grade
Student 3          D             D           D            D       D         A
Student 3          [             [           [            [       [         A
Student 3          B             B           B            B       B         A

*this is a diamond
^this is the symbol for female


I've narrowed it down to this function causing the issue.
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
//each conditional is assigning a value of one of the smaller arrays to a place in the output array
void table(string output[][7], string labels[], string names[][1],  int grades[][4], double average[][1], char ltrgrds[][1]){
    int r=0;
    for(int i=0; i<3; i++){
        for(int x=0; x<7; x++){
            if((x==0)){
                output[i][x]=names[i][x];
            }
            else if((x>0)&&(x<5)){
                for(int r=0; r<4; r++){
                    output[i][x]=grades[i][r];
                    r=r+1;
                }
            }
            else if(x==5){
                for(int q=0; q<3; q++){
                    output[i][5]=average[q][0];
                }
            }
            else{
                for(int q=0; q<3; q++){
                    output[i][5]=ltrgrds[q][0];
                }
            }
            cout<<output[i][x];
            if(x>=6){
                cout<<endl;
            }
            else{
                cout<<"\t";
            }
        }
    }
}


Does anybody know what is causing my problem?
Last edited on
Does anybody know what is causing my problem?

Could be a segmentation fault: that is when you try to access memory you shouldn't.
You get into this situation with bad pointers, or when you go out of bounds using an array.

Examples:

1
2
3
4
5
int *p = new int[10];

p[25] = 1; // out of bounds: possible segmentation fault
delete[] p;
p[0] = 1; // "dangling" pointer: possible segmentation fault 


In your own code parameter average is shown as double average[][1], but you access:

output[1][5] = average[0][0];
output[1][5] = average[1][1]; // !!!
output[1][5] = average[2][2]; // !!!

output[2][5] = average[0][3]; // !!! you don't reset r
output[2][5] = average[1][4]; // !!!
output[2][5] = average[2][5]; // !!!

// and so on


In fact I may be wrong, because if(i=0){ resets i to zero every time.
This is because it's an assignment =, not a comparison ==.
Last edited on
I realized that I had been resetting i to zero a few minutes after posting this. I've edited the original post to reflect that change, as it still isn't working properly.
I've fixed the symbols problem, now my code is this:
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
void table(wchar_t output[][7], string labels[], int names[][1],  int grades[][4], double average[][1], char ltrgrds[][1]){
    int r=0;
    for(int i=0; i<3; i++){
        for(int x=0; x<7; x++){
            if((x==0)){
                output[i][x]=names[i][x];
            }
            else if((x>0)&&(x<5)){
                for(int r=0; r<4; r++){
                    output[i][x]=grades[i][r];
                    r=r+1;
                }
            }
            else if(x==5){
                for(int q=0; q<3; q++){
                    output[i][5]=average[i][0];
                }
            }
            else{
                for(int q=0; q<3; q++){
                    output[i][5]=ltrgrds[i][0];
                }
            }
            cout<<output[i][x];
            if(x>=6){
                cout<<endl;
            }
            else{
                cout<<"\t";
            }
        }
    }
}


and my output is

Student      Grade 1       Grade 2     Grade 3      Grade 4     Avg     Grade
1                 100         100          100          100         95        0 
2                 91           91            91            91           91       30182  
3                 66           66            66            66           65       28160


I need to find a way to make sure the letter grades are actually letters and to make the output for each grade value actually be what was inputted into the original array.
I really need somebody to reply in the next hour or so.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
                for(int r=0; r<4; r++){
                    output[i][x]=grades[i][r];
                    r=r+1; // r increased twice!
                }
            }
            else if(x==5){
                for(int q=0; q<3; q++){
                    output[i][5]=average[i][0]; // q never used
                }
            }
            else{
                for(int q=0; q<3; q++){
                    output[i][5]=ltrgrds[i][0]; // q never used
                }
            }
I've changed it but it doesn't seem to do anything to help.
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
//displays everythng as one array
void table(int output[][7], string labels[], int names[][1],  int grades[][4], double average[][1]){
    for(int i=0; i<3; i++){
        for(int x=0; x<7; x++){
            if((x==0)){
                output[i][x]=names[i][x];
            }
            else if((x>0)&&(x<5)){
                for(int r=0; r<2; r++){
                    output[i][x]=grades[i][r];
                }
            }
            else{
                    output[i][5]=average[i][0];
            }
            cout<<output[i][x];
            if(x>=6){
                cout<<endl;
            }
            else{
                cout<<"\t";
            }
        }
    }
}



I'm getting the error "invalid conversion from 'const char*' to 'char'" for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//makes a separate column to avoid the char issue
void letterGrades(char ltr[][1], char ltrgrds[][1]){
    int x=0;
    int q;
    for(int i=0; i<4; i++){
        if(i==0){
            ltr[1][0]="G";
        }
        else{
            q=i-1;
            ltr[i][x]=ltrgrds[q][0];
        }
        cout<<ltr[i][x];
        x=x+1;
    }
}


Here's my main (if the error's there):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(){
    int grades[3][4], output[4][7];
    double avg, average[3][1];
    char ltrgrds[3][1], ltr[4][1];
    int names[3][1];
    string labels[6];
    cout<<setprecision(2);
    cout.setf(ios::showpoint|ios::fixed);

    gradeInput(grades);
    namec(names);
    avgm(ltrgrds, average, grades);
    heading(labels);
    letterGrades(ltr, ltrgrds);
    table(output, labels, names, grades, average);

return 0;
}
Topic archived. No new replies allowed.