C++ cin get() Read char one by one from user and add to string

Description

C++ cin get() Read char one by one from user and add to string

#include <fstream.h>
void get_in_str(char str[], int len);
const int MAX=25; 
void main()//w w  w. j  av a2 s .c  o m
{
   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;
}



PreviousNext

Related