Ncurses and C++ string concatenation not working properly - c++

I'm trying to display a statusbar using ncurses with "---" between items, like most terminal programs do.
I have the following code so far:
#include <iostream>
#include <cmath>
#include <string>
#include <curses.h>
using namespace std;
int main() {
initscr();
start_color();
init_pair(3, COLOR_WHITE, COLOR_RED);
getch();
attron(COLOR_PAIR(3));
string a = "--- Tickets: 455";
a.append(70, '-');
mvprintw(LINES-1, 0, a.c_str());
getch();
endwin();
}
The mvprintw should output the whole width of the screen until the right border. But in fact, only one "-" is appended.
I'm not sure if this is a an ncurses problem or if strings are not converted to c style strings properly. Could someone help me identify the issue?

Related

Why is color not working in the Ncurses library in C++?

I'm trying to write an ncurses program in c++ that uses color, except every time I compile it acts like my terminal does not support colors, rejecting me when I include a color check, and not printing in color when not checking colors. My terminal does support colors (the terminal itself is terminator term emulator w/ zsh, have tried on xterm and tty). Is there some option I need to add to g++ to tell it to compile with color support? I don't get any errors during compilation or runtime.
Here's my code if it helps:
#include <stdio.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
if(!has_colors()) {
printf("Terminal does not support colors \n");
return 0;
}
initscr();
start_color();
init_pair(1, COLOR_BLACK, COLOR_RED);
/* Get terminal dimensions */
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
/* Make X and Y variables global for later */
extern int x;
extern int y;
/* Print test text */
attron(COLOR_PAIR(1));
printw(" This text should be black on red ");
attroff(COLOR_PAIR(1));
refresh();
getch();
return 0;
}
The compiled program just spits out "terminal does not support colors" even though it does. Excluding the check still doesn't work, just spits it out in monochrome.
And before you ask, yes I googled it, and no it didn't help.

Capture the output of a QR scanner from a raspberry in C++

I have a QR scanner attached to my raspberry pi 3 B+. I need to capture the output of the scanner from C++ on raspbian. The scanner writes the read information to the window with the focus, like a keyboard input.
I have tried to capture the keyboard input with the ncurses library and other similar choices but sometimes I get an incomplete code, because I don't know when the read info ends, and I don't know when I have a complete input.
I tried something like this:
#include <stdio.h>
#include <iostream>
#include <ncurses.h>
#include <future>
using namespace std;
int main(int argc, char *argv[])
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
char buffer[1000];
while (true) {
getstr(buffer);
cout<< buffer << endl;
napms(500);
}
return 0;
}
The problem is that I may get the code in a fragmented way if it's too long or if the output slows down for whatever reason.

Farsi character utf8 in c++

i m trying to read and write Farsi characters in c++ and i want to show them in CMD
first thing i fix is Font i add Farsi Character to that and now i can write on the screen for example ب (uni : $0628) with this code:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"\u0628 \n";
wcout << L"ب"<<endl;
system("pause");
}
but how i can keep this character ... for Latin characters we can use char or string but how about Farsi character utf8 ?!
and how i can get them ... for Latin characters we use cin>>or gets_s
should i use wchar_t? if yes how?
because with this code it show wrong character ...
wchar_t a='\u0628';
wcout <<a;
and i can't show this character بـ (uni $FE91) even though that exist in my installed font but ب (uni $0628) showed correctly
thanks in advance
The solution is the following line:
wchar_t a=L'\u0628';
The use of L tells the compiler that your type char is a wide char ("large" type, I guess? At least that's how I remember it) and this makes sure the value doesn't get truncated to 8 bits - thus this works as intended.
UPDATE
If you are building/running this as a console application in Windows you need to manage your code pages accordingly. The following code worked for me when using Cyrillic input (Windows code page 1251) when I set the proper code page before wcin and cout calls, basically at the very top of my main():
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
For Farsi I'd expect you should use code page 1256.
Full test code for your reference:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
SetConsoleOutputCP(1256); // to manage console output
SetConsoleCP(1256); // to properly process console input
wchar_t b;
wcin >> b;
wcout << b << endl;
}

Unicode character Visual C++

