Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JanaL161
1 year ago

In c++ you can std::cin in a `string` stream:

#include 
#include 

std::string input;
std::cin >> input;
std::cout << input;

In c, you shoulddateline()` use1. This automatically saves memory and avoids bufferoverflows with UB or lost data:

#include 
#include 

char *input = NULL;
size_t len;

if (getline(&input, &len, stdin) != -1) {
  printf("%s", input);
}

free(input);

1 if "Dynamic memory extensions" are available. Otherwise you have to reserve a buffer manually and read it with fgets() or scanf().