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

namespace Lab5_A
{
    class Program
    {
        static void InputData(ref int counter, string[] playerName, int[] playerScore)
        {
            string aValue = null;
            Console.Write("Enter Player Name (Q to quit): ");
            playerName[counter] =Console.ReadLine();
            Console.Write("\nEnter score for {0}:", playerName[counter]);
            aValue = Console.ReadLine();
            playerScore[counter] = int.Parse(aValue);
            return;                      
            
        }
               
        static void Main(string[] args)
        {
        string[] playerName = new string[100];
        int[] playerScore = new int[100];
        string endProgram = "Q";
        int counter = 0;
        playerName[counter] = "temp";
          // while player name does not equal 'Q'
        {
            do
            {
                playerName[counter] = null;
                playerScore[counter] = 0;
                InputData(ref counter, playerName, playerScore);
                counter++;
            }
            while (playerName[count] != endProgram)


        }
        }
    }
}

Not sure the best way to check for the value 'Q' in a string.
I tried a few different lines of code, but I'm not sure of the "proper" C# way to do this... Obviously I'm talking about the while (playerName[count] != endProgram)
line.
Any coders want to help out a newbie? I checked both my textbook and internet sources, and I can't find the right way to do this.

Thanks.
Jim

Strings are a special case in C# and are treated like value types for equality comparison, so using == or != is fine. If you are testing for less than or greater than, you'll need to use String.Compare

Also, please remember that "q" != "Q". It may be wise to convert your input to upper case for the comparison.

You can use Equals ( ) method of string class to compare the value. The best way is to compare your string with either the upper case or lower case letter.

See the below modification :

while (!playerName[count].ToString().ToUpper().Equals(endProgram))

try by it and let me know...

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

namespace Lab5_A
{
    class Program
    {
        static void InputData(ref int counter, string[] playerName, int[] playerScore)
        {
            string aValue = null;
            Console.Write("Enter Player Name (Q to quit): ");
            playerName[counter] =Console.ReadLine();
            Console.Write("\nEnter score for {0}:", playerName[counter]);
            aValue = Console.ReadLine();
            playerScore[counter] = int.Parse(aValue);
            return;                      
            
        }
               
        static void Main(string[] args)
        {
        string[] playerName = new string[100];
        int[] playerScore = new int[100];
        string endProgram = "Q";
        int counter = 0;
        playerName[counter] = "temp";
          // while player name does not equal 'Q'
        {
            do
            {
                playerName[counter] = null;
                playerScore[counter] = 0;
                InputData(ref counter, playerName, playerScore);
                counter++;
            }
            while (playerName[count] != endProgram)


        }
        }
    }
}

Not sure the best way to check for the value 'Q' in a string.
I tried a few different lines of code, but I'm not sure of the "proper" C# way to do this... Obviously I'm talking about the while (playerName[count] != endProgram)
line.
Any coders want to help out a newbie? I checked both my textbook and internet sources, and I can't find the right way to do this.

Thanks.
Jim

This article has been dead for over six months. Start a new discussion instead.