带空格的字符串输入 [英] string input with spaces

查看:169
本文介绍了带空格的字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include< 

我试图在基本的ubuntu gcc编译器中为基本的C类运行以下代码。 ; stdio.h中>

struct emp
{

int emp_num,basic;
char name [20],department [20];

};

struct emp read()
{
struct emp dat;

printf(\\\
Enter Name:\\\
);
scanf(%s,dat.name);

printf(输入员工编号);
scanf(%d,& dat.emp_num);

// printf(Enter department:);
// fgets(dat-> department,20,stdin);

printf(Enter basic:);
scanf(%d,& dat.basic);

返回dat;


void print(struct emp dat)
{
printf(\\\
Name:%s,dat.name);

printf(\ nEmployee no。:%d,dat.emp_num);

// printf(Department:%s,dat.department);

printf(\\\
Basic:%d\\\
,dat.basic);
}

int main()
{
struct emp list [10];

for(int i = 0; i <3; i ++)
{
printf(输入员工数据\ n%d:\ n,i + 1);
list [i] = read();
}

printf(\\\
输入的数据为:\\\
);

for(int i = 0; i <3; i ++)
{
print(list [i]);
}

返回0;
}

我希望名称能够接受空格。



当我输入结构的值时,问题就出现了。我能够在第一时间输入名称,但随后的迭代甚至不会提示我输入。



我试过使用 fgets scanf(%[^ \\\
],dat.name)
甚至 gets()(我很绝望),但每次都面临同样的问题。



第1个结构的输出很好,但剩下的就是垃圾,这个人的姓氏或只是空白。



有什么想法?

解决方案

问题在于 scanf(%[^ \ n,.. fgets 不会跳过上一行读取可能遗留的任何空格,特别是,在最后一行结尾处不要跳过换行符,因此如果该换行符仍在输入缓冲区中(当使用 scanf(%d,...)读取最后一行时), scanf 在没有读取任何内容的情况下会失败(在 name array中留下随机垃圾),而 fgets 只会读取换行符。



最简单的修复方法是在scanf跳过空格:

  printf(\\\
输入名称:\\\
);
scanf( %19 [^ \\\
],dat.name);

o跳过行开头的空格(空行),如果你想要一个以空格开头的名字,可能会出现问题。



注意我还添加了一个长度限制 19 以避免名称数组溢出 - 如果用户输入一个更长的名称,其余部分将留在输入上并成为阅读为employeee数字。您可能想跳过该行的其余部分:

  scanf(%* [^ \\\
]);

这将读取输入中留下的任何非换行符并将其丢弃。您可以将此与以前的scanf结合使用,为您提供如下代码:

  printf(\ n Enter Name: ); 
scanf(%19 [^ \\\
]%* [^ \\\
],dat.name);
printf(输入员工编号:);
scanf(%d%* [^ \\\
],& dat.emp_num);
printf(Enter department:);
scanf(%19 [^ \\\
]%* [^ \\\
],dat.department);
printf(Enter basic:);
scanf(%d%* [^ \\\
],& dat.basic);

这会忽略任何有人在一条线上输入的虚假额外东西,但仍然会有人遇到问题在需要输入数字的位置输入字母,或输入文件结束条件。为了处理这些问题,你需要检查scanf的返回值。


I'm trying to run the following code in the basic ubuntu gcc compiler for a basic C class.

#include<stdio.h>

struct emp
{

  int emp_num, basic;
  char name[20], department[20];

};

struct emp read()
{
  struct emp dat;

  printf("\n Enter Name : \n");
  scanf("%s", dat.name);

  printf("Enter Employee no.");
  scanf("%d", &dat.emp_num);

  //printf("Enter department:");
  //fgets(dat->department,20,stdin);

  printf("Enter basic :");
  scanf("%d", &dat.basic);

  return dat;
}

void print(struct emp dat)
{
  printf("\n Name : %s", dat.name);

  printf("\nEmployee no. : %d", dat.emp_num);

  //printf("Department: %s", dat.department);

  printf("\nBasic : %d\n", dat.basic);
}

int main()
{
  struct emp list[10];

  for (int i = 0; i < 3; i++)
  {
    printf("Enter Employee data\n %d :\n", i + 1);
    list[i] = read();
  }

  printf("\n The data entered is as:\n");

  for (int i = 0; i < 3; i++)
  {
    print(list[i]);
  }

  return 0;
}

I want the name to accept spaces.

The problem comes when I'm entering the values to the structures. I am able to enter the name the first time but the subsequent iterations don't even prompt me for an input.

I've tried using fgets, scanf("%[^\n]",dat.name) and even gets() (I was desperate) but am the facing the same problem every time.

The output for the 1st struct is fine but for the rest is either garbage, the person's last name or just blank.

Any ideas?

解决方案

The problem is that scanf("%[^\n",.. and fgets don't skip over any whitespace that may be left over from the previous line read. In particular, they won't skip the newline at the end of the last line, so if that newline is still in the input buffer (which it will be when the last line was read with scanf("%d",..), the scanf will fail without reading anything (leaving random garbage in the name array), while the fgets will just read the newline.

The easiest fix is to add an explicit space in the scanf to skip whitespace:

printf("\n Enter Name : \n");
scanf(" %19[^\n]", dat.name);

This will also skip over any whitespace at the beginning of the line (and blank lines), so may be a problem if you want to have a name that begins with a space.

Note I also added a length limit of 19 to avoid overflowing the name array -- if the user enters a longer name, the rest of it will be left on the input and be read as the employeee number. You might want to skip over the rest of the line:

scanf("%*[^\n]");

This will read any non-newline characters left on the input and throw them away. You can combine this with the prior scanf, giving you code that looks like:

printf("\n Enter Name : ");
scanf(" %19[^\n]%*[^\n]", dat.name);
printf("Enter Employee no. : ");
scanf("%d%*[^\n]", &dat.emp_num);
printf("Enter department : ");
scanf(" %19[^\n]%*[^\n]", dat.department);
printf("Enter basic : ");
scanf("%d%*[^\n]", &dat.basic);

This will ignore any spurious extra stuff that someone enters on a line, but will still have problems with someone entering letters where numbers are expected, or end-of-file conditions. To deal with those, you need to be checking the return value of scanf.

这篇关于带空格的字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