well, this is my code:

while(!isdigit(c) || num.length() < 4){
    cout << "Enter your four digit pin: ";
    cin >> num;
       
        for(i = 0;i < num.length(); i++){ 
                c = num.at(i);
                    if (isdigit(c)){
                         pin[i] = atoi(num.substr(i, 1).c_str());
                    }
        }
        
        if(!isdigit(c) || num.length() < 4){
            cout << "Invalid Pin." << endl;
        }
}

and this should be the output:

Enter your 4 digit pin number: qwerty

Invalid pin.

Enter your 4 digit pin number: 43x1

Invalid pin.

Enter your 4 digit pin number: 432

Invalid pin.
Enter your 4 digit pin number: 4321

Select Option ([L]ist Titles, [D]isplay Item, [A]dd Item, [Q]uit):

**but instead i get this:
Enter your 4 digit pin number: 43x1

Invalid Pin.
Select Option ([L]ist Titles, [D]isplay Item, [A]dd Item, [Q]uit):

**whenever i enter "43x1" it does show "Invalid Pin" but it still displays the menu options

how can i fix this?

Don't understand the while loop. You should do the digit check in the for loop. Also show the code where you write the 'menu'. This doesn't make much sense.....

Logic of above program is not correct.
In posted program while loop is not correct value of variable c is not known, and after for loop c will contain last value of string.
It should be like this.

bool isValidPin = false;

while(!isValidPin) {
    isValidPin = true;
    cout << "Enter Your 4 digit pin";
    cin >> num;

   for (int i = 0; i < num.length(); ++i) {
    char c = num.at(i);
    if (!isdigit(c)) {
      isValidPin = false;
      break;
   }
  }
  if(!isValidPin) {
   cout << "Invalid Pin";
  }
}
cout << "Enter your four digit pin: ";
    cin >> num;
       
        for(i = 0;i < num.length(); i++){ 
          c = num.at(i);
          if (isdigit(c)){
             pin[i] = atoi(num.substr(i, 1).c_str());
             }else{
                    cout << "Invalid Pin!" << endl;
             }
        }
    
do {         
     
     displayMenu();
     cin >> ans;
     cout << endl;
     ans = tolower (ans);
     
     switch (ans){
            case 'l':
                 listTitles();
                 break;
            case 'd':
                 displayItem(pin);
                 break;
            case 'a':
                 addNewItem(pin);
            case 'q':
                 break;
            default:
                 cout << "Invalid Option" << endl;
                break;                
                   
            }
}while(ans != 'q'); 

      return 0;
}

This will always show the menu even after an Invalid Pin, because you will always enter the do - while loop where displayMenu is the first function that will be called.
One way to fix this:

bool validPin = false;
while( !validPin )
{
  cout << "Enter your four digit pin: ";
  cin >> num;
  validPin = true;     
  for(i = 0;i < num.length(); i++){ 
    c = num.at(i);
    if (isdigit(c)){
       pin[i] = atoi(num.substr(i, 1).c_str());
       }else{
        cout << "Invalid Pin!" << endl;
        validPin = false;
    }
  }
}

Oh and you don't check for a 4-number digit anymore

thanks alot!!! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.