Getline is skipped in C + +

Source: Internet
Author: User

#include "stdafx.h" #include "iostream" #include "math.h" #include "stdio.h" #include "cstdlib" #include "string" #include "CString" using namespace std; #define MAX 20struct player{string name;char sex;int High;}; int main (void) {struct player bit[3];int i;cout << ' press ENTER to start: '; for (i = 0; i<3;  i++) {cin.get (); cout << "************player" << I << "***************" << endl;cout << "now Input player "<< I <<" s name: "; Getline (cin, bit[i].name); cout <<" now input player "<< I &lt ;< "' s Sex:"; Cin >> bit[i].sex;cout << "Now input player" << I << "' s High:"; Cin >> Bi T[i].high;} cout << "##################################################" << endl;for (i = 0; i<3; i++) {cout << "  Player "<< i <<" ' s name is "<< bit[i].name <<" and he sex is "<< bit[i].sex <<" and The high is "<< bit[i].high << Endl;}}

In the first loop of the code above, I=0 executes normally, and I=1 skips the entry of the name, directly to the gender, and if I initial value is 1, then it skips at 2.

Baidu learned that the difference between CIN and getline.

Before you write the Getline () function, you must have used the carriage return.
Whether you enter a character, a number or a carriage return, a space, a getline () function receives

So the solution is to use getline once before the Getline function, the carriage return in the input stream is received, and then the normal input

Baidu knows the answer Http://zhidao.baidu.com/link?url=-67TNObP8QxnE7tROIXviIDcS1SPWA7t5GzBcLV5yxu8sRfVS8W_ H-0vr-hpw-acbpaalwdftfbgk3b5fnyqrq

Getline is skipped in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.