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 :)
#include <iostream> //file I/O
#include <iomanip> //setprecision
#include <fstream> //to read data from another file
usingnamespace std;
int getData(int list[], int count); //getData function prototype
int getMax(constint list[], int SIZE); // getMax function prototype
void printData(int list[]); // printData function prototype
int curveGrade(constint list[], int SIZE); // curveGrade function prototype
int main()
{
constint 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)
{
constint 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(constint 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 (constint 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;
}
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:
#include <iostream> //file I/O
#include <iomanip> //setprecision
#include <fstream> //to read data from another file
#include <cmath>
usingnamespace std;
int main()
{
constint 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;
}
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.
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.
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.