1

In this program for storing high scores I want the user to input a player's name and high score in a single line, for example "eric 87". After the user enters the last player's name and score, it should then list all at once the scores that were entered. I don't know how to do this when splitting strings like "eric 97". Thanks very much for any help!

const int MAX = 20;
static void Main()
{
    string[ ] player = new string[MAX];
    int index = 0;

    Console.WriteLine("High Scores ");
    Console.WriteLine("Enter each player's name followed by his or her high score.");
    Console.WriteLine("Press enter without input when finished.");

    do {
        Console.Write("Player name and score: ", index + 1);
        string playerScore = Console.ReadLine();
        if (playerScore == "")
            break;
        string[] splitStrings = playerScore.Split();
        string n = splitStrings[0];
        string m = splitStrings[1];


    } while (index < MAX);

    Console.WriteLine("The scores of the player are: ");
    Console.WriteLine("player \t Score \t");

  //  Console.WriteLine(name + " \t" + score);
    // scores would appear here like:
    // george 67
    // wendy 93
    // jared 14
1
  • I'm assuming there's a reason you aren't asking for name and score separately(aka the easy/lazy option)? Commented Dec 14, 2012 at 3:57

1 Answer 1

3

Looking at your code you didn't put your player array to use. However, I would suggest a more object oriented approach.

public class PlayerScoreModel
{
    public int Score{get;set;}

    public string Name {get;set;}
}

Store the player and scores in a List<PlayerScoreModel>.

And when the last user and score has been entered.. simply iterate through the list.

 do {
        Console.Write("Player name and score: ", index + 1);
        string playerScore = Console.ReadLine();
        if (playerScore == "")
            break;
        string[] splitStrings = playerScore.Split();
        PlayerScoreModel playerScoreModel = new PlayerScoreModel() ;

        playerScoreModel.Name = splitStrings[0];
        playerScoreModel.Score = int.Parse(splitStrings[1]);
        playerScoreModels.Add(playerScoreModel) ;

    } while (somecondition);

   foreach(var playerScoreModel in playerScoreModels)
   {
      Console.WriteLine(playerScoreModel.Name +" " playerScoreModel.Score) ;
    }

Provide error checking as necessary.

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

Comments

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.