简体   繁体   English

在结构中使用getline获取字符类型名称

[英]Taking char type name using getline in structure

As I declared a structure array: struct name data[5]; 当我声明一个结构数组时:struct name data [5]; When I try to take input using cin.getline(data[i].full_name,75) (which I need) then after first time it skips the char input. 当我尝试使用cin.getline(data [i] .full_name,75)(我需要)进行输入时,则在第一次之后它会跳过char输入。 I searched on this site and used fgets but was of no use. 我在该网站上搜索并使用了fgets,但没有用。 The code is : 代码是:

#include<iostream>
using namespace std;

struct name
{
   char full_name[75];
   int number; 
};

void input(struct name data[])
{
   int i=0;
   while(i<5)
   {
      cout<<"Enter the name: ";
      fgets(data[i].full_name,75,stdin);
                   OR
      cin.getline(data[i].full_name,75)


      cout<<"Enter the number: ";
      cin>>data[i].number;
      i++;
   }
}

int main()
{
    int times=0;
    struct name data[5];
    input(data);    
}

here is my suggestion hope it helps: 这是我的建议,希望对您有所帮助:

void input(struct name data[])
{
   int i=0;
   int number;
   char asciNumber[75];
   while(i<5)
   {
      cout<<"Enter the name: ";      
      cin.getline(data[i].full_name,75);     
      cout<<"Enter the number: ";
      cin.getline(asciNumber,75);     
      try
      {
          number = atoi(asciNumber);
          data[i].number = number;
      }
      catch (...)
      {
          //cout << "error in number parsing" << endl;
          // i think its important to check validity of std input\
      }
      i++;
   }
}

Well, for starters, you can't use fgets () here, at all. 好吧,对于初学者来说,您根本不能在这里使用fgets ()。 The results of mixing the C library's stdin -based functions, and C++ library's std::cin are undefined. 将C库的基于stdin的函数与C ++库的std::cin混合的结果是不确定的。

But your real problem is this: 但是您真正的问题是:

cin>>data[i].number;

Your intent here is to read a line of text containing a number. 您的目的是阅读包含数字的一行文本。

As you know, each line of text is terminated by a newline. 如您所知,每行文本都以换行符结尾。

The >> operator will read the entered number, but it will not actually read the newline character that follows it. >>运算符将读取输入的数字,但实际上不会读取其后的换行符。

As a result of that, on the next iteration of the loop: 结果,在循环的下一次迭代中:

cin.getline(data[i].full_name,75)

All this will do, then, is immediately read the newline character after the entered number, instead of the next line of text. 然后,所有这些操作将立即在输入的数字之后而不是下一行文本中读取换行符。

You will need to replace your usage of the >> operator with another getline () that reads the next line of text into a std::string , and then use std::istringstream to convert it to a number. 您将需要用另一个getline ()替换>>运算符的用法,该方法将文本的下一行读入std::string ,然后使用std::istringstream将其转换为数字。

That's the cleanest implementation. 那是最干净的实现。 There are a couple of other possibilities, such as manually reading past the newline character after the number, or another, throw-away call to std::getline (). 还有其他几种可能性,例如手动读取数字后的换行符,或者另一个对std::getline ()的std::getline调用。

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

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