Thread: File report question, need advice..

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Ireland
    Posts
    13

    Question File report question, need advice..

    A Game maintains scores for its players in a comma delimited sequential file “scores.txt” with the record structure outlined below.

    G001,Hot Shot,280
    G002,Loose Cannon,750
    G003,FireFox,20

    scores.txt record structure
    Field Name Type
    Player Number String
    PlayerName String
    Score Integer

    A program is required that will allow them enter a player number, the program will then search the file, and display the players name, and the score for chosen player. If the player is not found display a message stating “No match found”. Alongside the score, you must display the citation, use the table below do to determine the citation.
    Table 1
    Score Citation
    Less than 400 Sluggish Snail
    between 400 and 599 Ambling Armadeillo
    Between 600 and 699 Bobbing Bobcat
    Between 700 and 999 Rocketing Rabbit
    Greater than 999 TurboCharged Cheetah

    Example dialogue
    Enter Player Number: G002

    Player name : Loose Cannon

    Score : 750

    You are a Rocketing rabbit

    Search Again (y/n) :

    My Attempt so far:

    Code:
    string[] scores = new string
    
    streamreader myfile = file.opentext("scores.txt");
    string lineIn;
    lineIn = myfile.Readline();
    
    Console.Write("Enter name : ");
    string target = Console.ReadLine();
    Am i on the right track??
    Anyone any ideas??

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    After opening the file, loop through each line, storing the player's data in a struct:
    Code:
    struct Player
    {
      public string Number;
      public string Name;
      public int Score;
    }
    Then as you read in each line of the file, store the data in a new instance of the struct:
    Code:
    List<Player> players = new List<Player>();
    while((linein = myfile.ReadLine()) != null)
    {
      Player player = new Player();
      string[] parts = linein.Split(',');
      player.Number = parts[0];
      player.Name = parts[1];
      player.Score = int.Parse(parts[2]);
      players.Add(player);
    }
    Then you can prompt the user for a name to search for and look it up in the players list:
    Code:
    Player foundPlayer = players.FirstOrDefault(p => p.Name == target);
    if(foundPlayer != null)
    {
      // Print out the player stats
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice reading lines from a text file.
    By Fujitaka in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 09:43 PM
  2. Replies: 2
    Last Post: 01-18-2005, 06:24 PM
  3. I keep seeing some advice.. (Header file related)
    By Lithorien in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2005, 04:31 AM
  4. Legal Advice (more like question)
    By muttskilicious in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-27-2003, 04:02 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM