简体   繁体   English

带结构的c ++ getline()

[英]c++ getline() with structs

struct car
{
  string name;
  int year;
};    

int main() {
    int noOfCars;
    cout<<"enter no_ of cars : ";    
    cin>>noOfCars;    
    car* cars = new car[noOfCars];    
    for(int i=0;i<noOfCars;i++)
    {
        cout<<"Car #"<<i<<endl;
        cout<<"Name : ";    
        getline(cin,(cars[i].name));  //here is the problem 
        cout<<"\n year : ";    
        cin>>cars[i].year;    
        cout<<endl;
    }
}

something wron with taking a whole line as a string input to the name in the strcut,doesn't even take any thing and proceeds to ge the year directly ... :S ??? wron采取整行作为字符串输入到strcut中的名称,甚至不采取任何事情直接进入ge年...:S ???

it works with cin,but i want to take a whole line ! 它与cin一起使用,但我想要整行! and it works with strings globally defined,but not with this inside the struct 它适用于全局定义的字符串,但不在结构中使用

This has been explained so many times as chris says. 克里斯说,这已被解释过很多次了。

The problem is in your preceding input call 问题出在前面的输入调用中

cin>>noOfCars;

This reads a number, ie it reads digits, it does not read a newline. 这读取一个数字,即它读取数字,它不读取换行符。 You might type a newline but that doesn't mean that it gets read. 您可以键入换行符,但这并不意味着它会被读取。 In fact the newline is left over until the next time you read, which is your getline call. 事实上,换行将保留到下次您阅读时,这是您的getline呼叫。 So your first getline call reading the newline that has been left over from when you read the number of cars. 因此,当您阅读汽车数量时,您的第一个getline电话会读取已经遗留的换行符。

It doesn't surprise me that newbies get this wrong, but it does indicate that you should spent some time researching your problem before asking a question. 新手们错了,这并不让我感到惊讶,但它确实表明你在问问题之前应该花些时间研究你的问题。 This has been asked and answered hundreds of times before. 之前已经被问过并回答了数百次。

Insert cin.ignore( 1000, '\\n' ); 插入cin.ignore( 1000, '\\n' ); after getline . getline之后。

#include<string>
#include<iostream>
using namespace std;

struct car
{
  string name;
  int year;
};    

int main() {
    int noOfCars;
    cout<<"enter no_ of cars : ";    
    cin>>noOfCars;    
    car* cars = new car[noOfCars];    
    for(int i=0;i<noOfCars;i++)
    {
        cin.clear();


        cout<<"Car #"<<i<<endl;
        cout<<"Name : ";    
        getline(cin,(cars[i].name));  //here is the problem 

     cin.ignore( 1000, '\n' );

        cout<<"\n year : ";    
        cin>>cars[i].year;    
        cout<<endl;

    }
    return 0;
}

The problem is that cin>>noOfCars leaves the newline generated by the Enter key in the input queue. 问题是cin>>noOfCars在输入队列中留下了由Enter键生成的换行符。 The getline(cin,(cars[i].name)) is simply reading that newline character and assigns a null string to the car[i].name array. getline(cin,(cars[i].name))只是读取换行符并将空字符串赋给car[i].name数组。 And since at the end of your loop cin>>cars[i].year does the same thing over and over, getline(cin,(cars[i].name)) is always reading a newline character before it lets you input anything. 因为在你的循环结束时cin>>cars[i].year做同样的事情, getline(cin,(cars[i].name))总是在你输入任何内容之前读取换行符。

To fix this you just need to discard the newlines character after cin reads noOfCars and cars[i].year , which can be cleanly done by concatenating a cin.get() after your cin s like this: 为了解决这个问题,你只需要在cin读取noOfCarscars[i].year之后丢弃换行符,这可以通过在你的cin之后连接cin.get()来干净地完成:

cout<<"enter no_ of cars : ";    
(cin>>noOfCars).get();            //here is the solution
car* cars = new car[noOfCars];    
for(int i=0;i<noOfCars;i++)
{
    cout<<"Car #"<<i<<endl;
    cout<<"Name : ";    
    getline(cin,(cars[i].name));  //here is the problem 
    cout<<"\n year : ";    
    (cin>>cars[i].year).get();   //here is the solution 
    cout<<endl;
}

Try cin.getline(Cars[i].name,streamsize); 试试cin.getline(Cars[i].name,streamsize); Where streamsize is the maximum size of the string. 其中streamsize是字符串的最大大小。 I think that's the simplest form. 我认为这是最简单的形式。

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

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