How to supress display of \n after input in c++? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have written a simple FORTH-like interpreter where I get input a line at a time and process it like this:
std::string line;
while (!std::cin.eof()) {
std::getline(std::cin, line);
std::istringstream input(line);
std::copy(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
std::back_inserter(data.input_));
// some work
}
And if I input 5 5 + . <enter> it displays like this:
5 5 + .
10 ok
But what I would like it show is:
5 5 + . 10 ok
In other words a newline should be accepted to mean end of line input but it should not be echoed to std::cout.
Having done some reading it seems I will not be able to achieve this in std c++ (is this true?) but I can use the functions in the termios library to set up the console this way on a POSIX system without messing with other console functions such as ctl-c handling etc. I haven't figured out exactly how though. Can anyone show me the way?

You can Use VT100 Escape Sequences for customising console output (VT100 ESC SEQUENCES). in this case;
use \033[1A for go 1 line up, \033[nC (n is int value) to Go n letter right

Related

Is it possible to create "quick time events" in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some knowledge about C++, but I stumbled upon an problem. I want the user to enter some text, but when it takes longer than X seconds, the program will go on. I guess, it is not possible, but maybe you know something about it.
It will be in the command line, without GUI.
I am not certain how the programm will look like, but it will be something like a CMD-RPG. I wanted to use Quick Time Events to make it a little bit more exciting.
I cant comment so I will just leave this here
Input with a timeout in C++
Since I cannot comment, I will simply leave this as an answer.
One possible way to solve this problem is to have 2 threads:
Input capture thread: since receiving input is a thread-blocking action, you should have a thread that simply polls for user input, placing that input into a thread-safe buffer
Quick-time function on main thread: a function that will be responsible for executing the quick-time event. Something like the following could be done (pseudo code):
clear input buffer //the buffer provided by the input capture thread
bool success = false;
while(current time < ending time)
{
if(input buffer isn't empty)
{
get contents of input buffer and send contents to cout
if (user has finished input correctly)
{
success = true;
break;
}
clear buffer
}
}
return success;
For this to work, you would need to turn off echo on the command prompt (see this page for more info)
Although Diogo's reference is excellent (and informative), I believe this answer is more applicable than since the OP has indicated that this program will be ran on Windows.

C++ Text Files usage [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am currently learning how to use text files, but when i put a condition like the one in the code below, even if the condition is true it won't try the while part. Can anyone help out?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s, d;
ofstream k("t1.txt");
int a;
cin >> a;
if (a > 3)
{
while (k.is_open())
{
getline(cin, s);
k << s;
k.close();
}
}
ifstream r("t1.txt");
while (r.is_open())
{
getline(r, d);
cout << d;
r.close();
}
}
I ran the code in gdb, it runs perfectly. It opens the file, gets the < cr > character from stdin, writes that to the first line which wipes out the first line, then tries to read the first line which is empty so there is no output. Good job 8).
You're just having trouble understanding your own expectations, your code works fine, so just think a little bit more about what you think it's supposed to do.
You'll also need to examine the contents of your text file before and after running the code, and try entering some text in the text file before you start the program, to see what happens to the text.
The getline where you try to read from the console is the issue, that returns without asking for input so it ends up giving you a blank line to write to the file.
From this link:
http://www.cplusplus.com/forum/beginner/39549/
There's a similar question there, and the first of the comments mentions this about the behavior of getline:
std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. .......
That's why it's not "doing anything" - it's not asking you for input, so the program has nothing to output and it looks like it's not working.
(this, by the way, is why programmers are snarky - we have to deal with stupid little behavioral issues of machines and we forget that PEOPLE are not normally like this and that PEOPLE hate being held to these kinds of standards)
I put a second getline in your code right after hte first one and now it asks for and outputs a string that I type in and sticks it in the file.
To run the program in gdb, do the following:
g++ -g < your cpp file > -o < whatever you want the binary to be called >
like this:
g++ -g myfile.cpp -o myrunnableprogram
this creates a binary with symbols that you can open in gdb
then
gdb myrunnableprogram
then in gdb
start
next
next
next
....
these commands will start the program and pause it at the first breakable spot, then each time you type next you will step to the next command and you can poke around at variables to see what's going on.
read up on gdb, it's really useful.

Why cout and cin would pop up error when I use ncurses..? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Emmm...I am writing a code that it can read the user input immediately after user press the key...
and I found the code online:
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
while (true) {
if (getch() == 'g') {
printw("You pressed G\n");
}
napms(500);
printw("Running\n");
}
and it worked pretty well..then I turn to use cout to print what I read in getch()...
I am now confused how nucurse.h handle the cout or I mean standard I/O..?
initscr tells (n)curses to write to the standard output. However, ncurses buffers its writes separately from the cout stream (see for example Output buffering in the ncurses6 release notes), and flushes its output when told to refresh. getch does a refresh call as a side-effect. There aren't other refresh calls in your example.
ncurses (like SVr4 curses) sets its input to raw mode, but that aspect is not related to your problem with cout.

Remove character from String c++/Arduino UNO [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to delete some characters from string. When i used erase it doesn`t work, compilation error erase no member named. Please help me. Probaly it is because I writing for Arduino UNO.
Arduino String class is quite different from std::string. For example erase doesn't exists. But there is method remove.
Anyway, you should start with: https://www.arduino.cc/en/Reference/HomePage
Expanding on what KIIV suggested, you could possibly do something like this:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
String words = "This is a sentence."; //reassign same string at the start of loop.
delay(1000);
Serial.println(words);
char c;
char no = ' '; //character I want removed.
for (int i=0; i<words.length()-1;++i){
c = words.charAt(i);
if(c==no){
words.remove(i, 1);
}
}
Serial.println(words);
delay(5000);//5 second delay, for demo purposes.
}
The Libraries in Arduino are customized in order to factor the memory constraints of the target Micro controller. For example , the Uno runs on a mega328P Atmel (now Microchip) device, which has only has 32 KB flash memory.

How to explain the strange output by "puts" in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am reading a piece of code written by others, there is one line like this:
cout << "Data Loaded" << endl;
it seems nothing strange, however, the actually output is:
[18607330327, 18790481919] [19144201237, 19327352831] [20754813973, 20937965567] [21291684885, 21474836479] [21474836482, 21653864362] [22011707392, 22190735274] [23622320128, 23801348010] [24159191040, 24338218922] [27197264917, 27204255743] [27205653525, 27212644351] [27230819349, 27230959453] [27233615872, 27235153757] [30064771072, 30067638186] [30073159680, 30076026794] [30098325504, 30098440106] [30098456576, 30098536200] Data Loaded
where does the extra output come from? if I comment that line, then, nothing is output.
I then include the <cstdio> and replace that line by puts("Data Loaded"), still, the extra info get printed.
cout is a buffered output stream, and endl not only creates a new line, it also flushes the buffer. Without the flushing of the buffer it might happen that you do not see the output of a previous cout.