0
#define REGISTER (char *[5]) {"REGISTER NEW USER ACCOUNT", "Name:","User ID:", "Password:","Email ID:"}

typedef struct{
    int id;
    char *name;
    char *userID;
    char *pwd;
    char *emailID;
} password;


password pw={0,"","","",""};

int step=1;
int padding=15;
int y1=7
int x1=0;
int x2=167

int row_1 = sizeof(REGISTER) / sizeof(REGISTER[0]);

for (int i=1; i<=row_1; i++){    // i is corretly initiaised to 1

      j=(((x2-x1)-strlen(*(REGISTER+i)))/2)+strlen(*(REGISTER+i));
      step += increment;
      gotoxy(((x1-padding)+j), (y1+step));

      if (i==1){
         fflush(stdin);
         scanf("%30[^\n]s",pw.name);
      }
      else if (i==2){
         fflush(stdin);
         scanf("%12s",pw.userID);
      }
      else if (i==3){
         fflush(stdin);
         scanf("%15s",pw.pwd);
      }
      else {
         fflush(stdin);
         scanf("%30s",pw.emailID);
      }
}

My scanf is not working properly. For every iteration of i it first uses gotoxy and then scanf should take the value of properly field based on i.

I can't get this working properly. The cursor moves properly if no value is entered.

However, if a value is entered, it unpredictably terminates with an error or moves the cursor to somewhere and waits for input.

I only need to include spaces with the pw.name field.

NOTE: Using Code::blocks with gcc.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anil
  • 15
  • 4
  • 2
    First get rid of all the `fflush(stdin)`, its behavior is undefined. – Eugene Sh. Nov 08 '21 at 22:11
  • 2
    Why do you have `s` at the end of `%30[^\n]s`? It's either `%30s` or `%30[^\n]`, not both. – Barmar Nov 08 '21 at 22:12
  • I am a bit surprised, where is your variable j defined? – Gabriel Pellegrino Nov 08 '21 at 22:12
  • `i<=row_1` should be `i – Barmar Nov 08 '21 at 22:15
  • Aside - truncating user credentials is a bad, bad idea. It is opening so many potential security holes. If you have size limitations, you should let the user know and not accept anything exceeding it. – Eugene Sh. Nov 08 '21 at 22:21
  • @Anil I did not say it will fix your problem. I said, it is a problem by itself. Read this: https://stackoverflow.com/questions/2187474/i-am-not-able-to-flush-stdin – Eugene Sh. Nov 08 '21 at 22:22
  • 1
    @eugene-sh Removing fflush(stdin) made matters worse. It jumps to 2nd field. – Anil Nov 08 '21 at 22:29
  • @Barmar - 1. Agreed. fixed %30[^\n] 2. i<=row_1 is correct because i has been initialized to 1 for (int i=1; i<=row_1; i++){ // i is corretly initiaised to 1 – Anil Nov 08 '21 at 22:30
  • @Gabriel Pellegrino In the code: j=(((x2-x1)-strlen(*(REGISTER+i)))/2)+strlen(*(REGISTER+i)); – Anil Nov 08 '21 at 22:31
  • Initializing it to 1 doesn't change the fact that the last array index is `row_1-1`, not `row_1`. – Barmar Nov 08 '21 at 22:31
  • The array indexes still go from 0 to row_1-1. You're just skipping the first element, not changing how the array is indexed. – Barmar Nov 08 '21 at 22:32
  • @Barmar. Sorry, I am wrong. You are right. I have fixed it now. However, the problem remains with regards to scanf not behaving properly. – Anil Nov 08 '21 at 22:37
  • Did you ever allocate memory for `pw.name`, `pw.userID`, etc.? – Barmar Nov 08 '21 at 22:41
  • `scanf("%12s",pw.userID);` is a problem as `pw.userID` was initialized with _string literal_ `""`. This is only big enough for just the _null character_ and as a _string literal_ should not get written. Code needs memory to save the scanned `.userID`. – chux - Reinstate Monica Nov 08 '21 at 22:45
  • @Anil Who or what text suggested an `s` for format in `scanf("%30[^\n]s",pw.name);`? – chux - Reinstate Monica Nov 08 '21 at 22:47
  • @ Barmar - You are a champ, mate. Thank you so much. That was the problem. I now used malloc to allocate memory to the struct. That fixed the issue perfectly. yes, I know I have to free the memory. THANK ONE AND ALL – Anil Nov 08 '21 at 23:24

0 Answers0