[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

User name and password

Posted on 2006-07-11
4
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

656 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