Solved

User name and password

Posted on 2006-07-11
4
206 Views
Last Modified: 2010-04-15
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);
}
0
Comment
Question by:dandeliondream
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
bastibartel earned 125 total points
ID: 17082558
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
 
LVL 5

Expert Comment

by:bastibartel
ID: 17082589
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17082711
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
 
LVL 3

Author Comment

by:dandeliondream
ID: 17082938
Thanks to all for your valueable comments esp.bastibartel.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now