I have a code and when I want to run it with the .txt file it wont give me the right output. How do i make the code read the output file of score.txt My code is:
using namespace std;
void readFile(int *a, int s);
void readScores(int *a, int s);
void printAllScores(int *a, int s);
void printHighest(int *a, int s);
void printLowest(int *a, int s);
void average(int *a, int s);
void printOneScore(int *a, int s);
int readMenu();
int getpid();
int main() {
clock_t tStart = clock();
bool flag=true;
int size=10;
int a[10]={0,0,0,0,0,0,0,0,0,0};
int option;
do
{
option=readMenu();
switch (option)
{
case 1:
readScores(a,size);
break;
case 2:
readFile(a,size);
break;
case 3:
printAllScores(a,size);
break;
case 4:
printHighest(a,size);
break;
case 5:
printLowest(a,size);
break;
case 6:
average(a,size);
break;
case 7:
printOneScore(a,size);
break;
case 8:
flag=false;
break;
default:
cout <<"\nInvalid Option\n";
break;
}
}
while (flag);
cout << "Array processing test now concluded. Exiting program .....\n";
float time=(double)(clock() - tStart)/CLOCKS_PER_SEC;
cout <<" Execution Time: " << time<<"\n";
return 0;
}
void readFile(int *a, int s)
{
int i,p;
ifstream myfile;
myfile.open("scores.txt");
for(i=0;i<s;i++)
{
if(myfile >>p)
a[i]=p;
else cout <<"\nThere is some problem with the file. Cannot Read Full data\n";
}
}
void printHighest(int *a, int s)
{
int i,j;
int h=0,k=0;
for(i=0;i<s;i++)
{
k=a[i];
for(j=i+1;j<s;j++)
{
if(a[j]>k)
k=a[j];
}
if(k>h) h=k;
}
cout << "\nHighest Score: "<< h<<"\n";
}
void printLowest(int *a, int s)
{
int i,j;
int l=0,k=0;
l=a[0];
for(i=0;i<s;i++)
{
k=a[i];
for(j=i+1;j<s;j++)
{
if(a[j]<k)
k=a[j];
}
if(k<l) l=k;
}
cout << "\nLowest Score: "<< l<<"\n";
}
void average(int *a, int s)
{
int i,j;
float l=0;
int k=0;
for(i=0;i<s;i++)
{
l=l+a[i];
}
cout << "\nAverage Score: "<< (float)l/s<<"\n";
}
void printOneScore(int *a, int s)
{
int i,r;
int k=0;
cout<<"\n Enter row#: ";
cin >> r;
if(r<1 || r>s)
cout << "\n Invalid Row. \n";
else
{
k=a[r-1];
cout <<"Value at row#"<<r<<": "<< k<<"\nValues Geater than "<<k<<"\n";
for(i=0;i<s;i++)
{
if(a[i]>k)
cout << a[i]<<"\n";
}
}
}
int readMenu()
{
int option;
cout<< "\n-------------------------------------\n1-D Array Processing Menu Options\n-------------------------------------\n";
cout << "\n1. Read in 10 scores from user. \n2. Read in 10 scores from the file, scores.txt.\n3. Print all scores. \n4. Print the highest score.";
cout << "\n5. Print the lowest score. \n6. Print the average score. \n7. Print one score (give its entry number)\n8. Quit program.\n\nEnter OPtion: ";
cin >> option;
return option;
}
The scores.txt file is just a list of numbers that would be used to make the program run. I was wondering if you could help me figure out why the program wont give me the proper output and just 0 for each menu option