I'm trying to make my program work with unicode characters.
I'm using Visual Studio 2010 on a Windows 7 x32 machine.
What I want to print is the queen symbol ("\ul2655") and it just doesn't work. I've set my solution to use unicode.
This is my sample code:
#include <iostream>
using namespace std;
int main()
{
SetConsoleOutputCP(CP_UTF8);
wcout << L"\u2655";
return 0;
}
Also, I've tried many other suggestions, but nothing worked. (eg. change the cmd font, apply chcp 65001, which is the same as SetConsoleOutputCP(CP_UTF8), etc).
What is the problem? It's for the first time I encounter such a situation. On linux, it's different.
Thank you.
Once I managed to print chess pieces on the console; there are several complexities involved here.
First of all, you have to enable UTF-16 mode on stdout; this is described here as well as here, and it's exactly as Mehrdad explained.
#include <io.h>
#include <fcntl.h>
...
_setmode(_fileno(stdout), _O_U16TEXT);
Then, even if the output reached the console correctly, on the console you may get garbage instead of the intended characters; this comes form the fact that, at least on my machine (Windows 7), the default console font doesn't support the chess pieces glyphs.
To fix this, you have to choose a different TrueType font which supports them, but to make such a font available you have to go through some hoops; personally, I found out that DejaVu Sans Mono works just fine.
So, at this point, your code should work, and code like this (the example I wrote in the past to test this issue):
#include <wchar.h>
#include <stdio.h>
#include <locale.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
enum ChessPiecesT
{
King,
Queen,
Rock,
Bishop,
Knight,
Pawn,
};
enum PlayerT
{
White=0x2654, /* white king */
Black=0x265a, /* black king */
};
/* Provides the character for the piece */
wchar_t PieceChar(enum PlayerT Player, enum ChessPiecesT Piece)
{
return (wchar_t)(Player + Piece);
}
/* First row of the chessboard (black) */
enum ChessPiecesT TopRow[]={Rock, Knight, Bishop, Queen, King, Bishop, Knight, Rock};
void PrintTopRow(enum PlayerT Player)
{
int i;
for(i=0; i<8; i++)
putwchar(PieceChar(Player, TopRow[Player==Black?i: (7-i)]));
putwchar(L'\n');
}
/* Prints the eight pawns */
void PrintPawns(enum PlayerT Player)
{
wchar_t pawnChar=PieceChar(Player, Pawn);
int i;
for(i=0; i<8; i++)
putwchar(pawnChar);
putwchar(L'\n');
}
int main()
{
#ifdef _WIN32
_setmode(_fileno(stdout), _O_U16TEXT);
#else
setlocale(LC_CTYPE, "");
#endif
PrintTopRow(Black);
PrintPawns(Black);
fputws(L"\n\n\n\n", stdout);
PrintPawns(White);
PrintTopRow(White);
return 0;
}
should work equally well on Windows and Linux.
Now you still have a problem: the glyphs will be too small to be meaningful in any way:
this can be solved only by enlarging the console font, but you'd get all the other characters to be way too big to be usable. Thus, all in all, probably the best fix is just to write a GUI application.
Try this instead
_setmode(_fileno(stdout), _O_U16TEXT);

Can't launch website in C++

Trying to launch a website in Visual C++ 2010 Express, every researched solution has not worked. So I gather I need more in-depth assistance. Here's my code:
#include <stdafx.h>
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <string>
#include <string.h>
#include <conio.h>
#include <windows.h>
using namespace System;
using namespace std;
int main()
{
char name[240];
cout<<"\nHello, Please enter your name: ";
cin.getline(name,240);
cout<<"\nHello "<<name <<", your ID has been confirmed!";
cout<<"\nContinuing to launch website 'xyz' now.";
// system("C:\\Program%Files\\Google\\Chrome\\Application\\chrome.exe");
ShellExecute, TEXT("open"), TEXT(c:\\program
files\\google\\chrome\\application\\chrome.exe), NULL, NULL, SW_SHOWNORMAL;
goto end;
end:
cout<<"\n\nProgram completed, Pess any key to exit. ";
_getch();
return 0;
}
Like this.
ShellExecute(NULL, TEXT("open"), TEXT("c:\\program files\\google\\chrome\\application\\chrome.exe"), NULL, NULL, SW_SHOWNORMAL);
You had missing parens, missing double quotes and quite possibly spurious characters between 'program' and 'files' in your path. You also had a missing parameter to ShellExecute.
Reading a book and learning a minimum of C++ syntax would be a good idea. These are very basic errors.
system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" http://heise.de");
works for me.
See How to call an external program with parameters?