How to limit input letters (characters) into a char array in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

const int TOTAL = 5;

int main()
{
	char arr[TOTAL] = { '\0' };

	cout << "Enter a string: ";
	cin.get(arr, 5);     

	cout << arr << endl;

	return 0;
}

/*
run:

Enter a string: abcdefghij
abcd

*/

 



answered Apr 5, 2018 by avibootz

Related questions

3 answers 380 views
2 answers 291 views
1 answer 295 views
1 answer 223 views
223 views asked May 20, 2018 by avibootz
1 answer 219 views
2 answers 276 views
1 answer 254 views
...