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