c++ cin input not working?

前端 未结 2 1384
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 11:41
#include 
#include 

struct Car{
    std::string model;
    unsigned int year;
};

int main(){
    using namespace std;

    int carNum         


        
2条回答
  •  梦毁少年i
    2020-12-18 11:45

    After every cin call using insertion operator. You must call cin.ignore() if you want cin.getline () to work.

    As cin>> leaves behind a '\n' character when you press enter and because of that when you use getline () it picks up the \n and takes no input as it finds \n in the input stream which is the default delimiter.

    So you can either do cin.ignore () after every cin>> or simply set a delimiter character cin.getline ()

提交回复
热议问题