View Full Version : tolower() help

02-23-2004, 05:29 AM
For an assignment we need to be prompt the user to enter a department in the format of 3 letters then 3 numbers like itk168. We need to set it up to accept either caps or lower on the number part. How can I do it if I read in the department into a string. How do I go about converting the first 3 letters to lower and not mess with the numbers.

string dept;
cout << "Enter Department" << endl;
getline(cin, dept);

?how do I use tolower() here?


02-23-2004, 09:37 AM
Did you try using it directly on the string. I'm not too familiar with tolower(), but my guess would be that it justs changes letters and doesn't mess around with numbers and symbols.


02-24-2004, 10:07 PM
can you not split the input like into two parts the numbers and the letters.


Soy MrMike
02-26-2004, 05:01 AM
Instead of using getline() it would be easier to read the letters from user's character-input first, and then the integer afterwards:

string dept;
cout << "Enter Department" << endl;
// getline(cin, dept); getline() eats everything

char ch = cin.peek(); // peek() returns the next char that may be read
while(ch >= 65) { // while reading chars, 'A' == 65, 'B' == 66, etc
// ch = char(tolower(ch)) // uncomment this conversion if needed
dept += ch;
ch = cin.peek();

string remainderOfInput;
cin >> remainderOfInput; // get numbers
dept += remainderOfInput;

// cin.get() // kill '\n' if you want...

I think that's what you're trying to do.