Thread: help on simple program

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question help on simple program

    i was wondering if yas could help me with this simple code that i can't get around. There are 2 problems. First i dont know how to ask the password in a way so that ****** will come up on the screen when being typed in. Second of all, i noticed that after the third attempt at the password, if you enter it incorrect, the password will be accepted by passing progress onto the next statement, where i would prefer if the program exited? Heres the code. cheers !

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    int password(void);

    int main()
    {
    int choice; //variable called choice of type integer
    password(); //Calls password function

    cprintf("\nAccess accepted !");
    getch();
    }
    int password(void)
    {

    //Variables
    char passwd[10];
    int count, correct = 0;

    for(count=0;count<3;count++) /*Give user three attempts*/
    {
    clrscr(); //Call the clear screen function
    gotoxy(21,8);
    cprintf("**** PASSWORD IS CASE SENSITIVE ****");
    gotoxy(26,10);
    cprintf("Please Enter Password : ");
    cscanf("%s",passwd);
    if(strcmp("GOD", passwd)==0)
    {
    count=3; //Breaks out of for loop
    correct = 1; //Allows user into system
    }
    else
    {
    gotoxy(25,12);
    correct = 0; //Does not allow user into system
    textcolor(WHITE); //Sets font colour to white
    cprintf("Incorrect Password Entered!!!");
    gotoxy(29,13);
    cprintf("%d attempt(s) left",3-(count+1));
    gotoxy(25,14);
    cprintf("Press any key to continue....");
    getch(); getch();
    }
    }
    return correct;
    }

  2. #2
    Unregistered
    Guest
    I think you need to assign the "int" returned from password() to a variable in main and then use an "if" statement to examine the return value and do an "exit" if the user didn't "pass" the password test. Of course, you would also then need to modify the password() function to return a value other than "correct" if the user reaches 3 tries. Hope that helps.

  3. #3
    Unregistered
    Guest
    You don't necessarily need another variable to hold the return value from the password() function.

    int main()
    {
    int choice; //variable called choice of type integer

    if (password()) //Calls password function
    {
    cprintf("\nAccess accepted !");
    }
    else
    {
    cprintf("invalid password");
    exit(1) // or some value that is appropriate
    }

    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM