0

Here is my situation: I wrote a quick and dirty program to scan serial numbers off of a large number of items and store the data into a file. Unfortunately the real world process goes: scan, hit enter, scan again.

My question is can I get it to where I can avoid hitting enter repeatedly.

Sample input:

123YLR12

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int i=0;
string death;//was showing a third party that anything can be declared a variable.
int main()
{
 ofstream filevariable;
 filevariable.open ("SavedDataFile", ofstream::app);
 while (i==0) //infinite loop but it's fine i kill it with CTRL+C
 {
  cin>>death;
  cout<<endl;
  filevariable << death << endl;
 }
}

Since it's not a single character but a series of letters and numbers pulled off a scanner i can't use _getch() and I can't seem to find any sort of reference to how to avoid pressing enter.

Platform:

Ubuntu 14.04 32-bit.

Rahn
  • 4,787
  • 4
  • 31
  • 57
  • Have you thought about passing your input in as a command line argument or input file? – Greg Hilston May 23 '16 at 16:18
  • The scanner can't be set to send enter after scanning? That's what I've always done. – Sami Kuhmonen May 23 '16 at 16:19
  • There's no portable way to do that in c++. Try to access your keyboard input at low level as appropriate with your operating system. – πάντα ῥεῖ May 23 '16 at 16:22
  • We used to pass Enter automatically.. but I totally forgot how. It is doable so keep searching – Khalil Khalaf May 23 '16 at 16:26
  • _@Salvadorjer_ IMHO going with [ncurses](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) would be the most portable solution. – πάντα ῥεῖ May 23 '16 at 16:33
  • Also "it's fine because I use Ctrl-C" is not fine. That interrupts your program, which does not guarantee the output file gets flushed correctly. Instead, exit on EOF. – Zastai May 23 '16 at 16:54
  • Thanks for all the responses. I'm looking into ncurses now. As for the duplicate thread, the other ones are only looking for a single character and can use specific functions for that whereas i have a series (of varying size depending on the model) of characters as the input. And for Zastai :) i'm modifying it with an end condition to the loop followed by a return 0. – Salvadorjer May 23 '16 at 18:19

0 Answers0