Problem printing results (using arrays and functions)

This program is supposed to be able to get data from a file (in this case, the file just contains 10 grades), find the maximum value (highest score), and print those results, plus the curved score for each.

The values in the file indata3.txt - (92 65 68 75 90 95 84 70 63 58).

My problem is the program compiles, but only the getData and getMax functions are working. printData won't print the scores, so that means the curveGrade function won't work either.

If someone could point me in the right direction for my print function, that would help a lot :)

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
#include <iostream>								//file I/O
#include <iomanip>								//setprecision
#include <fstream>								//to read data from another file
	using namespace std;

int getData(int list[], int count);				//getData function prototype

int getMax(const int list[], int SIZE);			// getMax function prototype

void printData(int list[]);						// printData function prototype

int curveGrade(const int list[], int SIZE);		// curveGrade function prototype


int main()
{
  const int SIZE = 15;			// to ensure elements in file are not modified
  ifstream inFile;				// Input file stream
  ifstream inData;
  ofstream outData;
  ofstream outFile;				// Output file stream
  int list[SIZE];				// declaration of array of type int
  int count = 0;
  
	getData(list, count);		// getData function call
	cout << endl;
	outData << endl;

	getMax(list, SIZE);			// getMax function call
	cout << endl;
	outData << endl;

	printData(list);		// printData function call
	cout << endl;
	outData << endl;

	inData.close();
	outData.close();

	return 0;
}

 // getData function definition

int getData(int list[], int count)
{
	const int SIZE = 15;

	ifstream inFile;
	ifstream inData;
	ofstream outFile;
	ofstream outData;

	inFile.open("indata3.txt");
	outFile.open("outdata3.txt");

  	// Read the first item from the list in the input file and count the first item
	inFile >> list[0];
	count++;
  
  // As long as there are items in the file AND the count of items in the file does not
  // equal or exceed MAX, read the next item and put it in the array.
  while (inFile)
  {
    
    if (count < SIZE) {
      inFile >> list[count];
      count++;
    }
    else {
      cout << "List size exceeds SIZE.  Skipping remaining items in input file." << endl;
	  outData << "List size exceeds SIZE.  Skipping remaining items in input file." << endl;
      break;
    }  
  
    }
  
  count--; // Readjust value of count to avoid off-by-one count
  
  return list, count;
  }

int getMax(const int list[], int SIZE)		// getMax function definition
{
	
	int i, max;
	
	ofstream outData;

	// Set the index of max to 0 to start stepping through array to max
	max = list[0];
	 
	// For loop to find the max
	for (i = 0; i < SIZE; i++)
	  if (list[i] > max)
			max = list[i];

	cout << "max is = " << max << endl; // for testing
	  
	 return max;

}

void printData (int list[])
{								
	
	int i, count = 0, curvedGrade = 0;

	ifstream inFile;
	ifstream inData;
	ofstream outFile;
	ofstream outData;

	inFile.open("indata3.txt");
	outFile.open("outdata3.txt");
	
    cout << setw(2) << "Student" << setw(15) << "Absolute" << setw(15) << "Relative" << setw(15) << "Graph" << endl;
	cout << setw(2) << "Number" << setw(13) << "Grade" << setw(15) << "Grade" << endl;
	cout << setw(2) << "-------" << setw(14) << "-------" << setw(15) << "-------" << setw(19) << "--------" << endl;

	for (i = 0; i < count; i++)			// For loop to print
  {
	cout << setw(3) << i+1 << setw(15) << list[i] << setw(15) << curvedGrade << endl;
  }
	
 cout << endl;
 outData << endl;

}

int curveGrade (const int list[], int SIZE)
{
	
	int i = 0;
	int max = 0;
	int absGrade, curvedGrade;
	
	absGrade = list[i];
	curvedGrade = static_cast<int>(static_cast<float>(absGrade)/static_cast<float>(max) * 100.0 + 0.5);

	return curvedGrade;

}

Last edited on
I think the problem is here,the for loop isn't executing

