How to enter and scan multiple chars in c

早过忘川 提交于 2020-01-07 16:17:48

问题


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;
}

回答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.




回答2:


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.




回答3:


Use c library function fgets().

#include <stdio.h>
int main(){
     Char username[10];
     printf(“Username: “);
     fgets(username,10,stdin);
}


来源:https://stackoverflow.com/questions/47816312/how-to-enter-and-scan-multiple-chars-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!