This is kind of long, but please help me with this code

This is a discussion on This is kind of long, but please help me with this code within the C Programming forums, part of the General Programming Boards category; Ok, I wrote this small program to get used to using functions of time (like time() and ctime()). Well it ...

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    This is kind of long, but please help me with this code

    Ok, I wrote this small program to get used to using functions of time (like time() and ctime()). Well it eventually built up to this medium size program that I wrote just for exercise purpose.

    The program has a menu system.

    1. Log In - Basically just writes to a log file that a username has logged in and writes the date/time they did
    2. Log Out - Basically just writes to a log file that a username has logged out and writes the date/time they did
    3. Change Password - Basically changes/creates a password.
    4. Exit - Exits the program.

    Now, everything is peachy keen, except for when I choose a menu option, do it, and then it returns to the menu loop, it loops through the menu TWICE. This is really annoying and I can't figure out why.

    Here's the code:
    Code:
    /*
    
      Programmed by biosx
      September 10, 2001
    
      newfile: Just an experiment with files and other functions
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    /* Function Prototypes */
    char *archive_pass(void);
    char *get_user(void);
    char *get_passwd(void);
    char *get_time(void);
    void log_user_in(char *name, char *time);
    void log_user_out(char *name, char *time);
    void passwdset(void);
    int menu(void);
    
    
    
    int main()
    {
        /* Variables used to store important values */
        static char *archive, *user, *passwd, *log_time;
        int i = 0;
    
        while (i != 4) {
    
            i = menu();
    
            switch(i) {
                case '1':
                    /* Get password from a password file */
                    archive = archive_pass();
                    user = get_user();
                    passwd = get_passwd();
                
                    /* If strcmp() function returns 1, -1, then password is wrong */
                    if (strcmp(passwd, archive)) {
                        printf("Password incorrect...\n");
                        system("PAUSE");
                        return 1;
                    }    
                    else {
                        /* Get time for use of logging */
                        log_time = get_time();
                        log_user_in(user, log_time);
                        printf("\n%s logged in at: %s\n", user, log_time);
                    }
                    break;
                case '2':                
                    log_time = get_time();
                    log_user_out(user, log_time);
                    printf("\n%s logged out at: %s\n", user, log_time);
                    break;
                case '3':
                    passwdset();
                    break;
                case '4':
                    printf("Exiting...\n");
                    return 0;
                    break; 
                }
              
        } 
                 
        system("PAUSE");
        return 0;
    }
    
    
    
    /* Gets a password from a passwd file then returns it a pointer to it */
    char *archive_pass(void)
    {
        char *c, *d;
        FILE *p;
    
        p = fopen("passwd.txt", "r");
        c = (char *)malloc(10 * sizeof(char));
         
        fscanf(p, "%s", c);    
    
        return c;
    }
    
    /* Gets user name from STDIN */
    char *get_user(void) 
    {
        char *c, *s;
    
        c = (char *)malloc(10 * sizeof(char));
    
        printf("User: ");
        scanf("%s", c);
    
        return c;    
    }
    
    /* Gets user password from STDIN */
    char *get_passwd(void)
    {
        char *c;
    
        c = (char *)malloc(8 * sizeof(char));
    
        printf("Password: ");
        scanf("%s", c);
    
        return c;
    } 
    
    /* Gets time from time() function then returns formatted value */
    char *get_time(void)
    {
        time_t m;
    
        m = time(NULL);
    
        return ctime(&m);
    }
    
    /* Logs a line in a log file of info on user */
    void log_user_in(char *name, char *time) 
    {
        FILE *p;
        p = fopen("log.txt", "a");
    
        fprintf(p, "%s logged in: %s\n", name, time); 
    }
    
    /* Logs a line in a log file on info on user */
    void log_user_out(char *name, char *time)
    {
        FILE *p;
        p = fopen("log.txt", "a");
    
        fprintf(p, "%s logged out: %s\n", name, time);
    }
    
    /* Edits passwd.txt file to change password */    
    void passwdset(void)
    {
        FILE *fp;
        char *passwd;
    
        passwd = (char *)malloc(10 * sizeof(char));
        
        fp = fopen("passwd.txt", "w");
    
        printf("Enter Value: ");
        scanf("%s", passwd);
    
        while (*passwd != '\0')
            putc(*passwd++, fp);
        
        fclose(fp);
    }
        
    
    int menu(void)
    {
        int c;
        printf("\nTestProg v1.0\n");
        printf("1. Log in.\n");
        printf("2. Log out.\n");
        printf("3. Reset Password.\n");
        printf("4. Exit Program.\n");
        printf(":> ");
        c = getchar();
    
        return c;
    }
    Please help me out. Thank you so much

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    c = getchar()
    You don't check to get the newline or what's passed
    the number the user entered. Then the next go around
    it's not in the case as i == '\n'.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Ok, so I changed the code a bit. I put in a check for the newline '\n' character. It doesn't do what it did before (which was print 2x copies of the menu after doing a command. However, now after I do a command, it no longer accepts characters for the menu.

    Here's the code:

    Code:
    /*
    
      Programmed by biosx
      September 10, 2001
    
      newfile: Just an experiment with files and other functions
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    /* Function Prototypes */
    char *archive_pass(void);
    char *get_user(void);
    char *get_passwd(void);
    char *get_time(void);
    void log_user_in(char *name, char *time);
    void log_user_out(char *name, char *time);
    void passwdset(void);
    int menu(void);
    
    
    
    int main()
    {
        /* Variables used to store important values */
        static char *archive, *user, *passwd, *log_time;
        int i;
    
        while (i != 4) {
            if (i != '\n') {
                i = menu();
            
                switch(i) {
                    case '1':
                        /* Get password from a password file */
                        archive = archive_pass();
                        user = get_user();
                        passwd = get_passwd();
                
                        /* If strcmp() function returns 1, -1, then password is wrong */
                        if (strcmp(passwd, archive)) {
                            printf("Password incorrect...\n");
                            system("PAUSE");
                            return 1;
                        }    
                        else {
                            /* Get time for use of logging */
                            log_time = get_time();
                            log_user_in(user, log_time);
                            printf("\n%s logged in at: %s\n", user, log_time);
                        }
                        break;
                    case '2':                
                        log_time = get_time();
                        log_user_out(user, log_time);
                        printf("\n%s logged out at: %s\n", user, log_time);
                        break;
                    case '3':
                        passwdset();
                        break;
                    case '4':
                        printf("Exiting...\n");
                        return 0;
                        break; 
                }
            }      
        } 
                 
        system("PAUSE");
        return 0;
    }
    
    
    
    /* Gets a password from a passwd file then returns it a pointer to it */
    char *archive_pass(void)
    {
        char *c, *d;
        FILE *p;
    
        p = fopen("passwd.txt", "r");
        c = (char *)malloc(10 * sizeof(char));
         
        fscanf(p, "%s", c);    
    
        return c;
    }
    
    /* Gets user name from STDIN */
    char *get_user(void) 
    {
        char *c, *s;
    
        c = (char *)malloc(10 * sizeof(char));
    
        printf("User: ");
        scanf("%s", c);
    
        return c;    
    }
    
    /* Gets user password from STDIN */
    char *get_passwd(void)
    {
        char *c;
    
        c = (char *)malloc(8 * sizeof(char));
    
        printf("Password: ");
        scanf("%s", c);
    
        return c;
    } 
    
    /* Gets time from time() function then returns formatted value */
    char *get_time(void)
    {
        time_t m;
    
        m = time(NULL);
    
        return ctime(&m);
    }
    
    /* Logs a line in a log file of info on user */
    void log_user_in(char *name, char *time) 
    {
        FILE *p;
        p = fopen("log.txt", "a");
    
        fprintf(p, "%s logged in: %s\n", name, time); 
    }
    
    /* Logs a line in a log file on info on user */
    void log_user_out(char *name, char *time)
    {
        FILE *p;
        p = fopen("log.txt", "a");
    
        fprintf(p, "%s logged out: %s\n", name, time);
    }
    
    /* Edits passwd.txt file to change password */    
    void passwdset(void)
    {
        FILE *fp;
        char *passwd;
    
        passwd = (char *)malloc(10 * sizeof(char));
        
        fp = fopen("passwd.txt", "w");
    
        printf("Enter Value: ");
        scanf("%s", passwd);
    
        while (*passwd != '\0')
            putc(*passwd++, fp);
        
        fclose(fp);
    }
        
    
    int menu(void)
    {
        int c;
        printf("\nNewfile 1.0\n");
        printf("1. Log in.\n");
        printf("2. Log out.\n");
        printf("3. Reset Password.\n");
        printf("4. Exit Program.\n");
        printf(":> ");
        c = getchar();
        return c;
    }
    Any help and/or insight is greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 01:30 PM
  5. What kind of code do u want a front-end designer generating?
    By MovingFulcrum in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-15-2001, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21