0

I want to check if some C++ programs are running correct or not. The programs are similar, take single or multiple inputs from user and print out the output. To automate this, I compile a program and provide the input from file(windows cmd):

a.out < input.txt > output.txt

However, in the output, the End line or even the input is not printed. Example:

cout<<"Enter a number: ";
cin>>a;
cout<<"You entered: "<<a;

I create the input file and just write 4 in it. Then the output is:

Enter a number: You entered: 4

How do I make the program to output properly. I tried saving the input.txt after pressing "Enter", saving 4\n in the file and also tried piping: more input.txt | a.out > output.txt. But these didn't work. I don't want to modify the C++ program code and was wondering if there was a way to achieve this by the way we run the program or by changing input format.

  • 2
    It would be helpful if you said what you expect the output to be. It sounds like you expect the output to be `Enter a number: 4\n` when you redirect from a file, but that will never happen because stdin doesn't get echoed to your terminal like when you type it. – indiv Feb 15 '15 at 20:55
  • You will have to modify your code quite a lot to do this. – M.M Feb 15 '15 at 21:10
  • @Anurag Kalra I testet it with Visual Studio 2013 and Windows 8. I used Notepad to write a textfile with 4 (where is only hitting the return key) and it worked! Wich compiler and editor did you used? – Martin Schlott Feb 15 '15 at 21:19
  • @MartinSchlott: I am using g++ compiler(MinGW) . And using Notepad editor. I tried again. Here's the input what I want::::: Enter a number: 4 (line break) You entered: 4 And here's what I get::::::: Enter a number: You entered: 4 – Anurag Kalra Feb 15 '15 at 22:13
  • @indiv: Yeah, that's the behavior I want. But yeah, your point makes sense. Do you know of a workaround to make the cin echo the input from file to screen? – Anurag Kalra Feb 15 '15 at 22:17
  • @AnuragKalra: Sure, you could print it from your program. You'd have logic like `if( program is redirected from a pipe ) { std::cout << a << "\n"; }` after your `cin` line. How to tell whether your program's stdin is interactive or redirected depends on your operating system: http://stackoverflow.com/questions/1312922/detect-if-stdin-is-a-terminal-or-pipe-in-c-c-qt – indiv Feb 15 '15 at 22:21
  • @indiv: yeah, I looked at that thread. I don't want to modify the C++ programs. But it is fine if the input (and newline) is not echoed. I will just consider when checking if the result is correct or not. Thanks – Anurag Kalra Feb 15 '15 at 22:36

0 Answers0