#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) 


{

    char bname[97],number[34],code[22], name[10],bcate[10],pass,book,author,ID,school,course ;   
    char type, address[5],x,department,quantity,choice,date[10],aname[20],btitle[50], bcode[9] ;
    int getch();
    int num1,num2,num3,num4,num5,num6,num7,num8,op,a, ph[20],num,i=0;
    int input[10];







    cout<<"\t\tWELCOME TO OUR LIBRARY"<<endl;
    cout<<""<<endl;
    cout<<"Enter Username"<<endl;
    cin>>name;
    cout<<""<<endl;
    cout<<"ENTER PASSWORD"<<endl;

                   for(num=0;;)
      {
         a=getch();

         if((a>='a' && a<='z')||(a>='A' && a<='Z')||(a>='1' && a<='9'))
         {
               ph[num]=a;
               cout<<"*";
               ++num;
               }

         if(a=='\r')
         {
           ph[num]='\0';
           break;
           }      
           }

           if(pass=='input')
            {
                  cout<<"PASSWORD ACCEPTED"<<endl;
            }
 if(pass=='else') 
 {
    cout<<"Login rejected"<<endl;
 }
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;


     cout<<"\t\tOUR SERVICES IS AS FOLLOWS"<<endl;

        cout<<""<<endl;
        cout<<"\t\t1.Enter a new book"<<endl;
        cout<<""<<endl;
        cout<<"\t\t2.create student record"<<endl;
        cout<<""<<endl;
        cout<<"\t\t3.Buy Book"<<endl;
        cout<<""<<endl;
        cout<<"\t\t4.Find Book"<<endl;
        cout<<""<<endl;
        cout<<"\t\t5.Display Books"<<endl;
        cout<<""<<endl;
        cout<<"\t\t6.Borrow books"<<endl;
        cout<<""<<endl;
    cout<<"\t\t7.TO RETURN BOOK"<<endl;
     cout<<""<<endl;
     cout<<"\t\t8.DONATE BOOKS"<<endl;

    cout<<""<<endl; 

    cout<<"\t\tSelect An Option"<<endl;
        cin>>input[10];





    if(input[10]==1)
    {
    cout<<"\t\t1.NEW BOOK ENTRY"<<endl; 
    cout<<""<<endl;
    cout<<"\t\t*PLEASE FILL THE FORM AND PRESS 'ENTER' AFTER EVERY INPUT*"<<endl;    
    cout<<""<<endl;

    cout<<"Enter Book Title"<<endl;
    cin>>btitle;
    cout<<""<<endl;

    cout<<"Enter Author name"<<endl;
    cin>>aname;
    cout<<""<<endl;

    cout<<"Enter Book code"<<endl;
    cin>>bcode;
    cout<<""<<endl;

    cout<<"Category of book"<<endl;
    cin>>bcate;


    cout<<"\nNEW BOOK ENTRY SUCCESSFUL"<<endl;
}


if(input[10]==2)

cout<<"\t\tEnter ID number"<<endl;
            cin>>ID;
            cout<<""<<endl;
            cout<<"\t\tEnter  Name"<<endl;
            cin>>name;
            cout<<""<<endl;
        cout<<"\t\tEnter school name or institution name"<<endl;
        cin>>school;
        cout<<"\t\tEnter address"<<endl;
        cin>>address;

        cout<<""<<endl;
         cout<<"\t\tcourse title"<<endl;
         cin>>course;
         cout<<"\t\t\nRECORD CREATED SUCCESSFULLY!!"<<endl;

    if(input[10]==3)
    {   
    cout<<"WELCOME PURCHASE A BOOK"<<endl;  
         cout<<"ENTER YOUR NAME"<<endl;
          cin>>name;
          cout<<"ENTER CARD NUMBER"<<endl;
          cin>>number;

    cout<<"ENTER CARD NAME"<<endl;
    cin>>name;
          cout<<"ENETR BOOK NAME YOU WANT TO BUY"<<endl;          
          cin>>bname;

          cout<<"ENTER BOOK CODE"<<endl;
          cin>>code;

          cout<<"ENTER ADDRESS"<<endl;
          cin>>date;

          cout<<"QUANTITY YOU WANT BUY"<<endl;
          cin>>quantity;

          cout<<"THANKS FOR PURCHASING"<<endl;
          }

            if(input[10]==4)
            {
    cout<<"\t\tEnter book name"<<endl;
            cin>>name;
            cout<<""<<endl;
            cout<<"\t\tOR"<<endl;
            cout<<""<<endl;
        cout<<"\t\tEnter Author name"<<endl;
        cin>>name;
        cout<<"\t\tOR"<<endl;
         cout<<""<<endl;
         cout<<"\t\tInput book code"<<endl;
         cin>>code;
         }


        if(input[10]==5)
        {           
            cout<<"Linguistics"<<endl;
        cout<<""<<endl;
        cout<<"MANAGEMENT STUDIES"<<endl;
        cout<<""<<endl;
        cout<<"PROGRAMMING METHOD"<<endl;
        cout<<""<<endl;
        cout<<"MORPHOLOGY"<<endl;
        cout<<""<<endl;
        cout<<"HISTORY"<<endl;
        cout<<""<<endl;
        cout<<"SPORTS LEADERSHIP"<<endl;
        cout<<""<<endl;
        cout<<"Social Studies"<<endl;
        cout<<""<<endl;
        cout<<"Mathematics"<<endl;
        cout<<""<<endl;
        cout<<"Coaching"<<endl;
        cout<<""<<endl;
        cout<<"Resaerch Method"<<endl;
        cout<<""<<endl;
        cout<<"Sex Education"<<endl;
        }

       if(input[10]==6)
       {                                    
            cout<<"ENTER YOUR NAME"<<endl;
          cin>>name;                    
          cout<<"ENTER DEPARTMENT"<<endl;
          cin>>department;      
    cout<<"ENTER COURSE NAME"<<endl;
    cin>>name;
     cout<<""<<endl;
          cout<<"ENETR BOOK NAME YOU WANT BORROW"<<endl;          
          cin>>bname;
           cout<<""<<endl;
          cout<<"ENTER BOOK CODE"<<endl;
          cin>>code;
         cout<<""<<endl;
          cout<<"ENTER ADDRESS"<<endl;
          cin>>date;
           cout<<""<<endl;
          cout<<"ENTER PHONE NUMBER"<<endl;
          cin>>number;
           cout<<""<<endl;
          cout<<"ENTER DATE OF TAKING BOOK"<<endl; 
          cin>>date;
          cout<<"*BOOK SHOULDNT BE TAKEN FOR MORE THAN 1WEEK*"<<endl;
           cout<<""<<endl;
           cout<<"DATE OF RETURN TO BE GIVEN TO YOU AT THE RECEPTION"<<endl;
           }


            if(input[10]==7)
            {                               
                cout<<"ENTER YOUR NAME"<<endl;
          cin>>name;          
          cout<<"ENETR BOOK NAME YOU WANT TO RETURN"<<endl;          
          cin>>bname;
           cout<<""<<endl;
          cout<<"ENTER BOOK CODE"<<endl;
          cin>>code;
         cout<<""<<endl;
           cout<<""<<endl;
          cout<<"ENTER DATE BOOK WAS BORROWED"<<endl; 
          cin>>date;
          cout<<"ENTER DATE OF RETURNING BOOKS"<<endl;
          cin>>date;
           cout<<""<<endl;
           cout<<"PLEASE SIGN A BOOK AT THE RECEPTION BEFORE LEAVING"<<endl;
           }


          if(input[10]==8)
          {       
                 cout<<"Please enter your name"<<endl;
             cin>>name;

              cout<<""<<endl;
               cout<<"please enter your address"<<endl;
               cin>>address;

               cout<<"Please enter book type(s) you wnat to donate"<<endl;
               cin>>type;

               cout<<""<<endl;
                cout<<"Please enter quantity you would like to donate"<<endl;
                 cin>>quantity; 

                 cout<<""<<endl;
                cout<<"Please leave the book(s) at the library reception"<<endl;
                      cout<<""<<endl;

                cout<<"THANK YOU FOR DONATING!:)"<<endl;          




}

system("Pause");
    return 0;   
}

