Hello all... I'm having a few problems with my program. I will list the problems that I need help with..

1) I need help printing out the highest testScore (I'm am getting garbage numbers)
2) I need help printing out the person's name in my input file who has the highest testScore
3) Before I get to the highestTest function, I am able to print everything fine, but when I get to this function, I get stuck with how to print out everything.

As you can see in my code, I keep opening my output file in each function, and I don't know if I'm suppose to do that, but it was the only way I can get things to print out. I currently can only get print last name, first name, testScore, and lettergrade to print out. When I get into the last 2 functions, I am unable to get the highest testScore (I'm getting garbage numbers) when I print it out and I'm unable to print the person's name who has the highest testScore.

If you have any questions, please ask.


Example of my input file:

Kay, Harry 95
Barnes, Mary 88
Cartwright, Matt 75
Downes, Ryan 80
Fields, Whitney 69


Here is my code:

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

using namespace std;

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


void init (studentType students[]);
void assignGrades (studentType students[]);
void highestTest (studentType students[]);
void printHighest (studentType students[]);

int main()
{
    
  studentType students[5];
  

  init(students);
  assignGrades(students);
  highestTest(students);
  printHighest(students);
  

 
 system ("PAUSE");
 return 0;   
}

void init (studentType students[])
{

     ifstream inData;
     ofstream outData;
     
     inData.open("Unknown.txt");
     outData.open("Unknown_output.txt");
     
    for (int i = 0; i < 5; i++)
    { 
    inData>>students[i].studentLName;
    inData>>students[i].studentFName;
    inData>>students[i].testScore;
    }
    
    for (int i = 0; i < 5; i++)
    {
    outData<<students[i].studentLName<<" ";
    outData<<students[i].studentFName<<" ";
    outData<<students[i].testScore<<" ";
    outData<<endl;
    }
    
    inData.close();
    outData.close();  
}


void assignGrades (studentType students[])
{

     ofstream outData;

     outData.open("Unknown_output.txt");
     
     
     char grade;
     
     
     for (int i = 0; i < 5; i++)
     {
     if (students[i].testScore >= 90 && students[i].testScore <= 100)
        {
        students[i].grade = 'A';
        }
        
     else if (students[i].testScore >= 80 && students[i].testScore < 90)
        { 
        students[i].grade = 'B';
        }
     else if (students[i].testScore >= 70&& students[i].testScore < 80)
        {     
        students[i].grade = 'C';
        }
     else if (students[i].testScore >= 60&& students[i].testScore < 70)
         {
        students[i].grade = 'D';
        }
     else if (students[i].testScore <= 59)
          {
        students[i].grade = 'F';
          }
        } 
        
        for (int i = 0; i < 5; i++)
        {
             outData<<students[i].studentLName<<" ";
             outData<<students[i].studentFName<<" ";
             outData<<students[i].testScore<<" ";
             outData<<students[i].grade;
             outData<<endl;    
        }
    
     outData.close();
}

void highestTest (studentType students[])
{
     
     int highest;
     
     ofstream outData;

     outData.open("Unknown_output.txt");
     
     for (int i = 0; i < 5; i++)
     {
     if (students[i].testScore > highest)
     
     highest = students[i].testScore;
     }

    outData.close();
     
     
     
}

void printHighest (studentType students[])
{
     
     int highest;
     
     ofstream outData;

     outData.open("Unknown_output.txt");
     
        for (int i = 0; i < 5; i++)
        {
        outData<<students[i].studentLName<<" ";
        outData<<students[i].studentFName<<" ";
        outData<<students[i].testScore<<" ";
        outData<<students[i].grade;
        outData<<endl;
        }
         outData<<"Highest grade: "<<highest;
         outData<<endl;
     
     for (int i = 0; i < 5; i++)
     {
         if (students[i].testScore > highest)
         {
         outData<<"Student with highest test score is: "<<students[i].studentFName<<" ";
         }
     }

     
     outData.close();
     
}

Recommended Answers

All 2 Replies

Your function highestTest() does not initialise the variable "highest", but the first operation involves comparing its value with elements of the array. That means the value it ends up with in that function will probably be junk.

The printHighest() function declares its own local variable named highest (again uninitialised so it contains junk) and prints it out.

The variable highest within printHighest() also has no relationship to the variable named highest in highestTest(): although they happen to have the same name, they are both local to the particular functions.

If you have any questions .... think first before you ask. You might be able to answer them yourself, and you will be better off if you can.

Thanks for your response.

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.