C++ Array help!!!!!

ok my program reads data from a txt file and it displays the test scores and the sum of the scores, and it also shows the max and min from the scores , so far only one number shows up from the scores instead 10 and i think my sum is wrong too, but i cant get the max and min to work


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

using namespace std;
void openfile(ifstream &infile);
void getData(ifstream &infile, int scores[] , int &size);
double calcsum(int scores[], int size);
void getMax(int scores[]);
void display(int scores[], int &size, double sum);


int main()

{
ifstream infile;

int scores[10];
int size;
double min,max;
double sum;

openfile(infile);
getData(infile,scores,size);
sum=calcsum(scores,size);
getMax(scores);
display(scores, size,sum);


system("pause");
return 0;
}



void openfile(ifstream &infile)
{
string InFileName,outfilename;

cout << "Enter the name of your input file : ";
cin >> InFileName;
infile.open(InFileName.c_str());
if (!infile)
{
cout << "I can't find your input file" << endl;
exit(-1);
}


}

void getData(ifstream &infile, int scores[] , int &size)
{
int i = 0;
do
{
infile >> scores[i];
i++;
} while (infile);

size = i - 1;

}

double calcsum(int scores[], int size)
{
double sum = 0;

for (int i = 0; i < size; i++)
sum += scores[i];

return sum;

}

void getMax(int scores[])
{
double max = 0;
for (i = 1; i < scores; i++)
if (scores[i] > max)
max = scores[i];


}

void display(int scores[], int &size, double sum)
{
cout << fixed << showpoint << setprecision(2);
cout << endl << "Test Scores:" << endl;

for (int i = 0; i < size; i++)
cout << setw(7) << scores[i] << endl;

cout << endl << "Total Points: " << sum << endl;
}
Initialize size.
where?? in getMax
if so i did and nothing happened
it says in getmax that i is unidentified
1
2
3
4
5
6
void getMax(int scores[])
{
double max = 0;
for (i = 1; i < scores; i++) // should be for( int i = 1; .. ) 
// and scores is a pointer
}
k i did that now it gives a red line under my less then sign saying operand type
are incompatible
Well, i is type "int" and scores is type "int *", which is a pointer to an int. You wanted the length.
sry , i still dont get it
Give getMax() a size parameter so you know when to stop

1
2
3
4
5
void getMax(int scores[], int size)
{
 double max = 0;
 for (int i = 1; i < size; i++)
...
Last edited on
its working but now im getting more errors, when i try to display max i write
cout << " you High Score is " (<<) (its gives me an error here it says no operater match these operands) max << endl;
Can you please at least post the function that precedes the call so I know what's happening?
yea sry ,
void display(int scores[], int &size, double sum)
{
cout << fixed << showpoint << setprecision(2);
cout << endl << "Test Scores:" << endl;

for (int i = 0; i < size; i++)
cout << setw(7) << scores[i] << endl;
cout << endl << "Total Points: " << sum << endl;

cout << "you " << max << endl;
wait im dumb i never put max in my parameters
ok so its working now but its displaying the max as zero instead of the highest score,
and its still displaying the first number in my file , i have 10 numbers in the file , they go like 9.8 9.7 9.6 .........
Check your getData() function then. Do you properly get the scores[] after getData() is finished?
yea, unless you see something wrong, i have a sample program that reads the data from file and displays , and basically its the same code as mine .so i have no clue
The 'size' variable is passed by reference (& sign) into the getData() function.

In there you're decreasing it. So after getData() finishes, it stays the same number. (whatever it was inside getData)
Last edited on
thank you so much!!!!
Topic archived. No new replies allowed.