-
tolower function problem
Hi
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:
Code:
while(in_file == NULL);
while (fscanf ( in_file,"%d%s%s%d%f",
&slist[i].stocknumber,
slist [i].description,
slist [i].supplier,
&slist[i].quantity,
&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.)
-
tolower takes a single character -- if you want to do a whole string, use it on all the characters of the string.
-
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?
-
You should not use tolower on the string, but on each character in the string.
-
how would i go about doing that though? I don't understand this tolower function at all :(
-
>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.
-
Code:
void strtolower(char* str) {
for(int i = 0; str[i] != '\0'; i++)
str[i] = tolower(str[i]);
}