Hi guys this is the code i wrote for a library,i have tried adding the password feature to it but when i input a wrong password and username it still takes me to the program instead rejecting it and if i input the correct one too it still takes me to the program, i would like you guys to help me solve the problem, like when i input a wrong password it shouldnt open the program it should reject the login and when i input the correct one it should it should accept and give me access to the program. Thank you.

Recommended Answers

All 5 Replies

This entire block is bad:

                   for(num=0;;)
      {
         a=getch();
         if((a>='a' && a<='z')||(a>='A' && a<='Z')||(a>='1' && a<='9'))
         {
               ph[num]=a;
               cout<<"*";
               ++num;
               }
         if(a=='\r')
         {
           ph[num]='\0';
           break;
           }      
           }
           if(pass=='input')
            {
                  cout<<"PASSWORD ACCEPTED"<<endl;
            }
 if(pass=='else') 
 {
    cout<<"Login rejected"<<endl;
 }

At the least, you are not terminating when the password is rejected, but are falling through to the rest of the program. This may be better:

      for(num=0;num < 20;num++)
      {
         a=getch();
         if((a>='a' && a<='z')||(a>='A' && a<='Z')||(a>='1' && a<='9'))
         {
               ph[num]=a;
               cout<<"*";
         }
         else if(a=='\r')
         {
           ph[num]='\0';
           break;
         }
      }
      if(strcmp(ph,"input") == 0)
      {
         cout<<"PASSWORD ACCEPTED"<<endl;
      }
      else
      {
         cout<<"Login rejected"<<endl;
         exit(-1);
      }

