Reputation: 27
The first function is able to make new account. I want to store that new_acc name and DOB and transfer it to another function. So when I call the name in second function, it should check that name, if present then give me access.
One function
int new_acc(FILE *fp, char *name, size_t namesize, char *dob, size_t dobsize){
char data[8][51];
int done = 0;
int repeat = 0;
while (!done) {
if (repeat) {
if (! allowmore())
break;}
repeat = 1;
for (int i = 0; i < 8; i++) {
printf("Enter your %s: ", listing[i]);
if (scanf(" %50[^\n]", data[i]) != 1) {
done = 1;
break;}
}
if (!done) { fprintf(fp, "%s %s\n", data[0], data[1]); }
}
fclose(fp);
return 0;
}
Another function
int list_view(char *name, char *dob){
printf("For person details, please enter the person name: \n");
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
printf("Enter your Name: ");
if (scanf("%50s", name) != 1){ // Giving access through this
perror("Error while reading!"); // code
return 0;}
char ch[100];
int index = 0;
for (int i=0; i<8; i++){
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
if(ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your %s: %s\n",listing[i], ch);
index = 0;
i++;}
else { index++; }
}
fclose(fr);
}
else{ printf("Unable to read file."); }
}
return 0;
}
Upvotes: 1
Views: 93
Reputation: 61
You need to be mindful of the scope of your variables. As a refresher:
The scope of a variable is the part of the program within which the variable can be used. So, the scope describes the visibility of an identifier within the program.
The data
variable is declared locally inside new_acc
and not returned, which means that it is not visible outside the function.
To clarify this point, consider your listing
array. You access it within both new_acc
and list_view
despite not passing it into either function. This leads me to believe that it is declared globally (outside any functions). If you want name
to be visible in list_view
you must similarly make it global or return it from new_acc
.
note: the link I included also discusses lifetime of variables which is equally important to consider
Upvotes: 1