C++ Tutorial/Data Types/string read

Материал из 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