Thread: Header File and tolower issue with string

  1. #1
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34

    Header File and tolower issue with string

    I need to make a program that can accept a string input(I've already done this) and make all the letters lowercase. After this, the program needs to match up the string variable with other choices. I would try to make some kind of header file, but I don't really know how.

    Example: Enter a string
    Makes all letters lowercase
    reads to see if it matches choices
    if(string == "answer1")
    else if(string == "answer2")
    else if(string == "answer1")
    gives out correct input

    I had already set up a program to work like this, but it is extremely long.

    if(x == "Answer")
    else if(x == "answer")
    This isn't really a serious issue. Although, if someone can give me a soulution, I will be greatly happy.

  2. #2
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Quote Originally Posted by Inkinsarto View Post
    if(string == "answer1")
    else if(string == "answer2")
    else if(string == "answer1")
    sorry, that needs to be: if(string == "answer1")
    else if(string == "answer2")
    else if(string == "answer3")

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You've named the function you need to use - "tolower". Now all you need to do is google it and use it.
    Does that solve the problem?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Actually, I did add the tolower to the program, and when it ran, the input was displayed in all lowercase. That problem was fixed, but the program wouldn't check to see if the variable matched up with any choices.

    Example:
    enters a string:

    input=ApPlE
    (after tolower)

    "apple"
    (the program shows spectator the changed word, but it does not see if it equals the other choices) <--the problem
    cout<<"does not match up";


    The problem with this is that I Googled tolower many many many times. I did get the declaration and uses of it, but I still don't know how to see if the new lowercase string equals any choices.
    The program works perfect without tolower, but the user must enter a choice exactly as it is supposed to be entered. I can make the program to read the answer in many ways: apple, Apple, APple, etc. Although, I have to make a whole lot of if and else if statements or cases.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please post your code, then maybe we can see where your going wrong.

    Is your string a std::string or a C-string?

    How are you retrieving the string from the user?

    I suggest you post the smallest possible complete program that illustrates your problem.

    Jim

  7. #7
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Okay. But, It might take me awhile. I have the code at another location, and I am at school.(yes, I'm not professional)I will post the full code without the tolower so you can compile and run the program. So until then, please have patience.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why post the code without the tolower? Isn't this the part you are having trouble understanding?

    Jim

  9. #9
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Well you see, well um, yeah that sounds right. But it will take time. My email is [email protected] if you have anything else while I am offline. As of right now, I will try to post the code as soon as I possibly can.(with tolower)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg
    Why post the code without the tolower? Isn't this the part you are having trouble understanding?
    Exactly. The point is to post code, simplified from what you currently have, that demonstrates the problem. So the full code without that part is worth absolutely nothing since we are not trying to grade you, but to help you solve your own problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Wow. that's cool. okay. I will.

  12. #12
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Here is a very small part of the program that ends wher I wish to fix the tolower() problem.

    Code:
        string x;
    
        cout<<"Please enter an attribute. Then press ENTER.\n";
        cout<<"The choices are: \n";
        cout<<"1. Steel\n";
        cout<<"2. Dragon\n";
        cout<<"3. Fighting\n";
        cout<<"4. Lightning\n";
        cout<<"5. Earth\n";
        cout<<"\n";
        cout<<endl;
        cout<<endl;
        cout<<endl;
        cout<<"type: ";
        getline(std::cin, x); 
        tolower(x);
        if(x == "steel"){
        //run program
        }
        if(etc.)
    Here it is. Remember, I wish to make the full string lowercase and check to see if the newly changed variable equals any choices.

    Thanks.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that tolower is intended to work on a single character, returning the character in lowercase where applicable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Is there a way I can use transform()?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope issue with header file used as interface
    By Rath in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 05:04 PM
  2. Header File Issue CodeBlocks
    By mathguy in forum C Programming
    Replies: 8
    Last Post: 11-18-2011, 04:57 PM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  4. How to use tolower command for a string??
    By yong83 in forum C++ Programming
    Replies: 8
    Last Post: 04-01-2003, 05:44 AM
  5. String header file
    By Stan100 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 01:26 PM

Tags for this Thread