1

how can i make this program to accept both upper and lower case id's. The id when read from file is of upper case.The id's present are in the form of S2345. Help please.

   cout << "Enter client ID TO Change email";
   cin >> ids;

  for(int i=0; i<rec; i++)

        if(client[i].ID == ids){
            cout << "\nEnter New email\n";
            cin  >> client[i].email;

        }
Raj
  • 11
  • 1
  • 1
    I find your question confusing: You ask about how to transform a string, but the code seems to call for a case-insensitive comparison. Both are possible, and usually one doesn't make sense if the other is needed. Which is it? – Kerrek SB Mar 24 '15 at 10:00
  • [islower](http://www.cplusplus.com/reference/cctype/islower/) and [toupper](http://www.cplusplus.com/reference/cctype/toupper/) – Arun A S Mar 24 '15 at 10:01
  • to be precise i dont want the id's to be case sensitive. wen i run this and wen i enter s2345 "s" being in lower case it doesn't accept it. i have to enter S in caps each time. – Raj Mar 24 '15 at 10:05

2 Answers2

0
#include <boost/algorithm/string.hpp>
#include <string>

std::string str = "Hello World";

boost::to_upper(str);
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
0

You can use std::transform() from #include <algorithm> library:

#include <algorithm> // for std::transform
#include <functional> // for std::ptr_fun
#include <cstring> // for std::toupper

int main()
{
    std::string ids;

    std::cout << "Enter client ID TO Change email";
    std::cin >> ids;

    // make the entire string uppercase
    std::transform(ids.begin(), ids.end(), ids.begin()
        , std::ptr_fun<int, int>(std::toupper));

    std::cout << ids << '\n';
}

It may be worth making a function wrapper for it:

std::string to_uppercase(std::string s)
{
    std::transform(s.begin(), s.end(), s.begin()
        , std::ptr_fun<int, int>(std::toupper));
    return s;
}

References: std::transform, std::ptr_fun

Galik
  • 47,303
  • 4
  • 80
  • 117