在c中扫描空白/整行

时间:2017-01-25 20:40:03

标签: c whitespace scanf

所以,我知道之前已经问过这个问题,但我似乎无法做任何事情。我现在拥有的是:

#include<stdio.h>

struct ClothingCustomer{
  char name[20];
  int age;
  double inseam;
};

struct ClothingCustomer createACustomer(){
  struct ClothingCustomer aCustomer;

  printf("Enter Customer Name: ");
  scanf("%s",aCustomer.name);

  printf("Age: ");
  scanf("%d",&aCustomer.age);

  printf("Inseam: ");
  scanf("%lf",&aCustomer.inseam);
  return aCustomer;
};

int main(){
  FILE* customersFile = fopen("customers.txt","w");
  for (int i = 0; i < 5; i++){
    struct ClothingCustomer aCustomer = createACustomer();
    fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,     aCustomer.inseam);
  }

  fclose(customersFile);
  return 0;
}

无论我做什么试图让它扫描多个单词,比如名/姓或什么的,它都有效,但这是我在运行时在控制台中得到的结果(带扫描)尝试通过下面列出的空白区域的选项;上面的代码正常运行,但不允许空白区域):

Enter Customer Name:
Age:
Inseam:
Enter Customer Name: Age:
Inseam: Enter Customer Name: Age:
Inseam:
Enter Customer Name: Age:
Inseam:
Enter Customer Name: Age:
Inseam:

怎样才能让它不这样做?我尝试过使用:

[^\n]
fgets(name, sizeof(name), stdin);

每次都会发生同样的事情。

2 个答案:

答案 0 :(得分:1)

这将工作

  #include<stdio.h>
  #include<string.h>
  struct ClothingCustomer createACustomer(void);
  struct ClothingCustomer{
       char name[20];
             int age;
       double inseam;
                    };

       struct ClothingCustomer createACustomer(void){
       struct ClothingCustomer aCustomer;
       {                 //From Here Starts The Part in Which You Are Having Problems.
       char c;
        int i;
       printf("Enter Customer Name: ");
       scanf("%s",aCustomer.name);
       i = strlen(aCustomer.name);      // length of user input till first space
       do{
       scanf("%c", &c);
       aCustomer.name[i++] = c;  // reading characters after first space (including it)
       }while (c != '\n');     // until user hits Enter

       aCustomer.name[i - 1] = 0;       // string terminating
       }
       printf("Age: ");
       scanf("%d",&aCustomer.age);

       printf("Inseam: ");
       scanf("%lf",&aCustomer.inseam);
       return aCustomer;
       };

       int main(){
             FILE* customersFile = fopen("customers.txt","w");
             int i = 0;

             for (i = 0; i < 5; i++){
             struct ClothingCustomer aCustomer = createACustomer();
             fprintf(customersFile, "%s %d %lf\n", aCustomer.name, aCustomer.age,aCustomer.inseam);
             }
             fclose(customersFile);
             return 0;
             }

我强烈建议你看一下这个answer,它会对你有所帮助,我在这里使用的方法在上面提到answer。请给出{{3}如果此方法适合您。 以下是您遇到问题的部分的解释,现在是如何工作的。

  
    

这是如何运作的?当用户从标准输入中输入字符时,它们将存储在字符串变量中,直到第一个空格。之后,其余的条目将保留在输入流中,并等待下一个scanf。接下来,我们有一个for循环,它从输入流中获取char(直到\ n)并将它们附加到字符串变量的结尾,从而形成一个与键盘用户输入相同的完整字符串。

  

答案 1 :(得分:0)

不清楚为什么scanf(" %19[^\n], aCustomer.name) failed为OP。

不是将scanf()用于复杂输入,而是将用户输入与解析分开。   完全停止使用scanf()并使用fgets()来获取用户输入。使用sscanf()strtod()strtol()strtok()等进行解析。

务必检查用户输入的结果和解析函数的成功。

OP没有说明如何处理麻烦的输入。在这种情况下,下面返回一个归零的ClothingCustomer。其他错误代码或错误消息可能很有用。

struct ClothingCustomer createACustomer(void) {
  // Suggest initializing
  struct ClothingCustomer zero = { 0 };
  struct ClothingCustomer aCustomer = { 0 };
  char buffer[100];

  printf("Enter Customer Name: ");
  fflush(stdout);  // insure prompt is seen before asking for input
  if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero;
  buffer[strcspn(buffer, "\r\n")] = '\0';  // lop off potential line ending
  if (strlen(buffer) >= sizeof aCustomer.name) return zero;  // too long
  strcpy(aCustomer.name, buffer);

  printf("Age: ");
  fflush(stdout);
  if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero;
  if (sscanf(buffer, "%d", &aCustomer.age) != 1) return zero;
  // Let us do some range checking
  // https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people
  if (aCustomer.age < 0 || aCustomer.age > 122) return zero;


  printf("Inseam: ");
  fflush(stdout);
  if (fgets(buffer, sizeof buffer, stdin) == NULL) return zero;
  if (sscanf(buffer, "%lf", &aCustomer.inseam) != 1) return zero;

  return aCustomer;
}
相关问题