0

I want to enter multiple printfs but i dont get opportunity to enter. I can enter only 1, but after that it just ends the programme.I tried with do while but it didnt work

int main()
{
  int number;
  char username[30]="";
  char fullName[30]="";
  char password[30]="";
  printf("Do you want to log in(1) or register (2)? \n");  
  scanf("%d",&number);
  if (number==2)
  {
    printf("username : ");
    scanf("%s",&username);
    printf("Full name : ");
    scanf("%s",&fullName);
    printf("Password : ");
    scanf("%s",&password);
    printf("Repeat password : ");
    scanf("%s",&password);
  }
  return 0;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
miris
  • 11
  • 1

3 Answers3

1

Read full lines using fgets() into a suitably large buffer, then parse that.

Note that %s will stop at the first blank character, so a full name of "Mr X" will leave "X" in the input buffer, grabbing that for the password and so on. It's really not a robust way of getting input.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

I can enter only 1, but after that it just ends the programme.

Of course, as the code has if (number==2) @Scadge

If you enter "2", consider the following:


scanf("%s",&fullname); will not save spaces or other white-spaces into fullname. Entering a full name like "John Doe" will save "John" into fullname and "Doe" into password.

Avoid using scanf().


Rather than use scanf() to read user input, read user input with fgets(). This is a fine opportunity for helper functions that can handle various input issues.

int read_int(const char *prompt) {
  if (prompt) fputs(prompt, stdout);
  fflush(stdout);  // insure output is written before asking for input

  char buffer[40];
  if (fgets(buffer, sizeof buffer, stdin) == NULL) {
    return NULL;
  }

  int i;
  if (sscanf(buffer, "%d", &i) == 1) {
    return i;
  }

  // TBD - what should code do if invalid data entered.  Try again?
}

char *read_line(char *dest, sizeof size, const char *prompt) {
  if (prompt) fputs(prompt, stdout);
  fflush(stdout);  // insure output is written before asking for input

  char buffer[size * 2 + 1];  // form buffer at _least 1 larger for \n
  if (fgets(buffer, sizeof buffer, stdin) == NULL) {
    return NULL;
  }

  size_t len = strlen(buffer);
  if (len > 0 && buffer[len-1] == '\n') buffer[--len] = '\0';

  if (len >= size) {
    // input too big - how do you want to handle this?
    TBD_Code();
  } 
  return strcpy(dest, buffer);
}

Now use these 2 helper functions for clean user input

// printf("Do you want to log in(1) or register (2)? \n");  
// scanf("%d",&number);
number = read_int("Do you want to log in(1) or register (2)? \n");

...

// printf("username : ");
// scanf("%s",&username);
read_line(username, sizeof username, "username : ");
// printf("Full name : ");
// scanf("%s",&fullName);
read_line(fullName, sizeof fullName, "fullName : ");

Additional code could be added to check for end-of-file, extremely long lines, int range testing, etc.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Use c library function fgets().

#include <stdio.h>
int main(){
     Char username[10];
     printf(“Username: “);
     fgets(username,10,stdin);
}
  • Just a suggestion [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Arun A S Dec 14 '17 at 15:52
  • Oh yea, use fgets() instead. gets() function will read unlimited size of data inputted and store it past the buffer. It can break your computer. – Kenje Hofileña Dec 14 '17 at 17:15
  • 1
    Don't ever use `gets()`, which was deprecated in C99 and completely removed from C11. Don't even suggest using this dangerous function. – ad absurdum Dec 14 '17 at 17:20
  • My bad. And about buffer, it is the temporary storage area for example when we are passing more than required number of values as an input then rest of all values will be automatically held in standard input buffer, this buffer data will automatically pass to next input functionality if it is exist. – Kenje Hofileña Dec 14 '17 at 17:28
  • 1
    Change `“` to `"` (2 places.), `Char` to `char`. – chux - Reinstate Monica Dec 14 '17 at 17:29