0

I'm being all spacey and tired right now on this homework assignment. The issue I'm having is that I cannot seem to call the function/method to the Main method so it can be executed. For some reason the array won't pass. I have to pass the arrays by reference (which I don't seem to be having any issues with) and the average score by value. Any assistance would be greatly appreciated.

Edit: So I appear to have fixed it..kinda. But I am having an issue with the last function; DisplayBelowAverage.

I am receiving 2 errors: Argument1: Cannot convert from 'refString[] to 'ref string' and The best overloaded method match for Lab5_exA.Program.DisplayBelowAverage(ref string, ref double[],ref double, ref int)' has some invalid arguments.

I cannot seem to find where the issue is occurring. It is referring to my array: playerName when I select the errors but I can't see an issue anywhere besides maybe the fact that "ref string" in the first error doesn't have the array symbols "[]" as the other array displays.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab5_exA
{
class Program
{

    public static double InputData(ref string[] playerName, ref double[] playerScore, ref int players, ref double totalScore)
    {
        do
        {
            Console.Write("Enter the player name or type 'Q' or 'q' to quit.."); //get player name
            playerName[players] = Console.ReadLine();

            if (playerName[players] == "Q" || playerName[players] == "q") //checks for exit value
            {
                break;
            }

            Console.WriteLine("Enter the player's score.."); //get player score
            playerScore[players] = Convert.ToDouble(Console.ReadLine());

            totalScore += playerScore[players];

            players++; //establish size of array

        } while (true);
        return 0;
    }

    public static double DisplayPlayerData(ref string[] playerName, ref double[] playerScore, ref int players)
    {
        for (int w = 0; w < players; w++)
        {
            Console.WriteLine("Player: " + playerName[w] + "\t" + "Score: " + playerScore[w]);
        }

        return 0;
    }

    public static double CalculateAverageScore(ref int players, ref double totalScore, ref double averageScore)
    {
        averageScore = totalScore / players;
        return averageScore;
    }

    public static double DisplayBelowAverage(ref string playerName, ref double[] playerScore, ref double averageScore, ref int players)
    {
        int z = 0;

        Console.WriteLine("Average Score: " + averageScore);
        Console.WriteLine("The following players received a score below the average score..");
        Console.WriteLine("Name \t Score");
        Console.WriteLine("---- \t ----");

        while (z < players)
        {
            int y = 0;

            if (playerScore[y] < averageScore)
            {
                Console.WriteLine(playerName[y] + "\t" + playerScore[y]);
            }
        }
        return 0;
    }

    static void Main(string[] args)
    {
        string[] playerName = new string [100];
        double[] playerScore = new double [100];
        int players = 0;
        double averageScore = 0;
        double totalScore = 0;

        InputData(ref playerName, ref playerScore, ref players, ref totalScore);
        DisplayPlayerData(ref playerName, ref playerScore, ref players);
        CalculateAverageScore(ref players, ref totalScore, ref averageScore);
        DisplayBelowAverage(ref playerName, ref playerScore, ref averageScore, ref players);

        Console.Read();

    }
}

}

4
  • 1
    Remove the [] before playerName in the call to InputData. The other arrays you're passing don't have it.. why is that one special? Also - not sure why you're being asked to specifically pass them by reference.. you don't seem to be re-assigning the arrays in the methods (that I can see).. so there's no need. Commented Jun 8, 2014 at 1:21
  • I am now being told that "Program.InputData(ref string[], ref double[], ref int, ref double)" has some invalid arguments... Commented Jun 8, 2014 at 1:32
  • Why are you using ref everywhere? Commented Jun 8, 2014 at 3:34
  • I don't understand what you mean. I'm fairly new to C# programming. But I have been taught that when passing by reference you need to use the ref keyword must be used to pass by reference the memory location of the data. Commented Jun 8, 2014 at 16:30

1 Answer 1

1
public static double DisplayBelowAverage(ref string playerName, ref double[] playerScore, ref double averageScore, ref int players){

replace by

public static double DisplayBelowAverage(ref string[] playerName, ref double[] playerScore, ref double averageScore, ref int players){

You are using it like and array and passing an array, but the method declaration needs the square brackets.

Sign up to request clarification or add additional context in comments.

1 Comment

God damnit. I knew it was going to be something stupid...Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.