#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 < ;< "' 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 + +