Thread: Help with 2-D character array

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    Question Problems with 2-D character array

    Code:
    ...
    stdID[1000];   //1-D array that stores a 8-digi Student ID
    StdName[1000][50];   //2-D array that stores Student Name
    ...
    Is there any ways that enable me to check whether user enters a correct information for each array. That is, warns user if he enters character in stdID field? Moreover, each student ID must be formed by 8-digi, so how can I check the user inputs if he really keys in complete 8 digi? Is it checking the position of '\0'?

    One more, how can I find the end of the array, that is the amount of coloums of an array that have been used. (You may want to know why I want to know this...It's because, I want to continue to add some more data to an existing array that has been sotred some other data before.)

    Sorry to ask such stupid question yet I'm new in C++. Thx for anyone!
    Last edited by choykawairicky; 05-13-2004 at 10:37 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    What you want to do is often called data validation. The two basic ways I know of to handle this is to never accept input in any other form than a string. Then analyze the string to be sure it only contains those things that it should. This will need to be done manually, as there are no prewritten functions to do this for you, given every input is different. Ther are a number of functions that can be used in writing your own validation protocol, however.

    Alternatively, you can use member functions of the istream class like fail(), clear(), ignore(), etc. They won't tell you why input failed, bu they can detect some types of failure, like putting in a letter instead of a digit, etc.

    To determine how many elements are in an array you can keep track of them as you go along or you can use the sizeof operator to determine the size of the entire array and the size of each element of the array. Then a simple division will give you the number of elements in the array. Most of the string classes have a member function to give you this information, if the array is a string.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    11
    Quote Originally Posted by elad
    Alternatively, you can use member functions of the istream class like fail(), clear(), ignore(), etc. They won't tell you why input failed, bu they can detect some types of failure, like putting in a letter instead of a digit, etc.
    Um...I'm not quite sure how to use this standard functions to do the data validation check, could you raise some example, thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM