3

I've got a question about "catching output". putchar (tolower (VAR));<- This prints what I just entered via std::getline (std::cin, VAR); in lower case letters. I don't want to have this printed. How can I "catch" this output, to be not displayed on command prompt ?

Example: Input "TeSTIngThis" -> Output: "testingthis"

for (unsigned i = 0; i < VAR.length (); i++)
{
    char TEMP = VAR[i];
    VAR[i] = putchar (tolower (TEMP));
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
  • 6
    I'm not quite sure what you mean - what exactly don't you want printed? If you don't want it to print, why are you using putchar? – zennehoy Jun 21 '13 at 13:10
  • Thanks, just noticed it wasn't what I wanted at this place, haha. – Blacktempel Jun 21 '13 at 13:20
  • @zennehoy i second you on that. But i thought `c++` world people like to complicate things. j/k – DevZer0 Jun 21 '13 at 13:20
  • You maybe want to take a look on "pipes" – user2504380 Jun 21 '13 at 13:08
  • 2
    One comment, independently of the question: there is an almost universal convention in C++ circles that all caps are reserved for macros (with the possible exception of one character template parameters). Don't use all caps for variables. – James Kanze Jun 21 '13 at 13:45
  • Not using caps tho. Changed the names of the variables for the question. – Blacktempel Jun 28 '13 at 06:43

2 Answers2

2

It's not clear what you want. Your loop can easily be rewritten:

std::transform(
    var.begin(), var.end(),
    var.begin(),
    []( char ch) { tolower( static_cast<unsigned char>( ch ) ); } )

If, like most of us, you do not have C++11, you'll have to create a functional object for the tolower. But then, if you need this once, you'll likely need it again, and it makes sense to put such a functional object in your toolkit. (This is true even if you have C++11:

std::transform( var.begin(), var.end(), var.begin(), ToLower() );

is even clearer and simpler than the form with the lambda expression.)

Note to that I've eliminated the undefined behavior you proposed: you cannot directly call the one argument tolower with a char without risking undefined behavior.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

It sounds like what you need to do is use getch() or getche() in Windows and for Linux, you can use the method described in this page. With this, you can get character inputs from stdin, with or without echo to the stdout.

Community
  • 1
  • 1
ruben2020
  • 1,549
  • 14
  • 24