views:

14

answers:

0

I am having two major problems with the source code below (and probably other problems I haven't yet discovered...).

Problem Number 1: I have to apply the declared integer fields (day, month, year) under the class Friend to a string array in my Main() method. The way that I wrote the source code below doesn't allow for a definition for 'x' because it has to be outside of the for() loop in order to use it later in the program. This is generating the syntax error CS0103 "The name 'x' does not exist in the current context." I also tried declaring x (ie, int x;), but of course that doesn't work since x is already being used separately in the first for() loop. The integer fields must be assigned to the variables in the array because this assignment is the basis for other follow-on assignments in which they are used for searching the array (and because the assignment says so).

Problem Number 2: Before I wrote the integer fields into the source code, and tried to assign them to specific parts of the array; I tried compiling the program to make sure that everything else was working correctly before making it anymore complicated...

The program worked, up until it asked for the variables for the sixth friend... The program would then skip to the second loop, print the contents of the array that had been entered as directed by the second loop, and would then crash. I'm guessing that I'm not specifying the width of the array correctly within the loop. In other words, the program is reading "listArray[x, 3] = Console.ReadLine()" etc incorrectly. It seems like the for() loop thinks that the array is a box (5 x 5) as opposed to a rectangle (8 x 5).

The second for() loop (used to print the contents of the array) was working fine (the / / marks for the three remaining array columns showed up, but were empty of values since the first for() loop skipped them). To be honest, I haven't been able to find a for() loop example that applies user-input to a multi-dimensional array. I had to guess on how that would be applied to my array as the only example I found was for a regular one-dimension array (of course my text book doesn't cover applying user input to arrays at all...).

To be concise, I guess the two questions I'm asking are:

1) How do I apply a class's integer fields to a string array under the Main() method correctly so that they can be identified as equal to specific values in that array and can subsequently be used to represent those values later in the program?

and...

2) What is the correct syntax used in a for() loop that is applying user-input variables to a multi-dimensional array (as opposed to the obviously incorrect syntax used in my source code below)?

I am writing this program in response to the assignment quoted below:

"Create a class named Friend. Its fields include a Friend’s name, phone number, and three integer fields that together represent the Friend’s birthday—month, day, and year. Write a program that declares an array of eight Friend objects and prompts the user to enter data about eight friends. Display the Friend objects in alphabetical order by first name. Save the program as FriendList.cs."

978-1-111-05918-7, Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell - © Cengage Learning

Here is my error-plagued source code:

using System;

public class Friend

{

   //Step 1: Declare the three integer fields for the friend's birthdays

   private int day {get; set;}

   private int month {get; set;}

   private int year {get; set;}

   public static void Main()

   {
      //Step 2: Declare the array of 8 friends (with 5 user-input values listed for each friend)

      string[,] listArray = new string[8, 5];

      //Step 3: Create a loop that assigns user-input string variables to the array

      for(int x = 0; x < listArray.Length; ++x)
      {       
         Console.Write("Enter a friend's first name: ");
         listArray[x, 0] = Console.ReadLine();

         Console.Write("Enter this friend's phone number: ");
         listArray[x, 1] = Console.ReadLine();

         Console.Write("Enter the two-digit day on which this \nfriend was born (for example, if your \nfriend was born on the 1st of August, enter 01): ");
         listArray[x, 2] = Console.ReadLine(); 

         Console.Write("Enter the two-digit month in which this \nfriend was born (for example, if your \nfriend was born in January, enter 01: ");        
         listArray[x, 3] = Console.ReadLine();

         Console.Write("Enter the four-digit year in which this \nfriend was born (for example, 1971): ");         
         listArray[x, 4] = Console.ReadLine();

         Console.WriteLine("\n \n");

         if(x == 7)
            break;



      }     

//Step 4: Convert the day, month, and year values of the array into integers using the class's integer fields declared in step 1

      Friend mc = new Friend();
      mc.day = Convert.ToInt32(listArray[x, 2]);
      mc.month = Convert.ToInt32(listArray[x, 3]);
      mc.year = Convert.ToInt32(listArray[x, 4]);





      //Step 5: Sort the array alphabetically

      Array.Sort(listArray);

      //Step 6: Use a "for" loop to print the array's values in a nice looking format.

      Console.WriteLine("========================================\n");
      Console.WriteLine("Name          Number          Birthday");
      Console.WriteLine("---------------------------------------");

      for(int y = 0; y <=listArray.Length; ++y)
      {
         string s1 = listArray[y, 0];
         string s2 = listArray[y, 1];

         Console.WriteLine("{0, -14}{1, -16}{2}/{3}/{4}", s1, s2, mc.day, mc.month, mc.year);
      }
   }
}