Note that the pass variable is a single character. You need to test ph instead of pass for pass/fail. Also, you are writing most of this in C code, not C++... Not good for a passing grade I think.

To start with, I recommend that you be more careful about formatting your code; as it is, the indentation is all over the place, and inconsistent as hell. Now, you are free to choose how you style your code, but you definitely should apply some style to it, and be consistent with it. Readability is of high importance, especially when asking for help on a message board like this one. Most modern editors have an auto-indent feature that you can use to simplify this, or you can use a stand-alone program such as Astyle, which many IDEs use for formatting (and has several different styles from which to choose).

What are the variables

num1,num2,num3,num4,num5,num6,num7,num8

declared for, but never used, and given the naming, why didn't you declare them as a single array or vector<int>, rather than a series of unrelated (but similarly named) variables? I'm guessing that they - and the names given them - is an artifact of the development process, but if so, why are they still here?

Also, you are writing most of this in C code, not C++... Not good for a passing grade I think.

I agree, though we've had plenty of students come here and say that their instructors had forbidden or reuired some particular approach, especially towards string handling. Still, it is a good point: for most if not all of what you are doing here, using a string object would be the preferred approach over using legacy C-strings. For a more detailed discussion, you might want to see this recent thread, whic coincidentally is also regarding a password validator.

Speaking of which: as explained in that thread, you really want to separate the validator out of main() into its own function, and pass the password string to be validated to that independent of the code for reading the password. I would actually recommand two functions, one for reading in the password, and the other for validating it. This follows what is called the principle of 'separation of concerns', and the related 'Single Responsibility Principle', which both recommend that the code (functions, classes, whatever) that the implements one aspect of the program (e.g., input/output, validation, computating values, etc.) should handle only that part, and that each part should be independent of (i.e., loosely coupled to) of the other parts, even if they are connected.

Thanks guys, but am not really clear on the vector<int>..Could you Please explain Futher for me?

The Standard Template Library vector template class is an alternative to a fixed-size array; it is basically a dynamically-resizing array that can be parameterized to use any given type you want to work with. It overloads the array index operator, so it looks like an array most of the time, as well. It is useful when you don't know how large an array will need to be ahead of time (which is more often than not).

For example, instead of an array of ints like this:

int A_big_enough_array[100];

you could have

std::vector<int> vectors_are_always_big_enough = new vector<int>();

which would always be just big enough.

technical footnote:
Actually, the vector object initially allocates a fixed block of memory for its internal array, and tracks the actual number of elements in the container automagically. When it exceeds that size - or goes under it by a certain amount - it re-allocates the array according to an algorithm (just what that isn't is not important most of the time).

To add a new element to the vector, you would use the insert() or push_back() member functions. The insert() method lets you insert an element at an arbirary location; the push_back() method always adds the element to the end of the vector.

Thanks bro am very grateful for the help.

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.