0

I know there were a couple of other questions on this from a while back, but I looked over those and still couldn't figure it out. I'm trying to take user input in the form of a string, then loop through that string converting all of the uppercase to lowercase so that I can display it all in lowercase. Where am I going wrong?

int main() { cout << "Enter Text: "; string Text; getline(cin, Text); for(int i=0; i<Text.length(); i++) { if(islower(Text[i]) == false) { tolower(Text[i]); i++; } Text[i] = Text[i]; } cout << "Your text is: "; cout << Text; cout << "\n"; } 

I'm very new to C++ and I'd be lying if I said I had much of an idea even where I was going wrong. Line 11, where the for loop is says that it's trying to compare two different signs, but I don't know what that means or if that's the source of my problem. Line 15 where the tolower() is says that it's 'ignoring return value of function declared with pure attribute' but I still don't know what that means either. Please help.

3 Answers 3

7

A few points:

  • tolower returns the lowercase character if it exists ('A' becomes 'a', 'a' is unchanged, '9' is unchanged, etc.)
  • The line Text[i] = Text[i]; does not do anything, you want Text[i] = tolower(Text[i]);
  • There is no need to check if each character is lowercase, tolower will handle that for you

Simplified:

#include <iostream> using namespace std; int main() { cout << "Enter Text: "; string Text; getline(cin, Text); for (int i = 0; i < Text.length(); i++) Text[i] = tolower(Text[i]); cout << "Your text is: "; cout << Text; cout << "\n"; } 
Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest using the std library algorithm function transform to simplify and to make the code easier to read for you and others.

#include <iostream> //for cout and getline #include <algorithm> //for transform int main() { cout << "Enter Text: "; string Text; getline(cin, Text); //This will iterate over each character [Text.begin()-Text.end()] and then //replace it by a call to tolower with itself as a parameter transform(Text.begin(), Text.end(), Text.begin(), ::tolower); cout << "Your text is: "; cout << Text; cout << "\n"; } 

EDIT:

As Remy pointed out to correct way of implenting this is by using a proxy lambda since

the behavior of std::tolower is undefined if the argument's value is neither representable as unsigned char nor equal to EOF.

transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char c){ return std::tolower(c); }); 

2 Comments

This is not the correct way to use tolower() as a predicate for std::transform(). See this documentation for an explanation and example of the correct way.
Also this should be applied to asimes answer as well.
0

I'd recommend looking at the ascii tables to see the codes for upper case and lower case characters. http://www.asciitable.com/

bool islower(char in) { return !(char >= 'A' && char <= 'Z'); //we do this so if its a not an alphabet character we don't get false positives } char tolower(char in) { return char-'A' + 'a'; //essentially get its distance from the start of the //alphabet and add this distance to the lowercase (only works on uppercase) } 

Its all just about working with the ascii values to get what you want since all characters are essentially integers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.