Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This method i already wrote, have a problem, Spaces are everywhere if user don't full all characters of strings,what should i do ?

C++
string CharacterLimitController(int limit)
{
	int count =0;
	char c;
	string A;
    A.resize(limit,' ');
	do
	{
	    c=getch();
		if(c==8)
		{
			if(count >0)
			{
			    cout <<"\b \b"; 
		   	    count -=1;
			}
			continue;
		}
		if(count==limit)
		{
			count -=1;
		    cout<<"\b";
		}
		if(c==13)
			break;
	    A[count]=c;
		cout << c;
	    count ++;
	}
	while(count!=limit+1);
       
    

	return A;
}
Posted
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 18:27pm    
What are you trying to achieve, exactly? What is "full a character"?
—SA

use a null character when user finish inputting. null character is zero
 
Share this answer
 
If you dont want to fill your output string with any additional char remove the line
C++
A.resize(limit,' ');
this method instructs to fill the empty spaces in the string empty space. see more about string.resizehere[^]
 
Share this answer
 
If you want to remove spaces at both ends, it is probably simpler to trim the string once the user get out of the loop.

Alternatively, you might not fill the string with space at the beginning but add one character to the end of the string as appropriate and when the user press backspace remove last character.

Given that you already keep the count, the following might be what you want:

C++
return A.substr(0, count);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900