Solved

User name and password

Posted on 2006-07-11
4
205 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 use strings and some functions related to them in the C programming language.

743 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

14 Experts available now in Live!

Get 1:1 Help Now