User input stored in a char array (C ++)

I am trying to do a backup in my programming and find out something that I somehow missed (if I don't understand the problem). I am trying to prompt the user to enter a project name and I want to store that name in a variable. Normally I would use getline()

or std::cin >> std::string some_string

, but the assignment asks me to do this without using strings. "You cannot use the string class - use a character array instead." This is the question: how to take user input and store it in a character array?

The user has to type in a word and then press Enter ... How can I capture this into a character array? If the word came in one char at a time, I could just add it to the array, dynamically expanding if needed, but when it enters the char block then I'm at a loss for ideas. Thank!

+3


source to share


2 answers


The member function cin.getline()

allows you to specify the buffer and character length.

char name[32];
cin.getline(name, 32);

      



Although, any C ++ assignment that asks you to use character arrays instead of strings (especially in this context) is suspicious.

+5


source


Try the following:



char arr[100];
cin >> arr;

      

0


source







All Articles