User name and password

Hi,

This is my sch project. Right now, when user name and password matches the predefined one, user is able to proceed. However, when it doesn't match, the script is not showing the error msg. Hope someone can help.

#include <stdio.h>
#include <conio.h>
#include <string.h> //for using string manipulation like strcmp
#include<stdlib.h> //for ending program
#define cusername "sysadm" //correct user name
#define cpassword "musicace" //correct password

void mainmenu(void);// Go to Main Menu

void main(void)//function definition
{
      char username[10];
      char password[10];
      int try =0;
      int counter =3;
      clrscr();
      fflush(stdin);
      gotoxy(20,5);
      cprintf(":: :: :: Singapore Music Academy :: :: ::\n");
      gotoxy(23,7);
      cprintf("Student Database Login Page\n");
      gotoxy(20,15);
      cprintf("Enter Username: ");

      gotoxy(20,17);
      cprintf("Password: ");
      do
      {
            gotoxy(36,15);
            clreol();
            fflush(stdin);
            gets(username);
            //cscanf(" %s", &username);

            gotoxy(30,17);
            clreol();
            fflush(stdin);
            gets(password);
            //cscanf(" %s", &password);

            if (strcmp(username, cusername)==0 && strcmp(password, cpassword)==0)
            {
                  gotoxy(12,24);
                  cprintf("You have access rights. Please press any key to proceed.");
                  getch();
                  mainmenu();
            }
            else
            {
             if (try > 2)
                  {
                  gotoxy(20,20);
                  clreol();
                  cprintf("Access Denied. You have more %i trys", counter);
                  getch();
                  }try++;
                  counter--;
             }
      }while ((strcmp(username, cusername)==0 && strcmp(password, cpassword)==0) && try <3);

            if (try == 3)
            {
                  gotoxy(20,20);
                  cprintf("You have exceeded 3 trys");
                  cprintf("\nYou will be log out soon");
                  getch();
                  exit(0); //exit program
            }
}

void mainmenu(void)
{
clrscr();
printf("Main Menu");
exit(0);
}
LVL 3
dandeliondreamAsked:
Who is Participating?
 
bastibartelConnect With a Mentor Commented:
Hi dandeliondream,
> try

The variable 'try' is set to 0 and the following is never executed:

f (try > 2)
               {
               gotoxy(20,20);
               clreol();
               cprintf("Access Denied. You have more %i trys", counter);
               getch();
               }try++;
               counter--;
           }


Cheers!
Sebastian
0
 
bastibartelCommented:
Hi again

How about:
 
  bool fAccessOK = false;
  /* ... */

  if (strcmp(username, cusername)==0 && strcmp(password, cpassword)==0)
          {
               fAccessOK = true;
               gotoxy(12,24);
               cprintf("You have access rights. Please press any key to proceed.");
               getch();
               mainmenu();
          }
          else
          {
              gotoxy(20,20);
               clreol();
               cprintf("Access Denied. You have more %i trys", counter);
               getch();
               try++;
               counter--;
           }
     }
     while ((!fAccessOK)  && (try <3));

     /* ...*/
0
 
manish_regmiCommented:
please do not use gets. Its dangerous and its use is highly discouraged. Some compiler give warnings if you use it.

use fgets instead.

regards
Manish Regmi
0
 
dandeliondreamAuthor Commented:
Thanks to all for your valueable comments esp.bastibartel.
0
All Courses

From novice to tech pro — start learning today.