C++ basic string input

Discuss all aspects of programming here.

Moderator: The Mod Squad

C++ basic string input

Postby dve83 » Fri Jun 24, 2005 8:28 am

hello, im trying to get a very basic cosole app to do things right, but now suddenly it doesn't seem so simple, plz help :(

I simply need to read text into a string variable of which the length should be at least 6 and max 90.

If I use getLine() from the string library I will only be able to determine the length after having entered the text. I would like to do so during the input. I know this sounds simple, but... id appreciate some help. thanks :oops:
-dve83- Folding for 33258
Gigabyte GA-MA69VM-S2,AMD Athlon X2 5200+, 6GB DDR2-800, Seagate 250GB 7200 RPM, Western Digital 1TB 7200RPM,
460W Coolermaster PSU, Windows 7

"Perhaps it's not paranoia. Perhaps it's just finesse." - dve83
dve83
Black Belt
Black Belt
 
Posts: 615
Joined: Sun Jun 29, 2003 3:58 pm
Location: South Africa, Cape Town

Postby olly » Sun Jul 31, 2005 7:34 am

It sounds possible by somehow reading a character at a time and storing them in an array of characters, but I'm having trouble grasping why you would want to do this. Surely its easier to read in the string, validate its the right length and then either request it to be entered again (if its the wrong length) or move on to the next procedure. :-k
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby dve83 » Sun Jul 31, 2005 8:51 am

yes, it is indeed easier to it that way, and eventually I decided to go with it, but: its not what i wanted to do. I was looking to verify each character as it was entered OR at least verify the length of the string while it was being entered. I feel it would irritate and end user enormously if he was to re-enter the complete sentence each time it was too long.
-dve83- Folding for 33258
Gigabyte GA-MA69VM-S2,AMD Athlon X2 5200+, 6GB DDR2-800, Seagate 250GB 7200 RPM, Western Digital 1TB 7200RPM,
460W Coolermaster PSU, Windows 7

"Perhaps it's not paranoia. Perhaps it's just finesse." - dve83
dve83
Black Belt
Black Belt
 
Posts: 615
Joined: Sun Jun 29, 2003 3:58 pm
Location: South Africa, Cape Town

Postby olly » Sun Jul 31, 2005 9:47 am

dve83 wrote:yes, it is indeed easier to it that way, and eventually I decided to go with it, but: its not what i wanted to do. I was looking to verify each character as it was entered OR at least verify the length of the string while it was being entered. I feel it would irritate and end user enormously if he was to re-enter the complete sentence each time it was too long.


Then the character array may have worked. My C is a bit rusty thesdays but I think its possible to read in a character at a time, store it in an array, validate it and then either request that character to be overwritten in the array or request the next character.

Here is some pseudo code as my C is not fresh in my mind:

int count = 0;
characterArray[90]

while(count<90)
{
characterArray[count] = getCharacter();

if(characterArray[count] != toWhatYouWantItToBE)
{
count = count --;
}
else
{
count++;
}
}


something like that. (very roughly and missing a few things like first time through the loop and controlling user input)
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby dve83 » Sun Jul 31, 2005 10:02 pm

hey and thanks for the reply. yip, a char array seems best. if you have any idea how to do it with a string type variable though, lemme know. Im still wondering wether its possible. I could find no method / function returning the length of a string.

again, thanks for the reply&code. :)
-dve83- Folding for 33258
Gigabyte GA-MA69VM-S2,AMD Athlon X2 5200+, 6GB DDR2-800, Seagate 250GB 7200 RPM, Western Digital 1TB 7200RPM,
460W Coolermaster PSU, Windows 7

"Perhaps it's not paranoia. Perhaps it's just finesse." - dve83
dve83
Black Belt
Black Belt
 
Posts: 615
Joined: Sun Jun 29, 2003 3:58 pm
Location: South Africa, Cape Town


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest