C++ Tutorial/Data Types/string read


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Using cin to read a string from the keyboard

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char str[80]; 
 
  cout << "Enter a string: "; 
  cin >> str; // read string from keyboard 
  cout << "Here is your string: "; 
  cout << str; 
 
  return 0; 
}
Enter a string: string
Here is your string: string

Using gets() to read a string from the keyboard

#include <iostream> 
#include <cstdio> 
using namespace std; 
 
int main() 
{ 
  char str[80]; 
 
  cout << "Enter a string: "; 
  gets(str); // read a string using gets() 
  cout << "Here is your string: "; 
  cout << str; 
 
  return 0; 
}
Enter a string: string
Here is your string: string