how do i make the program read uppercase and lowercase as the same?

Advertisements
string C;
cin>>C;
if (C=="add" ){
    int a=add(A,B);
    cout<<a<<endl;

I have here a simple condition where i am taking input "C" to ask the user to write which math operation they want to run. The user might input in capital letters or the first letter might be uppercase and the rest be lowercase. What should I do so that the program reads all the inputs weather uppercase or lowercase as the same?

>Solution :

Transform the user input into all lowercase or uppercase.

#include <algorithm>
#include <cctype>
#include <string>

std::string toLowerCase(const std::string& str) {
  std::string lower(str);
  std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
  return lower;
}

Leave a ReplyCancel reply