I\'m working on this C program and these two functions confuses me!! Help Please
ID: 3767631 • Letter: I
Question
I'm working on this C program and these two functions confuses me!! Help Please
int load_data(char* filename, char *acn, char *pwd): This function takes the input file name, two character arrays, integer array . It opens the input file, if unable to open it returns 0 otherwise it loads the account number and password from the text file into the character arrays and returns 1 at the end.
int checkAccountNumber(char * a, int n): This function takes an account number in the form of string and check if the account number consist of n digit number or not if not it returns 0 otherwise it returns 1.
main(): Use command line arguments to read the file name and the length of account number and password from the user. Call load_data function and load the inputs from the file into two characters arrays. Call check account number function and check if the account number is as per the specification and if it doesn't follow the specification throw an error message and ask for new account number from the user. Repeat this until user provide with a valid account number. See sample output below to display the error messages and collect information from the the user.
The output should be like this:
./a.out
Insufficient arguments
./a.out input.txt 6
Account number is 1112356
Password number is Dft10
Invalid account number account should be 6 digit number Enter the account number again: 1122er
Invalid account number account should be 6 digit number Enter the account number again: er1122
Invalid account number account should be 6 digit number Enter the account number again: 112233
Your account number is 112233
Explanation / Answer
/**C program that reads a text file and check if account number is valid.
If account number is not valid, prompt for account number until user enters a valid
account number*/
#include<stdio.h>
#include<conio.h>
#include<string>
int load_data(char *filename, char *acn, char *pwd);
int checkAccountNumber(char *a, int n);
int main(int argc, char *argv[] )
{
//file name "input.txt";
char *filename;
char acn[10];
char pwd[10];
char *a=acn;
char *p=pwd;
//Check if argument count is not 2
if(argc!=2)
{
printf("Invalid argumnets. ");
getch();
return 0;
}
//pass argv[1],filename and account number poiter and password,p
load_data(argv[1], a, p);
printf("Account number is %s ",acn);
printf("Password is %s ",pwd);
//Convert password to integer
int s=atoi(argv[2]);
while(!checkAccountNumber(a,s)!=1)
{
printf("Invalid account number account should be 6 digit number ");
printf("Enter the account number again: ");
//read account number
gets(a);
}
printf("Your account number is %s ",a);
//pause the program output
getch();
return 0;
}
int load_data(char *filename, char *a, char *p)
{
FILE *fp=fopen(filename,"r");
if(!fp)
{
printf("File does not exist. ");
getch();
exit(0);
}
fscanf(fp,"%s%s ",a, p);
fclose(fp);
}
//
int checkAccountNumber(char *a, int n)
{
int valid=0;
int length=strlen(a);
length=strlen(a);
//Check if lenght is not equal to n
//then return 1
if(n!=length)
{
valid=1;
return valid;
}
int index=0;
for(index=0;index<length;index++)
{
if(!isdigit(*a++))
valid=0;
}
return valid;
}
-------------------------------------------------------------------
input.txt
1112356 Dft10