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.
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 (unsignedint i = 1; i < strlen(str); i++)
// OR
for (size_t i = 1; i < strlen(str); i++)
constint 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).