0

I am new to structs in C# and I am stuck and can not pass the values in a file back to struct and successfully read them again.

The initialisation values work fine, but the file will not load at all

This is what I have so far:

    public struct Card
    {
        public char suit;//'C'- Clubs, 'D'-Diamonds,'H'- Hearts
        //and 'S'- Spades

        public int value;//2-14 – for 2-10, Jack, Queen, King, Ace
    }

    static void Main(string[] args)
    {

        const int CARDS_IN_HAND = 5;//number of cards in hand

        Card[] hand = new Card[CARDS_IN_HAND];//array of cards

        InitialiseArray(hand);
        Console.WriteLine("-------------\n");
        Console.WriteLine("Initialised Values in Array");
        DisplayHandData(hand);
        Console.ReadLine();

The issue seems to be below

        LoadArray(hand);//load values into array for use

        //check values loaded to array
        Console.WriteLine("-------------\n");
        Console.WriteLine("Actuals Values in Array from file");
        DisplayHandData(hand);
        Console.ReadLine();

    }
    public static void InitialiseArray(Card[] data)
    {
        for (int count = 0; count < data.Length; count++)
        {
            data[count].suit = 'C';
            data[count].value = 0;
        }
    }

    public static void LoadArray(Card[] data)
    {
        string fileName = "FullHouse.txt";//name of file for menu item 3
        //data in file = C 13 H 13 D 13 C 10 H 10
        string input = fileName;

        List<Card> cards = new List<Card>();
        //string input = fileName;

        StreamReader inFile = new StreamReader(fileName);//open file
        input = inFile.ReadLine();//priming read for array

        string[] inputArray = input.Split(new char[] { ' ' },
            StringSplitOptions.RemoveEmptyEntries);

        //input loading data into array
        for (int i = 0; i < 10; i += 2)
        {
            Card newCard = new Card();
            newCard.suit = inputArray[i][0];
            newCard.value = int.Parse(inputArray[i + 1]);
            cards.Add(newCard);
        }
        inFile.Close();
    }

    public static void DisplayHandData(Card[] data)
    {
        Console.WriteLine();
        //test values loaded into array in correct positions
        for (int records = 0; records < data.Length; records++)
        {
            Console.WriteLine(data[records].suit);
            Console.WriteLine(data[records].value);
        }
        Console.WriteLine("-------------\n");
        Console.ReadLine();

    }

Any help would be greatly appreciated.

3
  • You seem to be from a C background. Instead of using a char for the types of cards, consider using an enum. Commented Sep 12, 2015 at 14:05
  • Do you have an example on how this would look? Commented Sep 12, 2015 at 14:11
  • You can read about enums at : msdn.microsoft.com/en-us/library/… Commented Sep 12, 2015 at 22:46

3 Answers 3

1

Your LoadArray should work just fine. The problem is probably that you are filling in variable cards, which is a local List<Card>, but what you probably want to do is fill in the data parameter.

Here is my sugestion for altering the for-cycle in LoadArray method

for (int i = 0; i < data.Length; ++i)
{
    Card newCard = new Card();
    newCard.suit = inputArray[i * 2][0];
    newCard.value = int.Parse(inputArray[i * 2 + 1]);
    data[i] = newCard;
}

But be careful about this approach. It is quite fragile. If there will be a mismatch in the length of data and inputArray, you will get an exception (if inputArray will be shorter than expected). You should check the length before the for-cycle.

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

Comments

1

Your LoadArray() method isn't changing the contents of the array passed in to it.

I recommend using a List in your main method instead, and change LoadArray() to:

public static List<Card> LoadArray()
{
    string fileName = "FullHouse.txt";//name of file for menu item 3
    //data in file = C 13 H 13 D 13 C 10 H 10
    string input = fileName;

    List<Card> cards = new List<Card>();
    //string input = fileName;

    StreamReader inFile = new StreamReader(fileName);//open file
    input = inFile.ReadLine();//priming read for array

    string[] inputArray = input.Split(new char[] { ' ' },
        StringSplitOptions.RemoveEmptyEntries);

    //input loading data into array
    for (int i = 0; i < 10; i += 2)
    {
        Card newCard = new Card();
        newCard.suit = inputArray[i][0];
        newCard.value = int.Parse(inputArray[i + 1]);
        cards.Add(newCard);
    }
    inFile.Close();

    return cards;
}

Then you shouldn't need the InitialiseArray() method at all.

Instead you can do:

var hand = LoadArray();

Of course, then you might want to rename LoadArray() to something like LoadHand().

1 Comment

thank you Matthew, I am stuck on how to check the values returned to 'var hand'. in the main, sorry I am new to c#
0

You can check, the value using Console.WriteLine(hand.Count) this will give you the size, and you can display your data in hand some like this

foreach(var h in hand)
{
   Console.WriteLine(h.suit);
   Console.WriteLine(h.value);
}

if you wish you can try this too.

hand.ForEach(h=> 
{
   Console.WriteLine(h.suit);
   Console.WriteLine(h.value);
});

Edited: Sorry, Matthew; I should put this as a comment under your answer. :-)

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.