unsigned/signed error code

I am suppose to convert all 1st letter of each word in a sentences.
This is the function I created. I am getting an error code that signed/unsigned mismatch. please help

1
2
3
4
5
6
7
8
9
10
11
12

    void properWords(char str[])
	{
		
		for (int i = 1; i < strlen(str); i++)
		{
			if (str[i - 1] == ' ')
				str[i] = toupper(str[i]);
		}
		

	}
http://www.cplusplus.com/reference/cstring/strlen/
strlen() returns a size_t, which is commonly a typedef for unsigned int. As you are comparing an int (signed) to size_t (unsigned int), your compiler will get an unsigned mismatch error.
So i put unsigned in front of it and it still give me the same error code
void properWords(char str[])
{

for (int i = 1; i < (unsigned)strlen(str); i++)
{
if (str[i - 1] == ' ')
str[i] = toupper(str[i]);
}


}
Casting an unsigned type to an unsigned type doesn't do anything. The fact that you're comparing an int, which is signed, to size_t, which is unsigned, doesn't change. You'll need to change the int to an unsigned int or to size_t, so that you're comparing an unsigned type with an unsigned type.
1
2
3
for (unsigned int i = 1; i < strlen(str); i++)
// OR
for (size_t i = 1; i < strlen(str); i++)
Last edited on
Thank you for your help.
I know having issue with my main class can you help?
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
26
27
const int length = 100;
		char str[length];
		int choice;
		char again;
		cout << "Welcome to the Character Converter class! " << endl;
		do {
			cout << "What would you like to do?";
			cout << "\n 1. Converted all lowercase letters to uppercase";
			cout << "\n2. Convert the first letter of each word to uppercase";
			cin >> choice;
			if (choice == 1)
			{
				cout << "\nEnter a sentence to convert";
				cin.getline (str, length);
				upperCase(str);
			}
			else
			{
				cout << "Enter a sentence to convert: ";
				cin.get(str, length);
				properWords(str);
			}
			cout << "\nDo you want to do this again?";
			cin >> again;

		} while (again == 'y' || again == 'Y');
		return 0;

This is what it output::
Welcome to the Character Converter class!
What would you like to do?
1. Converted all lowercase letters to uppercase
2. Convert the first letter of each word to uppercase
1
Enter a sentence to convert: Do you want to do this again?

It stop to let me enter a sentence to even convert it

cin >> choice;
Whenever you use the extraction operator(>>), a new line feed('\n') is left in cin.


Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

http://www.cplusplus.com/reference/istream/istream/getline/
The default delimiting character is '\n'.

As a result, cin.getline() sees a '\n' in cin and stops reading.

To fix this, simply call cin.ignore(), after you cin.
http://www.cplusplus.com/reference/istream/istream/ignore/
when i input a sentences to convert it isn't converting why isn't my method working?
Topic archived. No new replies allowed.