Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code, the problem which I am facing is whenever I try to use scanf after
printf("Enter your choice: ");
in first part and
printf("Enter your name: ");
in second part, it does not allow me to
fputs("Select your choice to update: ", stdout);
execute this code further. It stops there but whenever I use fgets it gives message of
printf("Access Denied. Please enter correct Details.\n");
How to solve this problem.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 15
    #define STRING_LEN 50
    
    int update_info(FILE * fr1, FILE * fr2, FILE * fr3, FILE * fw1, char *name, int *dob);
    /* Global Variables */
    char one='1', two='2', three='3', four='4', five='5', six='6', seven='7', eight='8', choice[MAX];
    static const char * listing[] = {"Name", "Date of birth","ID card number","Phone number","Address","Account","Fixing year","Deposit amount"};
    
    int main(){
    	FILE * fw1 = fopen("new.csv","w");
        FILE * fr1 = fopen("file.csv", "r");
        FILE * fr2 = fopen("file.csv", "r");
    	FILE * fr3 = fopen("file.csv","r");
    
    	int dob[11];
    	char name[15] = "";
    
    	while(one != choice[0]){
    		printf("%c. Create new account\n",one);
    		printf("%c. Update information\n",two);
    
    		printf("Enter your choice: ");
    		scanf("%7s", &choice); 
            // fgets(choice, 7, stdin);         
    
    		if (choice[0] == one){
    			printf("Selected One");
    			break;
    		}
    		else if (choice[0] == two){
    			update_info(fr1, fr2, fr3, fw1, name, dob);
    			break;
    		}
    		else{
    			printf("Please enter the correct information\n");
    			break;
    		}
      }
    	return 0;
    }
   
Next part 

    int update_info(FILE * fr1, FILE * fr2, FILE * fr3, FILE * fw1, char *name, int *dob){
    	int option, i=0, one_by_one;
    	char file_text[100], updated_name[50], str[100], string[STRING_LEN], input[STRING_LEN], saved_word[STRING_LEN];
    	char* word = NULL;
    	short int FLAG = 0;
      
    	printf("Enter your name: ");
    	scanf("%s", &str);
    	// fgets(str, 100, stdin);
    
    	while(fscanf(fr1, "%s", file_text) != EOF){
    		if(!strcmp(file_text, str)){
    			printf("Found. Here are your details. \n");
    			FLAG = 1;
    		}
    	}
    
    	if (!(FLAG == 1)){
    		printf("Access Denied. Please enter correct Details.\n");
    		return -1;
    	}
    	fclose(fr1);
    
    	int c = fgetc(fr2);
    	while(c != EOF){
    		file_text[one_by_one] = c;
    		if(file_text[one_by_one] == ','){
    			file_text[one_by_one] = '\0';
    			printf("Here is your %s: %s\n",listing[i],file_text);
    			one_by_one = 0;
    			i++;
    		}
    		else
    			one_by_one++;
    		c = fgetc(fr2);
       }
      fclose(fr2);
    
      for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option)
    	  printf("%d. Your %s\n", option, listing[option-1]);  
    
      // printf("Select your choice to update: ");
    	fputs("Select your choice to update: ", stdout);
    	scanf("%d", &option);
    
    	if (option == 1){
    		while(fgets(string, STRING_LEN-1, fr3)){
    			printf("Scanned now.\n");
    		}
    	}
     	return 0;
    }

What I have tried:

You can check it and its working fine but two problems I am not understanding.
Posted
Updated 18-Jan-23 8:28am
Comments
Richard MacCutchan 20-Aug-20 5:43am    
Why are you trying to input a string of 7 characters when all you need is a single numeric value? Do it this way:

int choice;
scanf("%d", &choice);
switch (choice)
{
case 1;
// whatever option 1 does;
break;
case 2;
// whatever option 2does;
break;
default:
// bad option situation
}
KarstenK 20-Aug-20 6:12am    
post it as answer.

1 solution

I think the problem is with how data is input to your .cvs file. I'm assuming its a text file.

If strings are input with dbl quotes: "fred", then when fscanf reads the data it would get this:

"\"fred\"\000\000...\000"

So if you give scanf a name with out dbl quotes: fred, memory will look like this:

"fred\000...\000".

this is what you give to fscanf to compare with what it got from your csv file. They won't give strcmp() a match.

Of course the opposite is true, no dbl quotes in data file but dbl quotes given to scanf.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900