3 Replies - 408 Views - Last Post: 04 December 2012 - 04:33 AM Rate Topic: -----

#1 Pwn-90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 12

Getting Garbage values for User ID, and how to read profiles off file?

Posted 03 December 2012 - 07:10 PM

Hey all, into phaseII with this program for my C programming class. This is basically an employee sign up/sign in program for an imaginary company. Part of my requirements are to strcat() the first and last name entered by the user to create a user ID. Upon doing this, I am getting garbage values returned. I have outlined the code with /********/ in the function allsignup(...).

My second requirement is to store the User profiles to a separate file, and then when the user goes to sign in with a user ID/ password, to open the file and read the profiles, sorting each one to a suitable array by user ID. I have coded the function to write the profiles, but am having trouble figuring out the read/sort part of it. We were given a very basic example of writing/reading to file with strings in class. Other than that, I've been reading my C book trying to make sense of how to do this. This code is at the very bottom of the code, the read/sort function I started is encased in /*** ... ***/ so the program will run, and the write function is above it.

I thank anyone taking the time to look at this, and greatly appreciate any help!

/*
Summary: Allows a user to login or create a personal account, allows an admin
         to login and create multiple user accounts for employees.
*/         
#include <stdio.h>
#include <stdlib.h>
#define BUFFER 128
#define PASS 16

struct profile
{
  char user[20];
  char pass[PASS];
  char last[15];
  char first[15];
  char email[30];
  int firsttime;
};

//prototypes
  void mainmenu(struct profile User [], int size, int *ucount);
  void signup(struct profile User [], int size, int *ucount);
  void signin(struct profile User [], int size, int *ucount);
  void adminSignup(struct profile User [], int size, int *ucount);
  void userSignup(struct profile User [], int size, int *ucount);
  int populate5(struct profile User []);
  int credentials(struct profile User [], int size, int *ucount, char userid[], char pass[]);
  int verifypass(struct profile User [], int *ucount);
  int verifyuser(struct profile User [], char userid [], int size, int *ucount);
  int verifyemail(struct profile User [], int *ucount);
  int admin(struct profile User [], int size, int *ucount, int a);
  void allsignup(struct profile User [], int size, int *ucount, int k);
  void adminlogin(struct profile User [], int size, int *ucount);
  void userfiles(struct profile User [], int *ucount);
  void readfiles(struct profile User [], int *ucount, int size);
  void accountwelcome(struct profile User [], int *ucount);
