My program prints the address of the pointer rather than the value

I had it working correctly before I implemented the do while. I thought using delete to free up the space would work, but it doesn't for some reason. So my question is how do I print the value of the pointer in a do while loop?

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
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
int main()
{
    ofstream out;
    ofstream deans;
    ifstream in;
    deans.open("deans.txt");
    in.open("students.txt");
    out.open("newtranscript.txt");
    out.setf(ios::fixed,ios::floatfield);
    out.precision(2);

    int credits[100],gradeValue[100],n,sumCredits,sumGradeValue,numCourses,courseNum,complCreds;
    int majorSumCredits, majorSumGradeValue,counter = 0;
    char gradeReceived[100];
    double gpa, majorGpa;
    string first,last,major,id,dept;
    
    do
    {
        int *majorGradeValue;
        majorGradeValue = new  int[100];
        int *majorCredits;
        majorCredits = new int[100];
        
        in>>id;
        in>>last;
        in>>first;
        in>>major;
        in>>numCourses;

        sumCredits=0,complCreds=0,sumGradeValue=0;
        majorSumCredits=0, majorSumGradeValue=0;

        for(int i = 0; i < numCourses; i++)
        {
            in>>dept;
            in>>courseNum;
            in>>credits[i];
            in>>gradeReceived[i];

            if(gradeReceived[i] == 'A')
                gradeValue[i] = 4*credits[i];
            if(gradeReceived[i] == 'B')
                gradeValue[i] = 3*credits[i];
            if(gradeReceived[i] == 'C')
                gradeValue[i] = 2*credits[i];
            if(gradeReceived[i] == 'D')
                gradeValue[i] = 1*credits[i];
            if(gradeReceived[i] == 'F')
                gradeValue[i] = 0*credits[i];
            if(gradeValue[i] >= 1)
                complCreds +=credits[i];
            if (dept == major)
            {
                majorGradeValue[i] = gradeValue[i];
                majorCredits[i] = credits[i];
                cout<<"major grade value is "<<majorGradeValue<<endl;
                cout<<"major credits is "<<majorCredits<<endl;
            }
            majorSumCredits += majorCredits[i];
            majorSumGradeValue += majorGradeValue[i];
            sumCredits += credits[i];
            sumGradeValue += gradeValue[i];
        }

        gpa=(double)sumGradeValue/sumCredits;
        majorGpa = (double)majorSumGradeValue/majorSumCredits;

        out<<left<<id<<setw(5)<<"\t"<<last<<","<<setw(5)<<first<<"\t";
        out<<right<<setw(11)<<"Major: "+major;
        out<<setw(15)<<"\t"<<"Completed Credits: "<<complCreds;
        out<<setw(15)<<"\t"<<"GPA: "<<gpa;
        out<<setw(15)<<"\t"<<"Major GPA: " <<majorGpa<<endl;
        if(gpa >= 3)
            deans<<id<<"\t"<<last<<", "<<first<<"\t"<<"\t"<<"Major: "<<major<<"\t"<<"\t"<<gpa<<endl;
        counter++;
        delete [] majorCredits;
        delete [] majorGradeValue;
    }
    while(counter != 100);

    in.close();
    out.close();
    deans.close();
    return 0;
}
Line 62,63: majorGradeValue and MajorCredits are int arrays. This is why you're getting pointers being printed. If you want the values, then use an index.

62
63
  cout<<"major grade value is "<<majorGradeValue[i]<<endl;
  cout<<"major credits is "<<majorCredits[i]<<endl;



Thanks man, your hint made realize the real crux of the problem. I actually had to assign the values of majorSumCredits and majorSumGradeValue within the if statement above it.
Last edited on
Topic archived. No new replies allowed.