Values Assigned as Characters Displaying as Integers

Is it possible to use two different arrays to produce the following result(with one array being the numbers and the other being the letters)?


98     99     99    100     A
86     80     85    92      B
103    94     96    97      A


Whenever I try to do this, it ends up looking like this:

98     99     99    100     
86     80     85    92      
103    94     96    97      A
B
A
Last edited on
Ok, I've figured how to "line them up". Now, however, the letters are displayed as some random-looking integers:


98     99     99    100     3611960
86     80     85    92      2147183319
103    94     96    97      36

Here's my code:
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

//displays everythng as one array
void table(int output[][6], string labels[], int names[][1],  int grades[][4], double average[][1], char ltrgrds[][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)){
                output[i][x]=grades[i][x-1];
            }
            else{
                output[i][5]=average[i][0];
            }
            cout<<output[i][x];
            if(x==6){
                cout<<endl;
            }
            else if(x==7){
                cout<<ltrgrds[i][0]<<"\t";
            }
            else{
                cout<<"\t";
            }
        }
    }
}

//...

//calculates the letter grade
    for(int i=0; i<3; i++){
            int x=0;
            if(average[i][x]<70){
                ltrgrds[i][0]='F';
            }
            else if((average[i][x]>=70)&&(average[i][x]<77)){
                ltrgrds[i][0]='D';
            }
            else if((average[i][x]>=77)&&(average[i][x]<=85)){
                ltrgrds[i][0]='C';
            }
            else if((average[i][x]>=85)&&(average[i][x]<=93)){
                ltrgrds[i][0]='B';
            }
            else{
                ltrgrds[i][0]='A';
            }
            x=x+1;
    }
Couple of problems. Starting at line 5:
1
2
3
4
for(int x=0; x<7; x++){
//stuff
else if(x==7) //never get here
}

This is why you don't see any letters. Also, output should only contain 6 columns, but you do stuff for 7 columns (x=0, x=1, ..., x=6) and are exceeding the array bounds of output on line 15 when x is 6:
cout<<output[i][x]; //remember index counting starts at zero
This is probably why you see some garbage displayed. There may also be inappropriate arrays being passed into the table() function which could also cause garbage to be displayed.

Also you may want to consider breaking up table() into smaller, more manageable functions you can modify and error-check one at a time.
I changed the 7 to an 8 (and everything that has to be changed because of it), and it's somewhat (?) better now.

Student      Test1    Test2     Test3    Test4    Avg      Grade
1               90           90         100       100      95        4638544  
4683024A2          94            90          91           89         6430008
2686552B3          67            64          66           63         16
6430008F  


I need "4683024A2" to just be a 2, and move the A to where "4638544" is.
Same with "2686552B3 " for "6430008", and 16 should be the F.



And I tested each function individually ahead of time, and putting them all together is my problem.
Last edited on
I changed the 7 to an 8

This makes the problem worse since now you're going even more out of the bounds of the output array. The problem is that only output[i][0] to output[i][5] are valid (assuming they've been initialized). output[i][6] (and now output[i][7]) is garbage.

Also something else I've just noticed is that you're printing a newline before the letter grade, which isn't what you want to do. Once you've fixed the index problem you'll want to fix this too or else your output will look like this:
Student      Test1    Test2     Test3    Test4    Avg      Grade
1               90           90         100       100      95
A

Also note that what is printed to the screen is not properly aligned, but I don't know if that's a result of your copying and pasting code or if that's what is really printed on the screen. If it's the latter you can format output to make everything fit nicely but it's not a big issue.
I'm not trying to put a new value into output. I'm trying to print ltrgrds after each line of output. Am I able to do this? If I can't, I know how to change it back but I need to find some way of making sure the letter gradses stay as letters. I have this whole problem because they are displayed as random letters there too, even when output is a char.
I'm not trying to put a new value into output.

You end up changing memory outside the bounds of output as well as displaying memory values outside the range of output thanks to line 15 of your code. Here's what happens (using code initially given in post):
1)Enter outer for() loop on line 4, declare integer i and assign it value 0.
2)Enter inner for() loop on line 5, declare integer x and assign it value 0.
3)x == 0 so output[0][0] assigned value names[0][0]
4)print output[0][0]
5)print tab
6)increment x, so now x == 1

7) for each x == 1 to x == 4 the inner loop assigns output[0][x] the value grades[0][x-1] then prints the value followed by a tab, then increments x.

8) for x == 5 the inner loop assigns output[0][5] the value average[0][0] then prints the value followed by a tab, then increments x.

9) for x == 6 the inner loop assigns output[0][6] the value average[0][0] the prints output[0][6]. Note that the assignment is changing memory outside the bounds of output
and the behaviour of such an assignment is undefined (I think), the program could even crash. For your system the program prints garbage. The program then prints a newline, then increments x.

10) for x == 7 the inner loop terminates and the x variable is destroyed (ltrgrds[0][0] is never printed). The outer loop then increments i and 2) - 9) repeat (using the new i value) until i == 3.


I'm trying to print ltrgrds after each line of output. Am I able to do this?

Yes, but you need to change you code so that when x == 6 you don't print garbage, but rather the letter grade.

Topic archived. No new replies allowed.