Thread: Structures and Arrays - C++ Help

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    4

    Structures and Arrays - C++ Help

    I am working on the following code, however I cannot figure out how to find the highest score and how to get it to output as well as the name that goes with it. For some reason for highest score I get zero and for students having the highest score I get a blank. Any help would be great!

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include "RomanHeading.h"
    
    
    
    
    using namespace std;
    
    
    
    
    const int STUDENTS = 24;
    
    
    
    
    struct studentType
    {
        string studentFName;
        string studentLName;
        int testScore;
        char grade;
    };
    
    
    
    
    void getData(ifstream& inFile, studentType sList[], int listSize);
    void calculateGrade(studentType sList[], int listSize);
    int highestScore(const studentType sList[], int listSize);
    void printResult(ofstream& outFile, const studentType sList[], int listSize);
    
    
    
    
    int main()
    {
        getHeading("Assignment 10 - Working with Structures");
    
    
        ifstream inData;
        ofstream outData;
        studentType studentList[STUDENTS];
        inData.open("Ch9_Ex2Data.txt");
        if (!inData)
        {
            cout << "The input file does not exist. Program terminates!"
                << endl;
            system("PAUSE");
            return 1;
        }
    
    
    
    
        outData.open("Ch9_Ex2Out.txt");
        if (!outData)
        {
            cout << "Cannot open the output file. Program terminates!"
                << endl;
            system("PAUSE");
            return 1;
        }
    
    
    
    
        getData(inData, studentList, STUDENTS);
        calculateGrade(studentList, STUDENTS);
        printResult(outData, studentList, STUDENTS);
        getClosing();
        return 0;
    }
    
    
    
    
    void getData(ifstream& inFile, studentType sList[], int listSize)
    {
        for (int i = 0; i < listSize; i++)
            inFile >> sList[i].studentFName >> sList[i].studentLName
            >> sList[i].testScore;
    }
    
    
    
    
    void calculateGrade(studentType sList[], int listSize)
    {
        for (int i = 0; i < listSize; i++)
            if (sList[i].testScore >= 90)
                sList[i].grade = 'A';
            else if (sList[i].testScore >= 80)
                sList[i].grade = 'B';
            else if (sList[i].testScore >= 70)
                sList[i].grade = 'C';
            else if (sList[i].testScore >= 60)
                sList[i].grade = 'D';
            else
                sList[i].grade = 'F';
    
    
            
    }
    
    
    int highestScore(const studentType sList[], int listSize)
    {
        double maxscores = 0;
    
    
        for (int i = 0; i < listSize; i++) 
        {
    
    
            if (sList[i].testScore > maxscores)
            {
                maxscores = sList[1].testScore;
            }
    
    
        }
    
    
        return 1;
    }
    
    
        
        void printResult(ofstream& outFile, const studentType sList[], int listSize) 
        {
            double maxscores = 0;
            int i = 0;
    
    
            cout << setw(15) << "\n\n  Student Name "
                 << setw(20) << "  Test Score"
                 << setw(7) << "  Grade" << endl;
            cout << "  ------------ " << setw(21) << " ---------- " << setw(7) << " ----- " << endl;
            for (i = 1; i < listSize; i++)
                cout << left << setw(25)
                << "  " + sList[i].studentLName + ", " + sList[i].studentFName
                << right << " " << setw(5) << sList[i].testScore
                << setw(8) << "  " << sList[i].grade << endl;
            
            cout << endl << "  Highest Test Score: " << maxscores << endl;
            cout << "  Students having the highest test score:" << endl;
            for (i = 1; i < listSize; i++)
                if (sList[i].testScore == maxscores)
                    outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
    
    
            outFile << "\n\n  Mayra Roman " << endl;
            outFile << "  C++ Programming 1 " << endl;
            outFile << "  Assignment 10 - Working with Structures " << endl;
            outFile << setw(15) << "\n  Student Name "
                << setw(20) << "Test Score"
                << setw(7) << "Grade" << endl;
            outFile << "  ------------ " << setw(21) << " ---------- " << setw(7) << " ----- " << endl;
            for (i = 1; i < listSize; i++)
                outFile << left << setw(25)
                << "  " + sList[i].studentLName + ", " + sList[i].studentFName
                << right << " " << setw(5) << sList[i].testScore
                << setw(8) << "  " << sList[i].grade << endl;
            
            outFile << endl << "  Highest Test Score: " << maxscores << endl;
            outFile << "  Students having the highest test score:" << endl;
            for (i = 1; i < listSize; i++)
                if (sList[i].testScore == maxscores)
                    outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
            outFile << "\n  Program ended " << getDate() << " at " << getTime() << endl;
            outFile << "  Press the Enter key to continue.  " << endl;
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (sList[i].testScore > maxscores)
    > maxscores = sList[1].testScore;
    It's easy to get them mixed up sometime.


    > return 1;
    Also, don't you think you should be returning the maxscore?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Structures and Arrays
    By lostinprogramC+ in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2011, 09:39 AM
  3. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  4. Arrays and Structures
    By Sherina in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2009, 10:02 PM
  5. Arrays in structures
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2002, 11:54 AM