本blog僅用於備考筆試用:
-
cin() 與 getline()
cin 與getline() 都可以獲取屏幕輸入,但是cin遇到空格就歇菜了。示例如下:
cin 用法示例:
#include <iostream>
using namespace std;
int main(void)
{
string str;
cin >> str;
//getline(cin, str);
cout << "str = " << str << endl;
system("pause");
return 0;
}
cin 可以獲取屏幕輸入,但是遇到空格就停止,例如用戶輸入AB CD EF, cin函數只能獲取到AB,後面的就直接丟失;
getline()用法示例:
#include <iostream>
using namespace std;
int main(void)
{
string str;
getline(cin, str);
//cin >> str;
cout << "str = " << str << endl;
system("pause");
return 0;
}
如果獲取用戶輸入的一個字符,示例如下:
#include <iostream>
using namespace std;
int main(void)
{
char c;
cin >> c;
cout << "c = " << c << endl;
system("pause");
return 0;
}