ncurses getch() weird output? - c++

I have installed the ncurses.h lib and started experimenting with the getch() function.When I built and run this code which seems alright to me at first, the console printed out a weird character: '�'(if it doesn't show and shows as a space here is a screen shot: https://prnt.sc/gbrp7b) The console starts spamming it but if I type a character, it shows up in the output but still the '�' spams. Here is the code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
cout << "You entered : " << input << endl;
//break;
}
return 0;
}
So I thought of trying to use an if statement to try and stop it spamming but the code doesn't recognise the character:
It gives this error:
error: character too large for enclosing character literal type
For this code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
if(input!='�'){
cout << "YOu entered : " << input << endl;
}
}
return 0;
}
I am on OSX Sierra 10.12.5 and using eclipse Oxygen

You need to initialize ncurses with initscr() and close it with endwin() functions:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
initscr();
while (true) {
input = getch();
cout << "YOu entered : " << input << endl;
}
endwin();
return 0;
}

Related

Command system("pause") not found on Linux

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main() {
string line;
cout << "HW\n";
getline(cin,line);
cout << "Your line is - " << line << "\n";
system("pause");
return 0;
}
I want to do gui to factorio headless server by myself so i need to exec few bash scripts. I think i need function system() to that ?
I think I got problem with lib path. Please don't blame to wrong installed vcpkg. Paths is :
/opt/factorio/bin/x64/vcpkg/installed
/usr/include/c++/9/x86_64-redhat-linux
/usr/include/linux
/usr/include/c++/9/tr1
Command system() not found says Visual Studio.
system("pause"); is meant to be used only on Windows. It runs the Windows command-line "pause" program and waits for that to terminate before it continues execution of your program. That's why it's a bad practice to use it in your code, no matter if you are running your code on Windows or Linux.
Here is a better way you can achieve the same result:
#include <iostream>
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
instead of:
#include <iostream>
using namespace std;
int main() {
system("pause");
return 0;
}

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

Launching a program with Voce

I've compiled with success my first program with the Voce library but when I start the program, it stops right after. I can't say any word and it stops
#include <iostream>
#include <string>
#include <voce.h>
#include <windows.h>
using namespace std;
int main()
{
voce::init("voce/lib", false, true, "voce/lib/grams", "digits");
while (voce::getRecognizerQueueSize() > 0)
{
std::string s = voce::popRecognizedString();
std::cout << "You said: " << s << std::endl;
}
system("PAUSE");
return 0;
}
I took this code on : http://voce.sourceforge.net/
Thank you for your help

C++ std::cout and string printing in wrong order

I have a simple program running on Linux using g++ compiler:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv){
fstream file;
string s;
file.open("sample/dates.dat", fstream::in);
if(!file.good())
return 0;
getline(file, s);
cout << s << "." << endl;
return 0;
}
Compiled with: g++ -o test test.cpp. When I run this, the fullstop is printed BEFORE the string s, not after. Does anybody know why this is happening? And is it easy to fix?
Thanks.
If there is a carriage return at the end of the string it will move the position of output to the beginning of the console line when printed.
#include <iostream>
int main()
{
std::cout << "some line\r" << "." << std::endl;
// ^^ carriage return
}

Making C++ pause

Is there a C++ equivalent to Python's time.sleep()?
Use boost::this_thread::sleep
// sleep for 5 seconds
boost::this_thread::sleep(boost::posix_time::seconds(5));
The following code will sleep for 10 milliseconds.
boost::this_thread::sleep(boost::posix_time::milliseconds(10))
Refer to boost::posix_time::time_duration for more ways to construct the duration.
I'm not aware of any portable function, but mainstream OSes have usleep for *nix and Sleep for Windows.
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch