?
Solved

User name and password

Posted on 2006-07-11
4
Medium Priority
?
221 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 375 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

589 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