how many spaces are considered in \t - c++

why the number of space is different in case 3
how the result is getting effected by \t character.
(-) refers space by (\t)
case 1
void main()
{
int a,b;
printf("%d",printf("hello%d\t",scanf("%d%d",&a,&b)));
}
here the output is>hello2-7
case 2
void main()
{
int a,b;
printf("%d",printf("hello\t%d",scanf("%d%d",&a,&b)));
}
here the output is>hello-27
case 3
void main()
{
int a,b;
printf("%d",printf("\thello%d",scanf("%d%d",&a,&b)));
}
here the output is>--------hello27
Why in the 3rd case there are 8 spaces.

Most terminal programs will have a tab stop at every 8th column - so I'd expect output to be determined like this (I know your output's a little different - discussed below):
. column
. 1 2
input 12345678901234567890
"%d",printf("hello%d\t" hello2__7
"%d",printf("hello\t%d" hello___27
"%d",printf("\thello%d" ________hello27
To understand this, you have to understand the order of evaluation of your (unnecessarily complex) code. Examining the first printf line...
printf("%d",printf("hello%d\t",scanf("%d%d",&a,&b)));
Above, the arguments to the left-hand printf have to be prepared before it can print anything itself, and those arguments include the result of calling the right-hand printf. That right-hand printf outputs hello, the number of arguments scanf read from standard input which is 2 if you typed two, then the tab, then the right-hand printf has finished outputting and returns "7" to indicate how many characters it printed, which is printed by the left-hand printf. I would expect a tab to take you to the 9th column on screen, which suggests TWO spaces before the 7, where-as your question says you're observing 1. Clearly your terminal works a little different, probably considering the 8th, 16th, 24th etc. columns to be tab stops.
More about tabs
There is no universal interpretation of the \t TAB character... how it's rendered depends on the terminal software or rendering device you're using (e.g. an xterm, vt220, vt100 terminal, MS-DOS command window, printer, IDE, text editor etc.).
Some display/printing/formatting programs will consider there to be a tab stop every N characters, where N is often 8, such that if you issue a tab from the first column through to the 8th column you're taken to the 9th, a tab from the 9th to 16th column takes you to the 17th etc.. But, many programs will have ways to set arbitrary columns for tab placements. Some programs like MS Word can use variable-width fonts with which the number of characters between tab stops varies: if your C++ program prints some text that you import into Word you may find it practically impossible to work out how many tabs are needed to get the desired alignment of output - it's generally easier to just put one tab between values and change your tab stops inside Word so it all looks ok, or stick to a fixed-width font such as Courier.
C++ IDEs often let you set the value ("N" above) for columns per tab stop - 4 and 8 are both common settings, with 8 often meaning your source code indentation is a mix of tabs and spaces to reach the desired left-hand-column: that's kind of messy to navigate with naive cursor movement implementation. Many people prefer to set a "insert spaces when tab is pressed" option so the file is always saved with actual spaces, and displays more predictably with a wide variety of display/printing software.

A TAB has only the space given to it as it is rendered (so does any character, really); however, one subtle difference with tabs is that they are often taken to mean advance to the next "virtual column" (I'm sure there is a better term), where these virtual columns are, say, 8 characters wide; although this width can often be changed.
Here is an ugly graphic, where n..- represents a "virtual column" and T..t represents the space "taken up" by the tab:
1-------2-------3-------
hello\tworld helloTttworld
\thelloworld Tttttttthelloworld
hello\t\tworld helloTttTtttttttworld

in C99 and C11,
\t ( horizontal tab ) Moves the active position to the next horizontal
tabulation position on the current line. If the active position is at
or past the last defined horizontal tabulation position, the behavior
of the display device is unspecified.
while C++03 and C++11 don't specify the difference of '\t' with C.

\t does not contain any space. \t is a proper character that could be displayed with different length, but it's only one char.

As per compiler and computer software width would be changing either 4bits or 8 bits For example: here is c program compiled using compiler gcc 6.3 compiler on windows 10 pro where width 4 bits has taken,
#include <stdio.h>
int main(void) {
printf("a12345678patil\n");
printf("a\tpat\til");
}
output:
a12345678patil
a pat il

Related

C++ char spacing in console output, UTF-16 characters

I'm making a game in C++ console using UTF-16 characters to make it little bit more interesting, but some characters are different size then others. So, when I print the level, things after character are moved further than others. Is there any way how to add spacing between characters with some console function, I try to google something helpful, but I have not found nothing.
I tried to change font size by CONSOLE_FONT_INFOEX, but it changed nothing, maybe i implement it in the wrong way, or it not work with UTF-16 characters.
// i tried this
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
cfi.dwFontSize.X= 24;
cfi.dwFontSize.Y= 24;
Unfortunately I expect that this will heavily depend on the particular console you're using. Some less Unicode-friendly consoles will treat all characters as the same size (possibly cutting off the right half of larger characters), and some consoles will cause larger characters to push the rest of the line to the right (which is what I see in the linked image). The most reasonable consoles I've observed have a set of characters considered "double-wide" and reserve two monospace columns for those characters instead of one, so the rest of the line still fits into the grid.
That said, you may want to experiment with different console programs. Can I assume you are on Windows? In that case, you might want to give Windows Terminal a try. If that doesn't work, there are other console programs available, such as msys's Mintty, or ConEmu.
So, after some intense googling i found the solution. And solution is fight fire with fire. Unicode include character Thin Space, that is 1/5 of the normal space, so if i include two of them with one normal space after my problematic character, the output is diplaying how i want. If anybody runs into some simliar issue, unicode have lot of different sized spaces. I found Website that shows them all of them, with their propperties.
fixed output picture

(C++) Can the color of text change as it is typed?

I have a list of instructions in my program and they are activated by entering a string. There are a large number of possible instructions. You could call them commands if you like.
I already have a program that can successfully execute the instructions I've added so far.
For example, adding a person to the database would require the user to enter add "John" "Doe".
This would output to the screen Added John Doe to the database, ID#1234. The IDs are random.
I know how to add colors; in this output text, "John Doe" would be colored green.
What I'm wondering is, can I make it so that color changes as one types? Because I learned how to use some kind of keyboard mode change so that when I type a password, all characters are displayed as * of any color I desire, or even nothing displayed at all, through "display" of \0, and I know how to do that. I was wondering if the color could change before the complete string (extracted with std::getline(std::cin, str);) is typed.
The first reason I want the colors to change is because when there are so many commands, and I already have more complex commands than that, I want to provide a way for the user to be able to correct syntax mistakes before they press enter. Something like Windows PowerShell, perhaps, which was written in C#. I know that C# is a very different language than C++, but if C# can achieve something like that, I want to see if C++ can as well. My hope is that it doesn't require thousands of lines of application-specific code, especially considering that PowerShell is an actual application and not a simple terminal-run executable. And while PowerShell appears to be open-source, I don't understand C#. See the bottom for the second reason.
I have no idea if this is possible, but because similar manipulation of entered text is possible (as I said, I know how to add colors and also mask text as some other single character like *), I want to know if this is also possible.
Simple examples:
Firstly, the user should know when they have not entered a valid command, and when they have. I want the text to be in red until the letters entered so far consist of an actual keyword, like add. So, the text would be red until the second d is added, when it reverts to white, and if another letter is entered, it becomes red again.
Example 1: add "John Elias" "Doe"
After the space, the text after "add" should be red no matter what, unless the character after the space is a quotation mark. In order to tell the user that they have not terminated the string, the text beyond the (orange?) quotation mark should be orange. When the final quotation mark is entered, the entire content of the quotation marks (including the quotation marks) should be some other color (probably green?) to tell the user that they have successfully entered an argument. The same applies to any instances of quotation-mark arguments. Note that a space is allowed in a quotation argument.
Example 2: list-info 1234
In this command, it gets more complex. list is a separate command, so the text should be red until t is entered, and it turns white. But then it turns red again after that, until o is entered, and it turns white again. The numerical argument following it should be red if the entered character isn't a digit. If it is, it's still red, because the only valid IDs are 3- or 4-digit numbers. It should turn green(?) once a third digit is entered, and still stay green when another digit is entered. But if a fifth digit is entered (or another character for that matter), the number turns red again. Although this would better be implemented as returning an error if the entered number is invalid, I would still like to know if this can be done as well.
Example 3: add "John" "Elias" "Doe" "fourth-string"
Since there is an overloaded function that enables an explicit first-middle-last name to be stored as well, it should be ok if there is a third string. But if there is a fourth string added, then it should be in red no matter what because add cannot take more than 3 arguments.
My question is, are any of these things possible? And yes, I am aware that it is almost certainly better to just implement an error system, but my intention is to expand my coding ability, and that is the second reason, and coding an error system will not do that because I have already done that for every command.
For reference, I'm operating in Linux Ubuntu 18.04, I compile with g++, my code conforms to C++17, I use ANSI escape sequences for color, bold, etc., and for masking characters with something like * I use a pointer to a char array (passed by address as char**) and a C-style FILE* to reference the input stream stdin (because I haven't bothered to conform it to a typical C++ implementation yet, learning ways to advance my current skills is my priority at this point in time).

Tab '\t' has inconsistent spacing

I am using \t to space out my output, but it produces inconsistent spaces.
For example, the following code produces
#include <iostream>
int main()
{
std::cout << "Terms\tResults\tet(%)\tea(%)\n";
return 0;
}
Terms Results et(%) ea(%)
Note the difference in space
Why is this so?
Tab ends each 8th column.
v v v v v
1234567812345678123456781234567812345678
Terms Results et(%) ea(%)
The word "tab" is a prefix from "table" or "tabulate". The purpose of tabs is to generate vertical alignment, such as arrangement of text into table columns, or achieving consistent leading indentation for each paragraph. The purpose of tabs isn't to generate equal horizontal spacing. How much space is generated by a tab depends on how close it is to the next tab stop. That's why you're seeing the "difference in space".
The tab character, and its interpretation on terminal devices, originates with the Tab key on typewriters. The Tab key on a typewriter triggers the motion of the carriage, which comes to rest at at the next tab stop. Typewriter tab stops are configurable by the typist by moving mechanical sliders.
On character display devices, the ASCII TAB character works similarly: it advances to the next tab position. Tab stops are commonly every eight characters. (On some terminals they are configurable via menus in the firmware, or even escape sequences that the host computer can generate).
Modern word processing programs still imitate the typewriter tab stop sliders as wedge-shaped elements that can be added to a "ruler" and moved around.
Try this:
std::cout << "Terms\tResults\tet(%)\tea(%)\n";
std<<cout << "a\tb\t\tc\n"
You should see tabular alignment:
Terms Results et(%) ea(%)
a b c
Note we had to use two tabs after b because the Results et ... field overflowed a tab position.
Ideally, we should move the tab stops around based on the width requirements of the columns in our table. But moving tabs stops around is quite non-portable. For this reason, using tabs for vertical alignment in the output of computer programs is basically not very feasible; columnar formatting is better achieved using spaces. A good formatting function can provide for arbitrary field widths, with left, center or right alignment within a field.

C++ - Displaying and Storing Japanese Characters

So, I'm attempting to use C++ and Windows Forms to create an application that will help me study Japanese (for now, Hiragana and possibly Katakana only). The aim is to be able to create a program that has the user select the character sets they want to use (A through O, KA through KO, etc.), and either view the cards freely or have the program test them over the characters. For debugging purposes, I currently have the View button set to output 5 values to 5 different text boxes - the Roman pronunciation, the corresponding character, its position in an array in which all of the characters are stored, and a Boolean value.
My problem lies in the fact that the characters all show up as "?", and I get multiple warnings when I compile. An example of this warning:
1>c:\users\cameron\documents\visual studio 2010\projects\japanesecards\japanesecards\Form1.h(218): warning C4566: character represented by universal-character-name '\u3093' cannot be represented in the current code page (1252)
This shows up 46 times, 1 for each Japanese character in the array. The array's declaration line is,
std::string hiraList[5][11][2];
An example of inserting a Romanji-Hiragana pair is,
hiraCheck[0][0][0] = "A";
hiraCheck[0][0][1] = "あ";
Finally, the Hiragana is being inserted into a text box using the following code:
System::String^ displayText = gcnew String(hiraList[x][y][1].c_str());
textBox5 -> Text = displayText;
Basically, given all of this, my question is - How can I get my form to display Japanese characters properly in a text box?
Okay! I've done a bit of tweaking and experimenting with wchar_t, and I've found out a solution.
First, I reduced the hiraList array to a two-dimensional array, and moved the Hiragana characters into their own, array, defined like so:
wchar_t hiraChar[5][11];
And added values like so:
hiraChar[0][0] = L'あ';
Then, I went down to the code for the 'View' button and made a few changes:
Deleted the method for declaring and filling the displayText variable
Updated the line of code which assigns textBox5 its text value to read from hiraChar[x][y]
A line of the new code has been pasted below:
textBox5 -> Text = hiraChar[x][y].ToString();
In essence, the program now creates three variables for Hiragana - One to monitor check boxes, one to store the romanji values, and one to store Hiragana characters. When at least one check box is selected, and the View button pressed, five things are outputted to text boxes - the character, its position in the array (x and y are separate boxes), its romanji equivalent, and a 'True' value which was used earlier in development for debugging purposes.

why does cout.tellp always return -1?

I want to provide a tab-like capability for C++ text output streams. The feature should allow me to say "note this position", then allow multiple insert operations, and finally allow me to say "add enough fill characters so as to end up N characters past the originally noted position".
The standard iostream system does not seem to maintain a column position but I had thought that I could fake it using tellp(). My assumption was that the difference between tellp() at two points in my output sequence would correspond to the number of intervening bytes.
Unfortunately, at least in my Gnu C++ environment, cout does not maintain the fiction of a stream position. Every cout.tellp() call returns -1. Why is that?
tellp returns a position in a stream so that you can seek to it. Console does not allow seeking. Besides, even you interpret position as "the number of bytes written to the stream since it was created", that number won't be of any use for cursor positioning - the screen wraps around, its width is generally unpredictable. You just won't know what column you're on, since the row length is variable.
If you want reasonable cursor positioning on the screen, check out the ANSI terminal specification and escape commands that come with it. They allow for cursor position discovery and placement.
http://ascii-table.com/ansi-escape-sequences.php
In general, the screen is not a stream. Neither is the keyboard, for that matter :)