dimanche 28 février 2021

Functions referencing arrays not giving any output

My code is simply not giving me any output when I try to use the functions. The code compiles correctly, and everything works except calling functions. I am compiling in Unix using the command g++ -std=c++11 Golf.cpp (this is required, I cannot change how the file can be compiled). I have tried filling in the parameters when I go to execute the function, but that is proving more harmful than good. Here is the code:

/*Name:Zayn Severance
Date: 2/28/2021
Section: COP3363
Assignment: Module 4 assignment 2
DueDate: 2/28/2021
About this project: Calculate scores for golf.
Assumptions: Assumes correct user input.

All work below was performed by Zayn Severance*/

#include <iostream>
#include <algorithm>
int getValidScore()
{
    bool isScoreValid = false;
    int scoreCheck = 0;
    while (isScoreValid == false)
    {

        std::cout << "Enter in a positive number greater than 0...";
        std::cin >> scoreCheck;
        if (scoreCheck >= 18 && scoreCheck <= 300)
        {
            isScoreValid = true;
        }
        else
        {
            isScoreValid = false;
        }
    }
}

int getValidPlayer()
{
    bool isNumGreater0 = false;
    int playerCheck = 0;
    std::cout << "Please enter in the score ...";
    while (isNumGreater0 == false)
    {
        std::cin >> playerCheck;
        if (playerCheck >= 1 && playerCheck <= 3)
        {
            isNumGreater0 = true;
        }
        else
        {
            isNumGreater0 = false;
            std::cout << "Please enter in a valid score (18...300) ...";
        }
    }
    return playerCheck;
}

void displayScores(int* scores, std::size_t NUM_PLAYERS)
{
    std::cout << "Display Scores\n\tPlayer 1\tPlayer2\tPlayer3\n\t";
    for (int i = 0; i < 3; ++i)
    {
        std::cout << scores[i] << "\t";
    }
}

void ChangeAScore(int *scores, std::size_t NUM_PLAYERS)
{
    int playerFinder = getValidPlayer();
    playerFinder = playerFinder - 1;
    int scoreFinder = getValidScore();
    scores[playerFinder] = scoreFinder;
}

void displayPlayerLowestScore(int* scores, std::size_t NUM_PLAYERS)
{
    int temp = scores[0];
    int winning = 0;
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        if (temp > scores[i])
        {
            winning = i;
            temp = scores[i];
        }
    }
    winning = winning + 1;
    std::cout << "The player with the lowest score is " << winning;
}

int main() {
    const int NUM_PLAYERS = 3;
    int scores[NUM_PLAYERS] = { 118,98,68 };
    int options = 0;
    //if options is 4 the loop ends, and the program stops
    while (options != 4) {

        int options;
        std::cout << "1) Display the scores\n2) Change a score\n3) Display player with the lowest score\n4) Quit\nSelect an option (1..4)..";
        std::cin >> options;
        int num = 0;
        //Display Scores
        if (options == 1) 
        {
            displayScores;
        }
        //Change Scores
        else if (options == 2) 
        {
            ChangeAScore;
        }
        //
        else if (options == 3) 
        {
            displayPlayerLowestScore;
        }
        //
        else if (options == 4)
        {
            std::cout << "Bye.\n";
            return 0;
        }

        //In case you miss the instructions and put in a letter
        else if (!std::cin) 
        {
            std::cout << "That was not a number. Invalid input.\n";
            std::cin.clear();
            std::cin.ignore(1024, '\n');
            options = 0;
        }
        //Any other input
        else 
        {
            std::cout << "Invalid input.\n";
            options = 0;
        }
    }
    return 0;
}

If anyone could tell me my mistake (I assume it's small, I'm quite new) I would be grateful.

Aucun commentaire:

Enregistrer un commentaire