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 247 views
2 answers 189 views
1 answer 207 views
1 answer 148 views
148 views asked May 20, 2018 by avibootz
1 answer 143 views
2 answers 174 views
1 answer 164 views
...