What if they supply more than 20 characters (null terminator!)?Originally posted here by avdven
If you want to use more than one character, you have to use an array of characters (also known as a string).
AJCode:#include <iostream> using namespace std; int main(void) { char userName[21]; // Lets you have up to 21 characters for a name. cout << "Please insert your name" << endl; cin >> userName; cout << "Hello and welcome " << userName << " to this really lame program" }
Easier and safer to use a string:Code:#include <iostream> #include <string> using namespace std; int main() { string userName; cout << "Name? >" << flush; getline(cin, userName); // get everything until the newline cout << endl; cout << "Hello and welcome " << userName << " to this really lame program" << endl; }




Byte Me
Reply With Quote