TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
- #include "stdafx.h"
- #include"iostream"
- #include"math.h"
- #include"stdio.h"
- #include"cstdlib"
- #include"string"
- #include"cstring"
- using namespace std;
- #define Max 20
- struct 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 >> bit[i].high;
-
- }
- cout << "##################################################" << endl;
- for (i = 0; i<3; i++)
- {
- cout << "player " << i << ""s name is" << bit[i].name << "and his sex is" << bit[i].sex << "and the high is" << bit[i].high << endl;
- }
- }
复制代码 上述代码第一个循环里,i=0执行正常,i=1的时候就会跳过输入名字这一项,直接到性别那里,如果i初值为1,则2的时候也跳过。
百度得知是cin和getline区别的问题。
- 在你写getline()函数之前,一定有使用过了回车了吧
- 不论你输入的是字符,数字或是回车,空格符,getline()函数都接收
- 而cin>>这种输入方式却是忽略回车的,如果你在getline()之前cin的一个数,回车被cin忽略了,却被[url=https://www.baidu.com/s?wd=getline%E5%87%BD%E6%95%B0&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mWwhmy7BnWI9PWPBnWnY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DYnWfLnHmsns]getline函数[/url]接收了,感觉就是这条语句被跳过了
- 所以解决的办法是在[url=https://www.baidu.com/s?wd=getline%E5%87%BD%E6%95%B0&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mWwhmy7BnWI9PWPBnWnY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DYnWfLnHmsns]getline函数[/url]之前再使用getline一次,将输入流里的回车符接收掉,后面就能正常输入了
- 百度知道的答案http://zhidao.baidu.com/link?url=-67TNObP8QxnE7tROIXviIDcS1SPWA7t5GzBcLV5yxu8sRfVS8W_H-0VR-HPW-ACbPAalWdFTfBGk3B5fnYqrq
复制代码
|
|