Using cin in C++
I'd like to use cin and I used char for the int type (do you c开发者_运维技巧all it like that?) and it just shows one letter of what typed. How can I get the whole sentence?
Since you're using c++ why not use std::string
instead? Something like this should do what you're looking for:
#include <iostream>
#include <string>
int main()
{
using namespace std;
string sentence;
cout << "Enter a sentence -> ";
getline(cin, sentence);
cout << "You entered: " << sentence << endl;
}
use cin.getline()
char name[256];
cout << "What is your name?\n>";
cin.getline(name, 256);
cout << "Your name is " << name;
精彩评论