Problem with tolower() and comparing strings?

I'm trying to get a library working for a project for class. All it is supposed to do is compare two strings and ignore case. We just started learning about libraries and this is the second one I've done so please bear with me. :)

driver.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include "strcmp_ncase.h"

using namespace std;

int main ( void )
{
        string str1, str2;
        bool result;

        cout << "\n\nEnter first string: ";
        cin >> str1;
        cout << "\nEnter second string: ";
        cin >> str2;

        result = strcmp_ncase(str1,str2);

        if ( result = true )
        {
                cout << "\nTRUE";
        }
        else cout << "\nFALSE";

        return 0;


strcmp_ncase.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cctype>
#include "strcmp_ncase.h"


using namespace std;

bool strcmp_ncase ( string& string1, string& string2 )
{
        string str1l, str2l;
        bool cmp;

        str1l = tolower(string1);
        str2l = tolower(string2);
        cmp = str1l.compare(str2l);

        return cmp;


strcmp_ncase.h:

1
2
3
4
5
6
7
8
9
10
11
#ifndef STRCMPNCASE_H_INC
#define STRCMPNCASE_H_INC

#include <string>
#include <iostream>
#include "strcmp_ncase.cpp"

std::bool strcmp_ncase ( string& string1, string&  string2 );

#endif


When I try to compile it, I get this:


driver.cpp***
strcmp_ncase.cpp...
In file included from strcmp_ncase.h:6,
                 from driver.cpp:3:
strcmp_ncase.cpp: In function `bool strcmp_ncase(std::string&,
std::string&)':
strcmp_ncase.cpp:14: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
strcmp_ncase.cpp:15: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
In file included from driver.cpp:3:
strcmp_ncase.h: At global scope:
strcmp_ncase.h:8: error: expected unqualified-id before "bool"
driver.cpp: In function `int main()':
driver.cpp:25: error: expected `}' at end of input
In file included from strcmp_ncase.h:6,
                 from strcmp_ncase.cpp:4:
strcmp_ncase.cpp: In function `bool strcmp_ncase(std::string&,
std::string&)':
strcmp_ncase.cpp:14: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
strcmp_ncase.cpp:15: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
In file included from strcmp_ncase.cpp:4:
strcmp_ncase.h: At global scope:
strcmp_ncase.h:8: error: expected unqualified-id before "bool"
strcmp_ncase.cpp: In function `bool strcmp_ncase(std::string&,
std::string&)':
strcmp_ncase.cpp:10: error: redefinition of `bool
strcmp_ncase(std::string&, std::string&)'
strcmp_ncase.cpp:10: error: `bool strcmp_ncase(std::string&,
std::string&)' previously defined here
strcmp_ncase.cpp:14: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
strcmp_ncase.cpp:15: error: no matching function for call to
`tolower(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'


It's probably a really dumb thing I'm not noticing, but I've been staring at this code for an hour now and I'm starting to get annoyed. Help is much appreciated, and thanks in advance! :)

The function tolower does not take a string, it takes a character.

e.g.

1
2
3
4
5
int lower = tolower('S');
cout<<(char)lower<<endl; //prints 's'

lower = tolower('+');
cout<<(char)lower<<endl; //prints '+' 


One possible solution is to loop through the string and call tolower on each character.

Also std::bool should be simpley bool.
Why are you including your source in your header?
1
2
3
4
5
6
7
8
9
10
#ifndef STRCMPNCASE_H_INC
#define STRCMPNCASE_H_INC

#include <string>
#include <iostream>
#include "strcmp_ncase.cpp" // why are you including your source in your header?

std::bool strcmp_ncase ( string& string1, string&  string2 );

#endif 

Don't do that. Just include your header in your source. :)


EDIT:



Also you are right not to do using namespace std; in your header, however you need to qualify your symbols:
1
2
3
4
5
6
7
8
9
10
#ifndef STRCMPNCASE_H_INC
#define STRCMPNCASE_H_INC

#include <string>
#include <iostream>

bool strcmp_ncase ( std::string& string1, std::string&  string2 );

#endif
Last edited on
You can use the transform() function to change a whole string to lower or upper. Syntax for changing a whole string to something would be:

string x = "Hello";

transform(string.begin(), string.end(), string.begin(), toupper)

note: change the toupper to tolower for lowecase, in case you didnt pick up on that :D
Topic archived. No new replies allowed.