Thread: tolower function problem

  1. #1
    Registered User
    Join Date
    May 2006

    tolower function problem


    I've got a program that reads a list from a text file. When the program reads the file, it displays all the items and allows you to search through it.

    The problem I'm having is that when I search through it, the case of the letters effects the results. I tried using the tolower function to make the case of the stored and input words all lower case but it says I am not allowed to convert char to int. I didn't think it was converting to int seeing as I am searching for words.

    The part that is giving me problems is:

    	while(in_file == NULL);
        	while (fscanf ( in_file,"%d%s%s%d%f",
    			slist [i].description,
    			slist [i].supplier,
    			&slist[i].price) != EOF) i++;
    	fclose (in_file);
    I've got the <cctype> header, and i is defined as an integer. So when I do something like, tolower(slist[i].description], it says invalid conversion from char to int.

    I don't know have I pasted enough code or even the right bit, but its basicly that and any advice would help.

    I'm trying to change all the letters stored under something like description to be altered into lowercase so it can be searched for regardless of the capital letters (the input from search will be turned into lower case too.)

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    tolower takes a single character -- if you want to do a whole string, use it on all the characters of the string.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2006
    I don't really understand what you mean I'm afraid. I'm still pretty new to this language, can you go into a little more detail?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You should not use tolower on the string, but on each character in the string.
    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

  5. #5
    Registered User
    Join Date
    May 2006
    how would i go about doing that though? I don't understand this tolower function at all

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    >I don't understand this tolower function at all
    This is what manuals are for, a Google search for "man tolower()" would tell you enough to use it properly.

    >how would i go about doing that though?
    Since the function takes a char at a time, you would need to loop through the string and call tolower individually. Just a guess.

  7. #7
    Registered User
    Join Date
    May 2006
    void strtolower(char* str) {
        for(int i = 0; str[i] != '\0'; i++)
            str[i] = tolower(str[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM