Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
  FILE * fp1 = fopen("file.csv", "r");
  char string[200], toFind[200], savedLine[200], option;
  char* lineOne = NULL;
  char* result = NULL; 

  printf("Enter your ID Card number: ");
  scanf("%s", toFind);

  while(fgets(string, 200, fp1)){
      lineOne = strtok(string, "\n");
      strcpy(savedLine, lineOne);
      result = strstr(savedLine, toFind);

      if(!result){
      printf("Are you sure? [y/n]: ");
      while ((getchar()) != '\n');
      scanf("%c", &option);
      if(option == 'y'){
          printf("%s\n", lineOne);
          printf("Your account is removed\n");
      }
      if(option == 'n'){
          printf("Your account is not removed.\n");
          break;
      }
   }
}    fclose(fp1);
return 0; 
}


What I have tried:

This code only prints the data except the one line which I gave the id card number. But here is one problem that I am facing that is whenever it prints one line from file, it again ask me for yes or no. If I enter yes, it prints one line and again ask me for permission.

How to stop this loop of multiple permssion and print the data at once?
Posted
Updated 23-Sep-20 1:23am
v2
Comments
jeron1 18-Sep-20 10:05am    
Are you able to set breakpoints and step through your code (debug)?

The logic of your c ode is quite hard to follow. However, I think you need to add a break statement after the message telling the user that the account has been removed. Also, I trust you realise that in reality nothing has been removed since you are not writing the updated data to a new file.
 
Share this answer
 
Comments
ibilalkayy 18-Sep-20 8:01am    
I added a break statement but it stops one-time repetition.
Richard MacCutchan 18-Sep-20 8:41am    
To be honest, I cannot figure out exactly what this code is supposed to do. It appears to be a continuation of all your previous questions, but is not really an improvement on any of them. I suggest you stop writing code and think about what your program is supposed to be doing. Once you have that clear in your mind try writing down (in your own language) each step that is needed to achieve it. From that you should be able to construct a logical program.
you must seperate your output code from the input code. Best is to mocve it to own functions, so the program flow gets clearer.

rule of thumb: one function for every task. And never ever "spaghetti code" ;-)
 
Share this answer
 
I'm not so sure about the login. I assume the following might help:

C++
...
while ((option = getchar()) != 'y' && option != 'n');
if(option == 'y') {
...
 
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