Click here to Skip to main content
11,480,938 members (63,084 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
This method i already wrote, have a problem, Spaces are everywhere if user don't full all characters of strings,what should i do ?

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 26-Dec-12 2:43am
Comments
Sergey Alexandrovich Kryukov at 26-Dec-12 18:27pm
   
What are you trying to achieve, exactly? What is "full a character"?
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use a null character when user finish inputting. null character is zero
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you dont want to fill your output string with any additional char remove the line
 A.resize(limit,' ');
this method instructs to fill the empty spaces in the string empty space. see more about string.resizehere[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:

return A.substr(0, count);
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 294
1 OriginalGriff 145
2 CPallini 135
3 CHill60 130
4 Member 10254688 65
0 Sergey Alexandrovich Kryukov 8,184
1 OriginalGriff 7,701
2 Sascha Lefèvre 3,114
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100