cin 问题求解
下面是我的程序:
- C/C++ code
#include <iostream>using namespace std;int main(){ char str1[10]; char str2[10]; int i; i = 0; do { cout << "\nEnter the first string:\n\n\t"; cin >> str1; cout << str1; cout << "\n\nEnter the second string:\n\n\t"; cin.get (str2, 5); cout << str2 << endl; } while (i ++ < 4); return 0;}
为何不能实现目标,
总是在还没有输入第二个字符串就结束了?
[解决办法]
cout << str1;
cout << "\n\nEnter the second string:\n\n\t";
cin.get();//加上这句
[解决办法]
应该是把输入str1时候的尾部的回车,读入到了str2,所以感觉是还没输入第二个字符串就结束了