简体   繁体   English

如何在C中输入和扫描多个字符

[英]How to enter and scan multiple chars in c

I want to enter multiple printfs but i dont get opportunity to enter. 我想输入多个printfs,但是我没有机会输入。 I can enter only 1, but after that it just ends the programme.I tried with do while but it didnt work 我只能输入1,但之后它才结束程序。

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

Read full lines using fgets() into a suitably large buffer, then parse that. 使用fgets()将整行读取到适当的大缓冲区中,然后进行解析。

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. 请注意, %s将停在第一个空白字符处,因此全名“ Mr X”将在输入缓冲区中保留“ X”,以获取密码,以此类推。 It's really not a robust way of getting input. 这实际上不是获得输入的可靠方法。

I can enter only 1, but after that it just ends the programme. 我只能输入1,但此后程序才结束。

Of course, as the code has if (number==2) @Scadge 当然,如代码中的if (number==2) @Scadge

If you enter "2" , consider the following: 如果输入"2" ,请考虑以下事项:


scanf("%s",&fullname); will not save spaces or other white-spaces into fullname . 不会将空格或其他空格保存为fullname Entering a full name like "John Doe" will save "John" into fullname and "Doe" into password . 输入全名(例如“ John Doe”)会将"John"保存为fullname ,将"Doe"password

Avoid using scanf() . 避免使用scanf()


Rather than use scanf() to read user input, read user input with fgets() . 而不是使用scanf()读取用户输入,而是使用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. 可以添加其他代码来检查文件结尾,超长行, int范围测试等。

Use c library function fgets(). 使用c库函数fgets()。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM