Hi,
My assignment is to read in data from a file(first name, last name, and grade) and then output it. i have my code and i beleive the problem lies within the function readData, but i cant figure out what it is. I run the program and i get an error saying it must be shut down, however i also get the "press any key to continue" in the box. i end up with a blank output txt file. does anyone have any idea?

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

using namespace std;

const int numStudents = 20;

struct studentType
{
 string studentFName;
 string studentLName;
 int testScore;
 char grade;
};

void readData(ifstream&, studentType person[]);
void assignGrade(studentType person[]);
int findHighest(studentType person[]);
void print(ofstream&, studentType person[]);

 

int main()
{
 ifstream inFile;
 ofstream outFile;
 int students = 20;

 inFile.open("c:\\chap11.txt");
 if (!inFile)
 {
  cout<<"not working";
  return 1;
 }

 outFile.open("c:\\chap11output.txt");
 
 studentType person[numStudents];

 readData(inFile, person);
 assignGrade(person);
 findHighest(person);
 print(outFile, person);


 inFile.close();
 outFile.close();

 return 0;
}


void readData(ifstream& in, studentType person[numStudents])
{

 for (int index=0; index < numStudents; index++)
 {
  in>>person[numStudents].studentFName>>person[numStudents].studentLName>>
  person[numStudents].testScore;
 }
}

void assignGrade(studentType person[numStudents])
{
 

 for (int y = 0; y < numStudents; y++)

 {
  if (person[numStudents].testScore > 90)
 person[numStudents].grade = 'A';

 else 
  if ((person[numStudents].testScore > 80) && (person[numStudents].testScore < 90))
 person[numStudents].grade = 'B';
 
  else
   if ((person[numStudents].testScore > 70) && (person[numStudents].testScore < 80))
  person[numStudents].grade = 'C';

    else
    if (person[numStudents].testScore > 60 && person[numStudents].testScore < 70)
   person[numStudents].grade = 'D';

    else 
     person[numStudents].grade = 'F';

 }

 

}

int findHighest(studentType person[])
{
 int max = 0;

 for (int r = 1; r < numStudents; r++)
  if (person[r].testScore < person[max].testScore)
   max = r;

  return max;
}

void print(ofstream& out, studentType person[numStudents])
{

 for (int h=0; h < numStudents; h++)
  out<<person[numStudents].studentLName<<","<<person[numStudents].studentFName<<"  "<<person[numStudents].grade;

}

please use code tags so as to make your code look readable...in you code i noticed a mistake that reads into a file u need this

inFile>> /*contents*/

line 59: use index variable instead of numstudents. in>>person[index].studentFName>>person[index].studentLName>>

Thank you, that fixed the problem. now the only issue i am left with is although it writes to a file, the findHighest function, is outputting the number of the index position, as opposed to the actual number itself. how can i fix this? (example: it outputs "14", which is where the highest grade of "95" is, instead of actually 95 itself)

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

using namespace std;

const int numStudents = 20;

struct studentType
{
 string studentFName;
 string studentLName;
 int testScore;
 char grade;
};

void readData(ifstream&, studentType person[]);
char assignGrade(studentType person[]);
void print(ofstream&, studentType person[]);

 

int main()
{
 ifstream inFile;
 ofstream outFile;
 

 inFile.open("c:\\chap11.txt");
 if (!inFile)
 {
  cout<<"not working";
  return 1;
 }

 outFile.open("c:\\chap11output.txt");
 
 studentType person[numStudents];

 readData(inFile, person);
 assignGrade(person);
 findHighest(person);
 print(outFile, person);


 inFile.close();
 outFile.close();

 return 0;
}


void readData(ifstream& in, studentType person[numStudents])
{

 for (int index=0; index < numStudents; index++)
 {
  in>>person[index].studentFName>>person[index].studentLName>>
  person[index].testScore;
 }
}

char assignGrade(studentType person[numStudents])
{
	for (int y = 0; y < numStudents; y++)

 {
  if (person[y].testScore > 90)
    person[y].grade = 'A';

 else 
  if ((person[y].testScore > 80) && (person[y].testScore < 90))
   person[y].grade = 'B';
 
  else
   if ((person[y].testScore > 70) && (person[y].testScore < 80))
 person[y].grade='C';

    else
    if (person[y].testScore > 60 && person[y].testScore < 70)
  person[y].grade = 'D';

    else
		person[y].grade= 'F';
 }

 return person[y].grade;

}

int findHighest(studentType person[])
{
int max = 0;

 for (int r = 1; r < numStudents; r++)
 	 if (person[max].testScore < person[r].testScore)
	
		 max = r;
	 
	 cout<<max<<"LOOK";
	 

	 return max;
 
}


void print(ofstream& out, studentType person[numStudents])
{
int h=0;
int highScore;

for (h; h < numStudents; h++)

	 out<<person[h].studentLName<<","<<person[h].studentFName<<"   "<<person[h].grade<<endl;

 highScore= findHighest(person);
out<<"\nThe highest test score was: "<<highScore<<endl;


 
 out<<"\n\nThe students with the highest score: \n";
	
		for (h= 0; h < numStudents; h++)
		if (person[h].testScore == highScore)
		out<<person[h].studentLName<<","<<person[h].studentFName<<endl;
	 
}

you are outputing the array position by using max..change line 93 to something like this

int max = person[0];

you need to also change from line 95

for (int r = 1; r < numStudents; r++)
       if (person[r].testScore >max)
 
	max = person[r].testScore;
 
	 cout<<max<<"LOOK";
 
 
	 return max;
 
}

I can't have max become an array b/c arrays cant be returned. however i changed the code in line 95 like you told you and it outputs a random score in the listing, not 95 which is the highest.

your not making max an array...your assigning the value in "person[0]" to max..the logic is supposed to be you assume the 0th element is the highest hence the

max=person[0];

did you try doing the exact thing and you got an error??

The error i get when using max=person[0] is " error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct studentType' (or there is no acceptable conversion)"

have you tried replacing the "int" with "studentType"

I think the original findHighest() code was working quite well, to get the
actual highest score, simply use person[max].testScore once the index of the
highest score is known (i.e. max in this case), so ...

int findHighest(studentType person[])
{
	// assume highest score is at index 0
	int max = 0;

	for (int r = 1; r < numStudents; r++)
	{
		if (person[max].testScore < person[r].testScore)
		{
			max = r;
			// currently highest scored at index max now
		}
	}

	// Tell what we got: the index and the actual score
	cout << "highest score is at index: " 
		<< max 
		<< " and the score is: " 
		<< person[max].testScore;

	// return the index which can be used to subscript the person array
	return max; 
}

The assignment only asks for 4 types of studentType, so since i already used those i have to figure the way around it.

Thank you SO much mitrmkar!!!!! it works! And thank you also josh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.