当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Using mixed type of >> and getline

问题描述:

My task is to enter a string input into a char array. Here's what it's like :

char name[30];

cin>>name;

cin.getline(name,29);

cout<<name;

as with the input is : Ly Nguyen Khang

and the result only return Nguyen Khang

Please anybody help me understand what is wrong with this :(

part of my code :

#define INPUT_HELP_NAME "Please input name: "

#define INPUT_HELP_PHONE "Phone: "

cout << INPUT_HELP_NAME;

cin.getline(res->name, MAX_NAME_LENGTH-1);

cout << INPUT_HELP_PHONE;

cin >> res->phone;

when it runs , it shows something like this :

Please input name : Phone :

网友答案:

You cined it twice, causing it to not work. This is the correct way to do it:

char name[30];
cin.getline(name,29);
cout<<name;

The cin.getline(name,29); is already cined it, so there is no need for cin>>name;ing it, that makes your results change because your code said to cin it twice.

分享给朋友:
您可能感兴趣的文章:
随机阅读: