-2

The program for my class needs to return the average score, the high score with first and last name as well as the low score. I get an error for the functions that find the names for the high and low score and I'm not sure what the issue is.

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

using namespace std;

float highestVal(float highScore, float score);
float lowestVal(float lowScore, float score);

string highFirstFun(float highScore, float score, string firstNameHigh);
string highLastFun(float highScore, float score, string lastNameHigh);
string lowFirstFun(float lowScore, float score, string firstNameLow);
string lowLastFun(float lowScore, float score, string lastNameLow);



int main() {

    ifstream inFile;
    string fileName = "", firstName = "", lastName = ""; 
    float score, average;
    float highScore = 0;
    float lowScore = 100;
    float studentNum = 0;
    float sum = 0; 
    int i;
    string highFirst = "", highLast = "";
    string lowFirst = "" , lowLast = "";
    cout << "Please enter the input file name: "; 
    cin >> fileName;
    inFile.open(fileName);

    while (!inFile.is_open()) {
        cout << "Sorry, the file did not open. \nTry again, or type \"cancel\" to quit: ";
        cin >> fileName; 
        if (fileName == "cancel") {
            cout << "Cancelling..." << endl; 
            break; 
        }
        else {
            inFile.open(fileName); 
        }
    }

    if (inFile.is_open()) {
        cout << "The scores are as follows: \n"; 
        while (!inFile.eof()) {
            inFile >> firstName >> lastName >> score; 
            cout << firstName << " " << lastName << " scored " << score << endl;
            sum += score;  
            studentNum++;
            highScore = highestVal(highScore, score);
            lowScore = lowestVal(lowScore, score);
            highFirst = highFirstFun(highScore, score, firstName);
            highLast = highLastFun(highScore, score, lastName);
            lowFirst = lowFirstFun(highScore, score, firstName);
            lowLast = lowLastFun(highScore, score, lastName);
        }

        average = (sum / studentNum);
        cout << "There are " << studentNum << " students with an average grade of " << average << endl;
        cout << "The high score was: " << highScore << " from " << highFirst << " " << highLast << endl;
        cout << "The low score was: " << lowScore << " from " << lowFirst << " " << lowLast << endl;
        inFile.close();
    }
    return 0;
}


float highestVal(float highScore, float score)
{
    if (score > highScore) {
        highScore = score; 
    }
    return (highScore);
}

float lowestVal(float lowScore, float score)
{
    if (score < lowScore) {
        lowScore = score;
    }
    return (lowScore);
}

string highFirstFun(float highScore, float score, string firstNameHigh)
{
    if (score > highScore) {

        return (firstNameHigh);
    }
}

string highLastFun(float highScore, float score, string lastNameHigh)
{
    if (score > highScore) {

        return (lastNameHigh);
    }
}

string lowFirstFun(float lowScore, float score, string firstNameLow)
{
    if (score < lowScore) {

        return (firstNameLow);
    }
}

string lowLastFun(float lowScore, float score, string lastNameLow)
{
    if (score < lowScore) {

        return (lastNameLow);
    }
}

Here is the Text File that it is pulling from called "Scores.txt":

John   Smith   99.0
Sarah Johnson  85.0
Jim Robinson  70.0
Mary Anderson  100.0
Michael Jackson 92.0

Any help would be much appreciated!

Zane R.
  • 13
  • 3
  • 4
    "I get an error for the functions that find the names for the high and low score and I'm not sure what the issue is." please provide exact error, and reduce your question to a [example]. If part of your code is working, get rid of that from the question. It just makes it harder for us to focus on the part that's broken. Only have the broken part, and the bare minimum amount of surrounding code required to duplicate the error. – JohnFilleau Mar 22 '20 at 22:46
  • 3
    For example, if you're not having any trouble with input, get rid of the part of your code that reads input and replace it with static values in the code. – JohnFilleau Mar 22 '20 at 22:47
  • if the functions *highFirstFun* etc if the test is false you do not return a value, but the code suppose it does, **the behavior is undefined** – bruno Mar 22 '20 at 22:49
  • Does it even compile? highFirstFun does not return value in all code paths. – Lesiak Mar 22 '20 at 22:49
  • Fyi, read this: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). Related to your code, all of those "low" and "high" functions have no determinate result if your conditions buried within fail. That's a recipe for *undefined behavior*. This code should be *puking* warnings all over your screen. if it isn't research your compiler toolchain and determine how to turn up your warning levels, do so, then *fix them*. – WhozCraig Mar 22 '20 at 22:50
  • 1
    You need to fix all the warnings: [listed here](https://godbolt.org/z/i79n82). You have plenty of "_control reaches end of non-void function_" which makes your program have undefined behavior. – Ted Lyngmo Mar 22 '20 at 22:51

1 Answers1

-1

A function that has a return type must always return that type. The function

string highFirstFun(float highScore, float score, string firstNameHigh);

For example must always return a string when it is called. The issue is the definition of your function

string highFirstFun(float highScore, float score, string firstNameHigh)
{
    if (score > highScore) {

        return (firstNameHigh);
    }
}

highFirstFun() Will only return a string if (and only if) score is higher than highScore. Well, what will happen if score is not higher than highScore? This will be a problem for the function because it's not designed to do anything if a condition like this happens, so the compiler will complain about it. This applies to all your functions. To make them work, you'll have to find a way to make them return a string is all scenarios.

pctopgs
  • 347
  • 1
  • 8