//endprotos

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  
int main(int argc, char *argv[])
{
  //declarations
    int size = 15;//create 15 empty profiles
    struct profile User[size];
    int ucount = 0;//used for keeping track of the number of stored accounts
    char choice = ' ';
  
  do
  {//beginprog
    system("CLS");
    printf("********** Login Program **********\n");
    printf("1 -- Begin\n");
    printf("2 -- QUIT\n");
    printf("***********************************\n");\
    choice = getch();
    if(choice == '1') mainmenu(User, size, &ucount);
  }while(choice != '2');  
  system("CLS");	
  return 0;
}//endprog

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void mainmenu(struct profile User [], int size, int *ucount)
{
  //declarations
    char choice = ' ';
    int a = 0;
    
  do
  {//main menu
    system("CLS");
    printf("********** Main Menu **********\n");
    printf("1 -- Admin Initial Sign Up\n");//admin must create a profile before other options can be accessed
    printf("2 -- Sign Up\n");//create profile
    printf("3 -- Sign In\n");//login
    printf("4 -- QUIT\n");
    printf("*******************************\n");
    choice = getch();
    switch(choice)
    {
      case '1' : a = admin(User, size, ucount, a);
                 break;
      ///////////////////////////////////////////
      case '2' : if(a == 0)
                 {
                   printf("\nAdmin profile must be created first!\n");
                   system("PAUSE");
                 }
                 else signup(User, size, ucount);
                 break;
      ///////////////////////////////////////////
      case '3' : signin(User, size, ucount);
                 break;
      ///////////////////////////////////////////
      case '4' : exit(1);
    }
  }while(choice != '4');
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void signup(struct profile User [], int size, int *ucount)
{
  //declarations
    char choice = ' ';
    
  do
  {//main menu
    system("CLS");
    printf("********** Sign Up **********\n");
    printf("1 -- Admin Creates Profiles\n");//admin creates profiles for users
    printf("2 -- User Sign Up\n");//user creates personal profile
    printf("3 -- Back\n");
    printf("*****************************\n");
    choice = getch();
    switch(choice)
    {
      case '1' : adminlogin(User, size, ucount);
                 break;
      case '2' : userSignup(User, size, ucount);
                 break;
      case '3' : break;
    }
  }while(choice != '3');
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void adminSignup(struct profile User [], int size, int *ucount)
{
  //declarations
    char choice = '\0';
    int k = 1;
    
  do
  {//admin authenticated and can create profiles
    system("CLS");
    printf("********** Admin Sign Up **********\n");
    printf("There are %d empty profiles remaining\n", (size - *ucount));     
    printf("1 -- Create New User\n");
    printf("2 -- Back\n");
    printf("***********************************\n");
    choice = getch();
    switch(choice)
    {
      case '1' : allsignup(User, size, ucount, k);
      case '2' : break;
    }
  }while(choice != '2');      
}  

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void userSignup(struct profile User [], int size, int *ucount)
{
  //declarations
    char choice = ' ';
    int k = 0;
    
  do
  {//user creates personal profile
    system("CLS");
    printf("********** New User Sign Up **********\n");     
    printf("1 -- Create New User\n");
    printf("2 -- Back\n");
    printf("**************************************\n");
    choice = getch();
    switch(choice)
    {
      case '1' : allsignup(User, size, ucount, k);
      case '2' : break;
    }
  }while(choice != '2');       
}     

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int credentials(struct profile User [], int size, int *ucount, char userid[], char pass[])
{//verifies input vs stored
  //declarations  
    int i = 0;
    int a = 0;
    int b = 0;
  
  do
  {//checks pointer to array of user input against Stucture arrays
    if(strcmp(userid, User[i].user) == 0) a = 1;//if all characters match, activate flag -a-  
    if(strcmp(pass, User[i].pass) == 0) b = 1;//if all characters match, activate flag -b-   
    if(a == 1 && b ==1) 
    {
      printf("\nLogin Successful!\n\n");
      system("PAUSE");
      return 1;//if flags -a- and -b- match, validation successful
    }
    else i++;//otherwise move to next account to check against 
  }while(i<size);
  return 0;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void signin(struct profile User [], int size, int *ucount)
{
  //declarations
    int i = 0;
    int z = 0;
    char userid[20] = {' '};
    char pass[16] = {' '};
    int attempt = 3;
    int success = 1;
    int len = 0;
 
  do
  {//validate input vs stored
    system("CLS");
    
    //enter user ID
    printf("Enter userID >> ");
    fgets(userid, BUFFER, stdin);
    while(userid[0] == '\n')
    {
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");//decrements # of attempts remaining if just [ENTER] is pressed
      if(attempt == 0) return;
      system("CLS");
      printf("Enter userID >> ");
      fgets(userid, BUFFER, stdin);
    }  
    len = strlen(userid);//removes [ENTER] as a character 
    if(userid[len - 1] == '\n') userid[len-1] = '\0';
    
    //enter user password
    printf("\nEnter password >> ");
    fgets(pass, BUFFER, stdin);
    while(pass[0] == '\n')
    {
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");//decrements # of attempts remaining if just [ENTER] is pressed
      if(attempt == 0) return;
      system("CLS");
      printf("Enter password >> ");
      fgets(pass, BUFFER, stdin);
    }
    len = strlen(pass);//removes [ENTER] as a character
    if(pass[len - 1] == '\n') pass[len-1] = '\0';
    
    //verify entries
    success = credentials(User, size, ucount, userid, pass);
    
    //decrements # of attempts remaining if incorrect
    if(success == 0)
    {
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");
      if(attempt == 0) return;//if no more attempts remaining, return to menu
    }
    
    //if the admin created the user and generated a random password
    if(User[*ucount].firsttime == 1)
    {
      do
      {
        printf("This is a new account. Please enter a new password.");
        printf("\n6 - 15 letters/numbers, cannot begin with a number >> ");
        fgets(User[*ucount].pass, BUFFER, stdin);
        //will not accept [ENTER] as the first character entry
        while(User[*ucount].pass[0] == '\n')
        {
          printf("\nEnter User Password");
          printf("\n6 - 15 letters/numbers, cannot begin with a number >> ");
          fgets(User[*ucount].pass, BUFFER, stdin);
        }                   
        len = strlen(User[*ucount].pass);
        if(User[*ucount].pass[len - 1] == '\n') User[*ucount].pass[len-1] = '\0';
        z = verifypass(User, ucount);//send for verification of password
      }while(z != 4);
      User[*ucount].firsttime = 0;
    }//end password creation
      
  }while(success == 0);
}  

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*
int populate5(struct profile User [])
{//hardcoded accounts, including admin
  strcpy(User[0].first,"Steve");
  strcpy(User[0].last,"Haren");
  strcpy(User[0].user,"Admin");
  strcpy(User[0].pass,"Pwd");
  
  strcpy(User[1].first,"John");
  strcpy(User[1].last,"Mathews");
  strcpy(User[1].user,"JM561");
  strcpy(User[1].pass,"AAA");
  
  strcpy(User[2].first,"Laura");
  strcpy(User[2].last,"Candy");
  strcpy(User[2].user,"LC968");
  strcpy(User[2].pass,"BBB");
  
  strcpy(User[3].first,"Mark");
  strcpy(User[3].last,"Johnson");
  strcpy(User[3].user,"MJ185");
  strcpy(User[3].pass,"CCC");
  
  strcpy(User[4].first,"Mike");
  strcpy(User[4].last,"Harrison");
  strcpy(User[4].user,"MH115");
  strcpy(User[4].pass,"DDD");
  
  return 5;  
}
*/     

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int verifypass(struct profile User [], int *ucount)
{
  //declarations
    int i = 0;
    int j = 0;
  
  //checks password for length requirements  
  if(strlen(User[*ucount].pass) < 6 || strlen(User[*ucount].pass) > 15)
    {
      printf("\nIncorrect entry, password must be between 6 and 15 characters");
      return 1;
    }
  
  //checks password for number as first character
  if(User[*ucount].pass[0] >= 48 && User[*ucount].pass[0] <= 57)
    {
      printf("\nIncorrect entry, password cannot start with a number");
      return 2;
    }
  
  //checks passsword for mixture of letters and numbers
  for(i = 0; i < PASS; i++)
  {
    if(User[*ucount].pass[i] >= 65 && User[*ucount].pass[i] <= 90 || User[*ucount].pass[i] >= 97 && User[*ucount].pass[i] <= 122) j++;
    if(strlen(User[*ucount].pass) == j) 
    {
      printf("\nIncorrect entry, password must contain letters and numbers");
      return 3;
    }
  }
  
  return 4;//returns value if password has met all requirements
}  

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int verifyuser(struct profile User [], char userid [], int size, int *ucount)
{
  //declarations
    int i = 0;
    
  while(i<size)
  {
    if(strcmp(userid, User[i].user) == 0)//if user entry matches a stored profile
    {
      printf("\nThis user ID is already taken.");
      return 1;
    }
    else i++;//else check next profile
  }    
  return 3;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int verifyemail(struct profile User [], int *ucount)
{
  //declarations
    int i = 0;
    int ATcount = 0;
    int DOTcount = 0;

  for(i = 0;i < strlen(User[*ucount].email); i++)
  {
    //check email for @ symbol
    if(User[*ucount].email[i] == '@') 
    {
      ATcount = 1;
      
      //ensure '@' is not followed directly by '.'
      if(User[*ucount].email[i+1] == '.')
      {
        printf("\nInvalid email entry 1");
        return 1;
      }
    }
    
    //check email for either .com or .net ending 
    if(User[*ucount].email[i] == '.') 
    {
      if(User[*ucount].email[i+1] == 'c' && User[*ucount].email[i+2] == 'o' &&
         User[*ucount].email[i+3] == 'm'
         ||
         User[*ucount].email[i+1] == 'n' && User[*ucount].email[i+2] == 'e' &&
         User[*ucount].email[i+3] == 't')
         
         DOTcount = 1;
      else 
      {
        printf("\nInvalid email entry, email must end in .com or .net");
        return 1;
      }
    }  
    
    //check if '.' found before '@'
    if(DOTcount > ATcount)
    {
      printf("\nInvalid email entry");
      return 1;
    }
    
    //if email checks out, return verification
    if(ATcount == 1 && DOTcount == 1) return 2;
  
  }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int admin(struct profile User [], int size, int *ucount, int a)
{
  int k = 0;
  //prompt user to create Admin account
  if(a == 0)
  {
    system("CLS");
    printf("Admin, press any key to create your account..");
    getch();
    allsignup(User, size, ucount, k);
    return 1;
  }
  //if Admin account already created, deny access to create another and return
  else
  {
    system("CLS");
    printf("**** ACCESS DENIED **** ADMIN ACCOUNT ALREADY CREATED!");
    getch();
    return 1;
  }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void allsignup(struct profile User [], int size, int *ucount, int k)
{
  //declarations
    int len = 0;
    int len1 = 0;
    int len2 = 0;
    int len3 = 0;    
    int i = 0;
    int j = 0;
    int a = 0;
    int x = 0;
    int y = 0;
    int z = 0;
    char *idStrCat;
    char *idU = User[*ucount].user;
    
  system("CLS");
                 
  //check if all profiles are full and return to menu if so
  if(*ucount == size)
  {
    printf("No more profiles available.");
    system("PAUSE");
    system("CLS");
    return;
  }
  
  User[*ucount].firsttime = k;//determines if this is the user's first time
                              //signing up, which will create a random password
                                  
  //enter user first name
  printf("Enter User First Name >> ");
  fgets(User[*ucount].first, BUFFER, stdin);
  //will not accept [ENTER] as the first character entry
  while(User[*ucount].first[0] == '\n')
  {
    printf("Enter User First Name >> ");
    fgets(User[*ucount].first, BUFFER, stdin);
  }
  len = strlen(User[*ucount].first);
  if(User[*ucount].first[len - 1] == '\n') User[*ucount].first[len-1] = '\0';    
  
  len1 = strlen(User[*ucount].first);//set length for first part of userid
  char *idF = User[*ucount].first;
  
  //enter user last name
  printf("\nEnter User Last Name >> ");
  fgets(User[*ucount].last, BUFFER, stdin);
  //will not accept [ENTER] as the first character entry
  while(User[*ucount].last[0] == '\n')
  {
    printf("Enter User Last Name >> ");
    fgets(User[*ucount].last, BUFFER, stdin);
  }                 
  len = strlen(User[*ucount].last);
  if(User[*ucount].last[len - 1] == '\n') User[*ucount].last[len-1] = '\0';
  char *idL = User[*ucount].last;  
  
  len2 = strlen(User[*ucount].last);//set length for second part of userid
  len3 = len1 + len2;//set userid length
/******************************************************************************/  
  idStrCat = strcat(idF, idL);//use pointers to set userid as combo of first and last name
  idU = idStrCat;//set the User ID to the strcat of first and last name
  
  if(len3 < 20)
  {
    i = len3;
    for(i = len3; i < 20; i++) User[*ucount].user[i] = '\0';
  }
/******************************************************************************/  
  //if first time account is being created by admin
  if(User[*ucount].firsttime == 1)
  {
    i = 0;
    srand(time(0));//generate different set of password characters each time
    while(i < 15)
    {//execute until 15 characters are stored
      int randnum = rand()%122;//stores random ASCII number generated
      if(randnum >= 48 && randnum <= 57 || randnum >= 65 && randnum <= 90 ||
         randnum >= 97 && randnum <= 122)
      {//if character generated is only either a letter or number
        User[*ucount].pass[0] = randnum;
        i++;
      }//endif
    }//endwhile for password storage
  }//end password creation
  
  else
  {
    do
    {
      printf("\nEnter User Password");
      printf("\n6 - 15 letters/numbers, cannot begin with a number >> ");
      fgets(User[*ucount].pass, BUFFER, stdin);
      //will not accept [ENTER] as the first character entry
      while(User[*ucount].pass[0] == '\n')
      {
        printf("\nEnter User Password");
        printf("\n6 - 15 letters/numbers, cannot begin with a number >> ");
        fgets(User[*ucount].pass, BUFFER, stdin);
      }                   
      len = strlen(User[*ucount].pass);
      if(User[*ucount].pass[len - 1] == '\n') User[*ucount].pass[len-1] = '\0';
      z = verifypass(User, ucount);//send for verification of password
    }while(z != 4);
  }
            
  //enter user email
  do
  {
    printf("\nEnter User Email >> ");
    fgets(User[*ucount].email, BUFFER, stdin);
    len = strlen(User[*ucount].email);
    if(User[*ucount].email[len - 1] == '\n') User[*ucount].email[len-1] = '\0';
    x = verifyemail(User, ucount);//send for verification of email
  }while(x != 2);                 //requirements
  
  accountwelcome(User, ucount);
  
  userfiles(User, ucount);               
  //move to next empty profile
  *ucount = *ucount + 1;
                 
  return;//exit to menu
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void adminlogin(struct profile User [], int size, int *ucount)
{
  //declarations
    int a = 0;
    int b = 0;
    int i = 0;
    char userid[10] = {' '};
    char pass[16] = {' '};
    int attempt = 3;
    int success = 0;
    int len = 0;
    
  do
  {
    system("CLS");
    printf("Enter Admin userID >> ");
    fgets(userid, BUFFER, stdin);//enter admin username
    
    while(userid[0] == '\n')
    {//wrong entry, X tries remaining out of 3
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");
      if(attempt == 0) return;
      system("CLS");
      printf("Enter Admin userID >> ");
      fgets(userid, BUFFER, stdin);
    }
    
    len = strlen(userid);//removes '\n' from string to match against hardcoded entries
    if(userid[len - 1] == '\n') userid[len-1] = '\0';
    printf("\nEnter Admin password >> ");
    fgets(pass, BUFFER, stdin);//enter admin password
    
    while(pass[0] == '\n')
    {//wrong entry, X tries remaining out of 3
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");
      if(attempt == 0) return;
      system("CLS");
      printf("Enter Admin password >> ");
      fgets(pass, BUFFER, stdin);
    }
    
    len = strlen(pass);//removes '\n' from string to match against hardcoded entries
    if(pass[len - 1] == '\n') pass[len-1] = '\0';   

    if(strcmp(userid, User[0].user) == 0) a = 1;//if all characters match, activate flag -a-  
    if(strcmp(pass, User[0].pass) == 0) b = 1;//if all characters match, activate flag -b-   
    if(a == 1 && b == 1) 
    {
      printf("\nLogin Successful!\n\n");
      system("PAUSE");
      success = 1;//if flags -a- and -b- match, validation successful
    }
    else
    {//if entries are not correct
      attempt = attempt - 1;
      printf("\nIncorrect entry, you have %d attempts remaining\n",attempt);
      system("PAUSE");
      if(attempt == 0) return;
    } 
  
  }while(success == 0);
  if(success == 1) adminSignup(User, size, ucount);//grant access to create profiles
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void userfiles(struct profile User [], int *ucount)
{
  //declarations
    FILE *fprofiles;
    struct profile *puser = &User[*ucount];
  
  if((fprofiles = fopen("Profiles", "a+")) == NULL)
  {
    printf("Cannot open file! ...any key to continue\n");
    getch();
    exit(1);
  }
  
  //create file and write current user profile to it
  fwrite(puser, sizeof(struct profile), 1, fprofiles);
  
  if(fclose(fprofiles) == EOF); printf("\nFile couldn't be closed!!\n");
  return;//close file and return
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
void readfiles(struct profile User [], int *ucount, int size)
{
  //declarations
    int i = 0;
    struct *puser = &User;
  
  if((fprofiles = fopen("Profiles", "r")) == NULL)
  {
    printf("\nFile couldn't be opened!\n");
    exit(1);
  }  

  //read all structures stored
  for(i = 0;i < size;i++)
  {
    fread(*(puser+i), sizeof(*puser), 1, fprofiles);
    
    
    
  fclose(fp);
  return;
}        
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void accountwelcome(struct profile User [], int *ucount)
{
  printf("\nNew account created!");
  printf("\n\nYour User ID is >> %s", User[*ucount].user);
  printf("\nYour Password is >> %s", User[*ucount].pass);
  getch();
  return;
}  



Is This A Good Question/Topic? 0
  • +

Replies To: Getting Garbage values for User ID, and how to read profiles off file?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Getting Garbage values for User ID, and how to read profiles off file?

Posted 03 December 2012 - 08:49 PM

Please ask a specific question.

Don't hand off code you are having trouble with to us. We aren't your homework assistants here...
Was This Post Helpful? 0
  • +
  • -

#3 Pwn-90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 12

Re: Getting Garbage values for User ID, and how to read profiles off file?

Posted 04 December 2012 - 04:27 AM

Ok. Can anyone point me in the direction as to why I am getting garbage values for User ID? And I am trying to use a for loop to read each struct off of the file and assign the values to arrays, but I don't know if this makes any sense. I was under the impression that this was the way to do it, unless I pull each struct and assign it a new struct?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Getting Garbage values for User ID, and how to read profiles off file?

Posted 04 December 2012 - 04:33 AM

You just dumped 700+ lines of code on us. Can't you narrow it down a little further? Like where are you creating the user ID? Where are you seeing it's "garbage"?

EDIT: Oh, I see, you mentioned it in the first post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1