Read char one by one from user and add to string - C++ File Stream

C++ examples for File Stream:cin

Description

Read char one by one from user and add to string

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
void get_in_str(char str[], int len);
const int MAX=25; 
void main()//from   w  w  w  . j av  a 2  s  .  com
{
   char input_str[MAX]; 
   cout << "What is your full name? ";
   get_in_str(input_str, MAX); 
   cout << "After return, your name is " << input_str << "\n";
   return;
}
void get_in_str(char str[ ], int len)
{
   int i = 0;   
   char input_char; 
   cin.get(input_char); 
   while (i < (len - 1) && (input_char != '\n'))
   {
      str[i] = input_char; 
      i++;                 
      cin.get(input_char); 
   }
   str[i] = '\0';   
   return;
}

Result


Related Tutorials