1
2
3
4
5
6
        int i, count = 0, curvedGrade = 0;
        //....
	for (i = 0; i < count; i++)			// For loop to print
       {
	cout << setw(3) << i+1 << setw(15) << list[i] << setw(15) << curvedGrade << endl;
       }


That's what I'm thinking too, only I can't figure out why.

I have a version of this code that doesn't use functions that actually does print, so that leads me to believe the way my function is set up isn't returning the values:

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
#include <iostream>								//file I/O
#include <iomanip>								//setprecision
#include <fstream>								//to read data from another file
#include <cmath>
	using namespace std;

int main()
{
  const int SIZE = 15;			// to ensure elements in file are not modified
  ifstream inFile;				// Input file stream
  ifstream inData;
  ofstream outData;
  ofstream outFile;				// Output file stream
  int list[SIZE];				// declaration of array of type int
  int count = 0;
  int i, max, absGrade, curvedGrade; 
	 
  //int stars;


inFile.open("indata3.txt");
	outFile.open("outdata3.txt");

  	// Read the first item from the list in the input file and count the first item
	inFile >> list[0];
	count++;
  
  // As long as there are items in the file AND the count of items in the file does not
  // equal or exceed MAX, read the next item and put it in the array.
  while (inFile)
  {
    
    if (count < SIZE) {
      inFile >> list[count];
      count++;
    }
    else {
      cout << "List size exceeds SIZE.  Skipping remaining items in input file." << endl;
	  outData << "List size exceeds SIZE.  Skipping remaining items in input file." << endl;
      break;
    }  
  
    }
  
  count--; // Readjust value of count to avoid off-by-one count
 

	// Set the index of max to 0 to start stepping through array to max
	max = list[0];
	 
	// For loop to find the max
	for (i = 0; i < SIZE; i++)
	  if (list[i] > max)
			max = list[i];

	
	cout << endl;


// -------------------------- Curved Grade Logic --------------------------------------------------

	
	cout << setw(2) << "Student" << setw(15) << "Absolute" << setw(15) << "Relative" << setw(15) << "Graph" << endl;
	cout << setw(2) << "Number" << setw(13) << "Grade" << setw(15) << "Grade" << endl;
	cout << setw(2) << "-------" << setw(14) << "-------" << setw(15) << "-------" << setw(19) << "--------" << endl;

	for (i = 0; i < count; i++)			// For loop to print
  {
    absGrade = list[i];
	curvedGrade = static_cast<int>(static_cast<float>(absGrade)/static_cast<float>(max) * 100.0 + 0.5);
	

	cout << setw(3) << i+1 << setw(15) << list[i] << setw(15) << curvedGrade 
		<< setw(17) << curvedGrade /* placeholder*/ << endl;
    
  }
	cout << endl;
	outData << endl;

	inData.close();
	outData.close();



  return 0;
}
Last edited on
In the first example, your PrintData() function sets count to 0 then tries to loop while (i < count) since i starts at 0, this means it will never be true. what you need to do really is change the parameters to PrintData() so you can tell it how many entries the array has, then loop from 0 up to that number.
Just wondering what the outData file is for?
int i, count = 0, curvedGrade = 0;
//....
for (i = 0; i < SIZE ; i++) // For loop to print
{
cout << setw(3) << i+1 << setw(15) << list[i] << setw(15) << curvedGrade << endl;
}

The argument has to be set to SIZE which is your constant int. Also you have useless code in your program. There isnt a need for the outData, since the purpose is to read from a file, and no cin or input is asked of from the user. Also, decalare the const int SIZE outside main, so you can use it in all your functions with out have to redeclare or pass through a function. Also, you shoud only return one value in a function. The purpose of a function is to modulise the code, and return single values. Also, int getData should be a void, you are not returning an int, your a writing data to an array, and since an array is basically a reference or pointer, it saves the data from the file into the array, with out the need to return any thing.
outData is to print to a file.

After a few modifications, I got the code to work. Thanks for the help :)
yea, i know what outData is for, but you are not requesting user input, so there is no need for it. The file is already created prior to the program running.
Topic archived. No new replies allowed.