5 Replies - 936 Views - Last Post: 30 November 2011 - 06:18 AM Rate Topic: -----

#1 starstruck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 05:12 AM

Hi there, I have a project where we are required to create a 2D array and store Names/address's based on userinput.
Basically an address book
One of the requirements is that the array is filled with 'XXXX' and the program needs to search for xxxx and replace with for instance name. This isnt the problem pretty basic,

The problem is where on the first run it will add a name to the first index, the second run it wont add any, and only sometimes when I search the array the second name will be in the 3rd array index. Just wondering if anyone can shed some light on why my code isn't adding name to the second array index yet sometimes it will add it to 3rd, others not at all.

  for (int a = 0; a < AddressBook.Length /3 ; a = 1+ 1) 
                       {
                           if (AddressBook[a, 0] == "XXXX")
                           
                               AddressBook[a, 0] = fullname;
                           a++;
                           Console.WriteLine("would you like to continue /1");
                           answer = Console.ReadLine();
                           if (answer == "Y")
                           {
                               break;
                           }



thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: c# address book using 2D array ~ need help :)

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 05:39 AM

uh yeah first why a should be smaller then AddressBook.Length /3?. other thing is that in your for you make "a" to be equal to 2 a=1+1. i suggest you to look again how to make a proper for cycle. if you're going to use for then you dont need to increase "a" in your code below:
AddressBook[a, 0] = fullname;
//If you're using for cycle this is useless
a++;



other thing that i notice is that when you ask if the user wants to continue if he choice yes "Y" then you exit the cycle maybe that is why it dont add anything after the first run?
Console.WriteLine("would you like to continue /1");
answer = Console.ReadLine();
if (answer == "Y")
{
//Here you exit the for cycle??? why when the user wants to continue
     break;
}


This post has been edited by NoBrain: 30 November 2011 - 05:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 starstruck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

Re: c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 05:50 AM

Hiya thanks for replying;

The reason for the 'for loop' /3 is because I want a record to be 3 dimensions down, ie
record 1 2 3 4
name name name name
town
road

With the A++ I was just trying anything that was hoping to work, I figured that A is not incrementing.

Im wonderin that is the problem because the program searches for 'XXXX'
and then if it is not true, ie sam in the first index, it does nothing?


Also when the user selects Y instead of the program closing it returns to the title menu


thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 06:04 AM

but that is not how to make a proper for cycle to loop your array. you need to search all elements of the first dimension and then you should check if the second dimension contains name or XXXX(AddressBook[0,0], AddressBook[1,0],AddressBook[2,0] etc).

give it a try and post it here if it isn't working and we will be happy to guide you further
Was This Post Helpful? 0
  • +
  • -

#5 starstruck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

Re: c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 06:12 AM

Ah right ok excuse me i'm kinda new to c# and especially very new to arrays, not sure how to do what you suggested but nevermind i'll try to figure it out. thanks
Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: c# address book using 2D array ~ need help :)

Posted 30 November 2011 - 06:18 AM

the part of learning is to try it out. a hint is that you should loop all elements from the first dimension. check what properties array have. google how to use a for cycle. if you have troubles with your code that you have tried then feel free to post them here again and we will be happy to help you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1