Getting string input from user using cin.getline
Collecting string like input from the user which could be a full line.
Learning Objectives
- Learning to collect whole line of data as string input to a character array.
- Learning to display string using the cout.
Source Code
|
|
Run Output
Code Understanding
char str[80]; //Here a storage space for string has been defined as maximum 79 chars. last one could be null char.
cout<<“Input your full name: ” ; //prompt for string input which may contain space or tab etc.
cin.getline(str,80);
This collects whole line of input and will store it in at variable referenced by str and upto 80 characters including the last null terminator can be stored.
cout<<“You Entered “<<str;
This is display of string entered by user.
Common Errors
- cin.getline(str,80) should contain same number of characters as of array size other wise some character may not be collected.
Suggested Filename(s): strinp-getline.cpp, strinput.cpp
Created: 12-Jan-2018 23:08 | Updated: 12-Jan-2018 23:38|
