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
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..