Thread: Array of strings & displaying them...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    Array of strings & displaying them...

    I'm new to C++ am having problems with this..it is a small part of an assignment. I need to store employee id numbers in an array of strings..in the format LNNN(letter, number, number,number) then I need to have a function to check each character of the string..to make sure it is the right format..then I need to display the contents of the array later in the program.. I've searched through the forums and have tried modifying my code..but it hasn't worked..Here is my code..

    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <string.h>
    
    bool validID(char id []);
    
    int main (void)
    {
    	char id[5];
    	int count=0;
    	const int MAX=3;
    
    	for (count; count<MAX;count++)
    	{
    	cout<<"\n""Enter id: ";
    	cin.getline(id,5);
    	cin.ignore();
    
    	if (validID(id))
    		cout<<"Valid id."<<endl;
    	else
    	{
    		cout<<"Invalid id."<<endl;
    	}
    
    		cout<<id[count];
    	}
    	return 0;		
    }
    
    bool validID (char id[])
    {
    int length;
    
    length=strlen(id);
    if (length>4)
    return false;
    
    for (int count=0; count<1; count++)
    {
    	if (!isalpha(id[count]))
    		return false;
    }
    
    for (count=1; count<4; count++)
    {
    	if (!isdigit(id[count]))
    		return false;
    }
    
    return true;
    }
    When I run this I get using a123, b123, c123 as input

    Enter id: a123
    valid id.
    a->>should display id # a123

    Enter id: b123
    valid id.
    1->>should display id # b123

    Enter id:c123
    Valid id.
    2->>should display id # b123

    What am I doing wrong?? Any help is greatly appreciated!

    lms

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    What I would do is move that for statement so tha it just encapsulated the cout statement like:
    for(int count = 0, count < (max +1), count++)
    cout<<id[count];

    Note: you can now remove the count definition from above:
    int count = 0;

    Hope this helps.

    Edit: Are you trying to do this 3 times? If so create a second for statement for the cout like demonstrated above.
    Last edited by Traveller; 05-05-2002 at 10:39 PM.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    6

    Array of strings & displaying them...

    Traveller,

    Mabye this will be clearer as to what I want to do..If I input a123,b123,c123...then it should display it

    Code:
    #include <iostream.h>
    
    
    bool validID(char id []);
    
    int main (void)
    {
    	char id[5];
    	int count=0;
    	const int MAX=3;
    
    	for(count=0;count <MAX; count++)
    	{
    	cout<<"Enter id: ";
    	cin.getline(id,5);
    	cin.ignore();
    	}
    
    
    	for (count=0;count<MAX;count++)
    	{
    	cout<<id[count]<<endl;
    	}
    	return 0;		
    }
    But when I run this piece..it outputs c 1 2..

    When I want it to output a123, b123, c123.. so I'm obviously missing something..

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    well your variable can only hold 5 characters and you want them to hold more...
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    Try using a multi. array like this:

    #include <iostream.h>


    bool validID(char id []);

    int main (void)
    {
    char id[5][2]; //You'll have to change your other functions
    int count=0;
    const int MAX=3;

    for(count=0;count <MAX; count++)
    {
    cout<<"Enter id: ";
    cin.getline(id[][count],5);
    cin.ignore();
    cout<<endl;
    }


    for (count=0;count<MAX;count++)
    {
    for(int times = 0, times <4, times++)
    cout<<id[times][count];
    cout<<endl;
    }
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    bool validID(char id []);
    
    int main (void)
    {
       char id[5];
       int count=0;
       const int MAX=3;
       char ids[MAX][5];
       int flag;
    
       for (count; count<MAX;count++)
       {
            //set flag to 0 for each new count
            flag == 0;
    
            //keep requesting user input until id is valid
            while(flag == 0)
             {
                 cout<<"\n""Enter id: ";
                 cin.getline(id,5);
                 cin.ignore();//won't hurt, but not needed.  best  
                                    //used before call to getline, not after. 
     
                  //check if id is valid
                  if (validID(id))
                  {
                      cout<<"Valid id."<<endl;
                      ids[count] = id;
                      cout << ids[count]  << " saved to ids" << endl;
                      flag = 1;
                   }
                   else
                   {
                        cout<<"Invalid id.  Try again."<<endl;
    	}
    
    	//cout<<id[count];
               }//end while
         }//end for
         return 0;		
    }
    
    bool validID (char id[])
    {
        int length;
    
        length=strlen(id);
        if (length>4)
        return false;
    
       for (int count=0; count<1; count++)
      {
    	if (!isalpha(id[count]))
    		return false;
      }
    
       for (count=1; count<4; count++)
      {
    	if (!isdigit(id[count]))
    		return false;
      }
    
      return true;
    }
    Last edited by elad; 05-06-2002 at 11:58 AM.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    6
    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <string.h>
    
    bool validID(char []);
    
    int main (void)
    {
       char id[5];
       int count=0;
       const int MAX=3;
       char ids[MAX][5];
       int flag;
    
       for (count; count<MAX;count++)
       {
            //set flag to 0 for each new count
            flag = 0;
    
            //keep requesting user input until id is valid
            while(flag == 0)
             {
                 cout<<"\n""Enter id: ";
                 cin.getline(id,5);
                 cin.ignore();//won't hurt, but not needed.  best  
                                    //used before call to getline, not after. 
     
                  //check if id is valid
                  if (validID(id))
                  {
                      cout<<"Valid id."<<endl;
                      ids[count]==id;
                      cout << ids[count]  << " saved to ids" << endl;
                      flag = 1;
                   }
                   else
                   {
                        cout<<"Invalid id.  Try again."<<endl;
    	}
    
    	//cout<<id[count];
               }//end while
         }//end for
         return 0;		
    }
    
    bool validID (char id[])
    {
        int length;
    
        length=strlen(id);
        if (length>4)
        return false;
    
       for (int count=0; count<1; count++)
      {
    	if (!isalpha(id[count]))
    		return false;
      }
    
       for (count=1; count<4; count++)
      {
    	if (!isdigit(id[count]))
    		return false;
      }
    
      return true;
    }
    Ok I ran this..and it is error checking correctly..but it prints out garbage for ids[count]..?..I'll continue to fiddle wqith it..thanks for the help though..any suggestions are welcome..

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    6

    Talking

    I got it!! For those who are interested...

    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <string.h>
    
    const int MAX = 3;
    bool validID(char [][10]);
    void assignNames(char [][10]);
    void main(void)
    {
       char names[MAX][10];
      //get ids'
       for (int count = 0; count < MAX; count++)
       {
           cout << "Enter name: ";
           cin >> names[count];
       }
      //display ids
       for (count = 0; count < MAX; count++)
       {
           cout <<"Name " << count + 1 << 
           " is " << names[count] << "\n";
        }
       //ids valid or not
       for (count=0;count<MAX;count++)
    	  {
    	   if (validID(names))
    		   cout<<"Valid."<<endl;
    	   else
    		   cout<<"Invalid"<<endl;
       }
    	   for (count = 0; count < MAX; count++)
       {
           cout <<"\n""Name " << count + 1 << 
           " is " << names[count] << "\n";
        }
    
    }
    
    
    bool validID(char id[][10])
    {
    	
    	int length,y;
    	for (int x=0; x<MAX; x++)
    	{
        length=strlen(id[x]);
        if (length>4)
        return false;
    	}
       for ( x=0,y=0; y<1, y!='\0'; y++ )
       {
    	    if (!isalpha(id[x][y]))
    		return false;
      }
    
       for ( x=1,y=1; x<MAX, y<4; y++ )
      {
    	if (!isdigit(id[x][y]))
    		return false;
      }
    
    return true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  3. Replies: 2
    Last Post: 11-08-2006, 12:29 PM
  4. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM
  5. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM