I'm creating a voting system in C. I've started off by creating a function that allows the user to register an account. One of the features of the function is it's supposed to generate a uniqueID for each new voter and assign it to the member variable uniqueID in the Voter struct. For some reason when I execute the code it adds every other variable into the voterDatabase.txt file except for the uniqueID. Why is this?
Update: I've updated the code, and there's slight progression. It writes most of the struct member variables in cleartext, except for the uniqueID which is written as garbage. I've also added added comments for the non-standard conio.h stuff so people can follow the code better.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
//Forward declaration
struct Voter;
//Function prototypes
int calcExistingVoters();
void createAccount(struct Voter** voterPtr, int* numVoters);
//Voter struct
struct Voter {
char firstName[20];
char lastName[20];
char email[60];
char password[17];
int age;
char vote[20];
char uniqueID[8];
};
int main(){
struct Voter* voterPtr = malloc(sizeof(struct Voter));
int numVoters = calcExistingVoters();
createAccount(&voterPtr, &numVoters);
free(voterPtr);
}
int calcExistingVoters(){
int numVotersInFile;
FILE *acc;
acc = fopen("voterDatabase.txt", "r");
if (acc != NULL) {
// read the number of voters from the file
fseek(acc, 0, SEEK_END); //Seeks to the end of the file
long int fileSize = ftell(acc); //calculates file size in bytes
numVotersInFile = fileSize / sizeof(struct Voter);// calculates number of existing voters
fclose(acc);
}
return numVotersInFile;
}
void createAccount(struct Voter** voterPtr, int* numVoters){
FILE *acc; //initializes file pointer
char finish;
acc = fopen("voterDatabase.txt", "a+"); //opens file in append mode
if (acc == 0) { //if file fails to open, creates the file
acc = fopen("voterDatabase.txt", "w+");
system("cls");
printf("Please wait one moment while we create the voter database on your system\n");
printf("Complete! Press any key to continue\n");
getch();
fclose(acc);
acc = fopen("voterDatabase.txt", "a+");
}
while(1){ //Infinite loop
printf("Enter your first name: ");
scanf("%s", (*voterPtr)->firstName);
printf("\nEnter last name: ");
scanf("%s", (*voterPtr)->lastName);
printf("\nEnter your age: ");
scanf("%d", &(*voterPtr)->age);
printf("\nEnter your email: ");
scanf("%s", (*voterPtr)->email);
//creates password
printf("\nCreate a password (Max. 16 Characters): ");
char pword[17];
for (int i = 0; i < 16; i++) {//for loop allows user to type a max of 16 chars
pword[i] = getch(); //takes user input and assigns it to pword string
char c = pword[i]; //assigns pword string to new char variable
if(c==13) break; //when user presses enter key the for loop exits
else printf("*"); //prints an asterix instead of input character to simulate password entry
}
pword[16] = '\0'; //assigns null character pword string
strcpy((*voterPtr)->password, pword); //copies pword string into Voter member variable
printf("\nAccount creation complete, press esc to go back to the Main menu");
// Assign a unique ID to the voter
(*numVoters)++; //increments number of voters
char voterId[8]; //Initializes voterId variable
sprintf(voterId, "V%d", (*numVoters)); //prints voterId to file
strcpy((*voterPtr)->uniqueID, voterId);
strcpy((*voterPtr)->vote, "");
finish = getch(); // take user input and assign to "finish" variable
if (finish == 27) //when user presses esc key it exits loop
break;
}
//writes new data to file
fprintf(acc, " %s\n %s\n %d\n %s\n %s\n %s\n %s\n",
(*voterPtr)->firstName, (*voterPtr)->lastName, (*voterPtr)->age,
(*voterPtr)->email, (*voterPtr)->password, (*voterPtr)->vote,
(*voterPtr)->uniqueID);
fclose(acc);
}