Working with arrays on the heap

Pages: 12
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.


Last edited on

How does your datafile layout look?

Like this:


Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48


Working fine here, only thing I changed was a test to see if it had successfully opened the text file.



Is the file good? --> true

Amy Adams
10111
97
86
78
95
I have read 1 records

Ben Barr
20222
89
81
73
87
I have read 2 records

Carla Carr
30333
79
71
63
77
I have read 3 records

Don Davis
40444
69
62
58
67
I have read 4 records

Edna Eaton
50555
63
51
62
48
I have read 5 records


EDIT: also took out some #include's you didnt need.
Last edited on


Source with my changes:

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
93
94

//  main.cpp
//  Program 8
//  Created by William Blake Harp on 7/16/14.

#include <iostream>
#include <fstream>
#include <string>

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");

	if (inputFile.fail())
		cout << "Cant open the file!";
	else
	{
		// 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.
I am using a mac working in Xcode. Would that change things?

Not too sure my friend, the only thing I can assume is there's a problem with your actual data file because I simply copied and pasted the data contents into a file of the same name and it worked fine.

You could try sending me the actual data file using dropbox or something and I can try with yours, but other than that I can't suggest much when it works here.



I think it is a bug with Xcode because my class mates were tried it and it worked mine my teacher even said he does not know why it would not work so I am going to try it on a different computer and see if it works.
I am using a mac working in Xcode. Would that change things?


As far as I know, it shouldn't. I use xcode, but to be honest, I haven't had the need to use fstream much.
Last edited on
@programmerdog297

yea I know Xcode is great I just don't know what is going on. would you mind trying out and see if you can get it to work?
I found the problem.

In Xcode, click on the title of your project (to the right of the stop button).
Choose "edit scheme".
Go to "Run (your project name here)" tab on the left.
Then, go to the options tab.
Select "choose custom working directory" and select the path.
Last edited on
Did you check the line endings of the input file Student Data.txt

Windows uses "\r\n", other OSes use a different representation. It's possible that this ignore() may be expecting different line endings than are actually contained in the file.
71
72
    // consume '\n' char
    inputFile.ignore();


@programmerdog297

That did not work for me. Is is working on your end?


@Chervil


That my be it but I don't know how I would fix that.
It worked fine for me.

Here is the link where I found the answer:
http://stackoverflow.com/questions/16779149/c-program-in-xcode-not-outputting-simple-text-file-using-outfile

Edit:
This is what I put in the text file:
Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48
Last edited on
also in line 57 62 69 I have put the info in a temporary spot. Can nome one show me how to move it from "val" to my other arrays.
@programmerdog297

I did what you said to do and nothing happened :(
I can find the file and that works fine it just stops after

Amy Adams
10111
97 86 78 95

here is what I get


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

Last edited on
That my be it but I don't know how I would fix that

You don't need to 'fix' anything until you've diagnosed that there is or isn't a problem.

On windows I'd suggest notepad++ as a text editor, or a hex editor such as HxD. Either of those would allow you to examine the actual line endings in the file. I've no idea what tools are available for your operating system.
I don't know...

This is the code I am using:
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>

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");
    
    if (inputFile.fail())
        cout << "Cant open the file!";
    else
    {
        // 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. 


And this is my output:
Is the file good? --> true

Amy Adams
10111
97
86
78
95
I have read 1 records

Ben Barr
20222
89
81
73
87
I have read 2 records

Carla Carr
30333
79
71
63
77
I have read 3 records

Don Davis
40444
69
62
58
67
I have read 4 records

Edna Eaton
50555
63
51
62
48
I have read 5 records
One thing you could, indeed should do, is to add additional checks to confirm that reading from the file was successful. Clearly the messages such as "I have read 3 records" is not a meaningful message. You might try something like this, to help pin down the problem:
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
    for ( int i(0); i < 5 && inputFile; i++ )
    {
        string str;
        int    val;
        
        // read name
        if (getline(inputFile,str))
        {
            cout << "Name:  " << str<<endl;
        }
        
        // read student id
        if (inputFile >> val)
        {
            cout << "ID:    " << val << endl;
        }
        
        // read four(4) scores
        for ( int j(0); j < 4; j++ )
        {
            if (inputFile >> val)
            {
                cout << "score: "<<  val << endl;
            }
        }
        
        if (inputFile)
            cout<<"I have read "<<i+1<<" records\n\n";
        
        // consume '\n' char
        inputFile.ignore();
        
    } // end read loop 


At the very minimum the test at line 74 would be useful, but note I added checks on each input operation, as well as in the condition statement of the for loop.
Last edited on
ok every one I got it to work!!!! It is a thing when you use a mac you can't use inputFile.ignore(); (or at least when you use Xcode.... so yea this is good to know)

you have to go and use a getline statement.(as I show in my code below)

But unfortunately I am not done and I ran out of time with my professor so I did not get to finish asking my last questions. Which is how now that i have done this go and use a compile of for loops to get average, class average etc. Below is my code. It looks like it could work but it needs some tweaking. Thanks in advance for your 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//  main.cpp
//  Program 8
//  Created by William Blake Harp on 7/16/14.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    double average = 0;
    double class_average = 0;
    int sum_of_test_scores = 0;
    
    //2D Array.
    const int students = 5;
    const int test_scores = 4;
    int scores[students][test_scores];
    
    //Arrays on the heap.
    string* arrayNames = nullptr;
    arrayNames = new string[students];
    
    int* arrayID = nullptr;
    arrayID = new int[students];
    
    int* arrayAverage = nullptr;
    arrayAverage = new int[students];
    
    int* arrayLetterGrade = nullptr;
    arrayLetterGrade = new int[students];
    
    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;
    
    if (inputFile.fail())
        cout << "Can't open the file!" << endl;
    
    else
    {
        for( int i(0); i != 5; i++ )
        {
            
            // read name
            getline(inputFile, arrayNames[i]);
            cout<<arrayNames[i]<<endl;
            
            
            // read student id
            inputFile >> arrayID[i];
            cout<< arrayID[i] <<endl;
            
            // read four(4) scores
            for( int j(0); j != 4; j++ )
            {
                inputFile >> scores[i][j];
                cout << scores[i][j] << endl;
            }
            
            string str;
            
            // consume '\n' char
            getline (inputFile, str);
        
            cout << "I have read "<<i+1 <<" record/s\n\n";
        }// end read loop
    
        
        // display student name and scores
        for( int i(0); i != 5; i++ )
        {
            cout << arrayNames[i] << "  ";
            
            for( int j(0); j != 4; j++ )
                cout << scores[i][j] << " ";
                
            
            cout << endl
            << endl;
        }
        
       
        // Loop for average
        for (int i(0); i != 5; i++)
        {
            sum_of_test_scores += scores[i];
            average = sum_of_test_scores / 5;
            cout << "The average is: " << average << endl;
        }
        
        
        
        // Loop for grade.
        for (int i = 0; i != 5; i++)
        {
            // Geting a letter grade.
            if (average >= 90)
            {
                cout << "Letter grade is A!" << endl;
            }
            else if (average >= 89 && average <= 80)
            {
                cout << "Letter grade is B" << endl;
            }
            else if (average >= 79 && average <= 70)
            {
                cout << "Letter grade is C" << endl;
            }
            else if (average >= 79 && average <= 60)
            {
                cout << "Letter grade is D" << endl;
            }
            else (average < 60);
            {
                cout << "Letter grade is F!" << endl;
            }
            
        }
        
        
        // Loop for class average.
        for (int i = 0; i != 5; i++)
        {
            class_average = sum_of_test_scores / 5;
            
        }
        cout << "The class average is: " << class_average << endl;
        
        
        // 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.


output:


Is the file good? --> true

Amy Adams
10111
97
86
78
95
I have read 1 record/s

Ben Barr
20222
89
81
73
87
I have read 2 record/s

Carla Carr
30333
79
71
63
77
I have read 3 record/s

Don Davis
40444
69
62
58
67
I have read 4 record/s

Edna Eaton
50555
63
51
62
48
I have read 5 record/s

Amy Adams
  97 86 78 95 

Ben Barr
  89 81 73 87 

Carla Carr
  79 71 63 77 

Don Davis
  69 62 58 67 

Edna Eaton
  63 51 62 48 

The average is: 3.21283e+08
The average is: -1.11571e+08
The average is: -1.25002e+08
The average is: -1.78725e+08
The average is: -3.93615e+08
Letter grade is F!
Letter grade is F!
Letter grade is F!
Letter grade is F!
Letter grade is F!
The class average is: -3.93615e+08


Last edited on
Pages: 12