Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: tolower() help

  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    tolower() help

    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?

    -GT

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    shmoove

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you not split the input like into two parts the numbers and the letters.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #4
    New to the CF scene
    Join Date
    Feb 2004
    Location
    CA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Soy MrMike; 02-26-2004 at 05:04 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •