Text File Isn't Working

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:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include<sys/types.h>

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

cout <<"\n File Read Complete";

}

void readScores(int *a, int s)
{
int i=0;

for(i=0;i<s;i++)
{
cout << "Enter score#"<<i+1<<": ";
cin >>a[i];
}

cout << "\nScores Read. Please select option 3 to view scores\n";

}
void printAllScores(int *a, int s)
{
int i=0;

for(i=0;i<s;i++)
{
cout << "Score#"<<i+1<<": "<< a[i]<<"\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
I wouldnt use a for loop here, it'd probably be better practice to use a while loop IMO.
I feel as if something went wrong here, but I could be wrong. It's displaying the 0s from when you initialized A
1
2
3
4
5
6
ifstream myfile;
myfile.open("scores.txt");
for(i=0;i<s;i++)
{
if(myfile >>p)
a[i]=p;
Hello katalinaisland,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button to the right of the text box) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second lint to be the most help.

I ran your program with the text file:
10 9 12 8 14 20 5 22 15 17
and had no problem. It did read the text file and let me print out all the scores. The other functions appeared to work OK too.

Karakuik's point of using the while loop is a good idea as it is what is common these days. The for loop works, but if yo have less than 10 numbers in the file it will be a problem.

The while loop could look something like this:
1
2
while (myFile >> p && i < s)
	a[i++] = p;
The "&& i < s" will keep the reading to 10 and keep you from trying to go past the boundary of the array. If there is less than ten scores in the input file that is not a problem because lhs of && will cause the while loop to end when there is nothing left to read. At this point you could return the "i" back to main and store it in size for later use.

I did have a problem with "i" not being initialized before it was first used. A simple way to write that is int i{}, p{} where the uniform initiler of the empty {}s will initialize the variables to zero. You can also put a number inside the {}s if needed.

I did not have any problem reading the file with your code unless your file is set up different than mine. Without seeing your file it is hadr to say right now.

I did not chece the otherr functions for correctness yet, but they appear to work OK.

Hope that helps,

Andy
Topic archived. No new replies allowed.