endl, '\t' and '\n' dont work after 15 tabs - c++

If you compile and run this code, the endl doesn't get executed. You will get 0hello when you pop the terminal into full screen.
#include <iostream>
int main() {
using namespace std;
for (int i = 0; i < 15; i++) {
cout << '\t';
}
cout << "0" << endl << "hello";
return 0;
}
However, if you use cout << "00" << endl << "hello"; then it works fine. I don't understand why this happens nor how to fix it.

I am assuming you are running this from visual studio where the default terminal width is 120 characters.
A tab is 8 characters. 8x15 = 120.
If you look at the output, there is a blank line before the 0. It is printing the tabs: just that you've reached the end of line so it has moved to the next line.
If you change the terminal width to 80 characters you might get a different result - a blank line and the 0 in the centre of the page.

Related

How to load projects with encoding in Visual Studio 2022

I have to print some ASCII characters (extended version, code page 437) in console application as an exercise. Now, instead of writing cout << char(196) << char(196) << char(196);, I'd like to write cout << "───" to make a line. I have found that I can save files with the correct encoding by clicking "Save as" and choosing "save with encoding" (in this example - OER United States - Codepage 437). After doing that, the following code compiles and runs without any problems:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "\n *** Multiplication Table ***\n\n";
for (int i = 1; i < 11; i++)
{
if (i == 2) cout << "────┼────────────────────────────────────\n";
for (int j = 1; j < 11; j++)
{
if (j == 2) cout << "│";
cout << setw(4) << i * j;
}
cout << "\n";
}
}
However, when I close Visual Studio and reopen the project, suddenly all special ASCII characters are changed. For example, this bit "────┼────────────────────────────────────\n"; becomes "ÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"; in the code editor. However, the program still runs correctly and displays ────┼────────────────────────────────────.
If I try to save the project with encoding again, I am warned that there are characters that I cannot save with selected encoding and asked to use Unicode instead, which then messes up the characters again and also stops the program from showing the correct characters.
Question: How do I make Visual Studio 2022 use the correct encoding when opening projects?

unable get output on the same line with space in between the two output in c++

This is my code
#include<iostream>
using namespace std;
int main()
{
int s,count1;
long long N,R,max1;
cin>>s;
for (int i=0; i<s; i++)
{
cin>> N>>R;
}
cout<< N << R <<endl;
return 0;
}
for input
1
5 6
output
56
but I want the output as
5 6
I am good in c know how to do the same in c,now started learning c++ please help
The way the output stream is being read as is, it is printing the characters contained in N and R consecutively. You need to specify to print a space/tab. Your output line should be:
cout<< N << " " << R <<endl;
That will put a space between the two characters. If you want a tab (which might be nice if you're doing multiple lines of output and want everything to be lined up nicely), replace " " with "\t".
If you want a space to be outputted, then output one!
cout << N << R << endl says output N, output R and then output a newline and flush the buffer. If you want a space, you need to add it to the output:
cout<< N << ' ' << R <<endl;

Why do I obtain this strange character?

Why does my C++ program create the strange character shown below in the pictures? The picture on the left with the black background is from the terminal. The picture on the right with the white background is from the output file. Before, it was a "\v" now it changes to some sort of astrological symbol or symbol to denote males. 0_o This makes no sense to me. What am I missing? How can I have my program output just a backslash v?
Please see my code below:
// SplitActivitiesFoo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
string s = "foo:bar-this-is-more_text#\venus \"some more text here to read.\"";
vector<string> first_part;
fstream outfile;
outfile.open("out.foobar");
for (int i = 0; i < s.size(); ++i){
cout << "s[" << i << "]: " << s[i] << endl;
outfile << s[i] << endl;
}
return 0;
}
Also, assume that I do not want to modify my string 's' in this case. I want to be able to parse each character of the string and work around the strange character somehow.This is because in the actual program the string will be read in from a file and parsed then sent to another function. I guess I could figure out a way to programmatically add backslashes...
How can I have my program output just a backslash v?
If you want a backslash, then you need to escape it: "#\\venus".
This is required because a backslash denotes that the next character should be interpreted as something special (note that you were already using this when you wanted double-quotes). So the compiler has no way of knowing you actually wanted a backslash unless you tell it.
A literal backslash character therefore has the syntax \\. This is the case in both string literals ("\\") and character literals ('\\').
Why does my C++ program create the strange character shown below in the picture?
Your string contains the \v control character (vertical tab), and the way it's displayed is dependent on your terminal and font. It looks like your terminal is using symbols from the traditional MSDOS code page.
I found an image for you here, which shows exactly that symbol for the vertical tab (vt) entry at value 11 (0x0b):
Also, assume that I do not want to modify my string 's' in this case. I want to be able to parse each character of the string and work around the strange character somehow.
Well, I just saw you add the above part to your question. Now you're in difficult territory. Because your string literal does not actually contain the character v or any backslashes. It only appears that way in code. As already said, the compiler has interpreted those characters and substituted them for you.
If you insist on printing v instead of a vertical tab for some crazy reason that is hopefully not related to an XY Problem, then you can construct a lookup-table for every character and then replace undesirables with something else:
char lookup[256];
std::iota( lookup, lookup + 256, 0 ); // Using iota from <numeric>
lookup['\v'] = 'v';
for (int i = 0; i < s.size(); ++i)
{
cout << "s[" << i << "]: " << lookup[s[i]] << endl;
outfile << lookup[s[i]] << endl;
}
Now, this won't print the backslashes. To undo the string further check out std::iscntrl. It's locale-dependent, but you could utilise it. Or just something naive like:
const char *lookup[256] = { 0 };
s['\f'] = "\\f";
s['\n'] = "\\n";
s['\r'] = "\\r";
s['\t'] = "\\t";
s['\v'] = "\\v";
s['\"'] = "\\\"";
// Maybe add other controls such as 0x0E => "\\x0e" ...
for (int i = 0; i < s.size(); ++i)
{
const char * x = lookup[s[i]];
if( x ) {
cout << "s[" << i << "]: " << x << endl;
outfile << x << endl;
} else {
cout << "s[" << i << "]: " << s[i] << endl;
outfile << s[i] << endl;
}
}
Be aware there is no way to correctly reconstruct the escaped string as it originally appeared in code, because there are multiple ways to escape characters. Including ordinary characters.
Most likely the terminal that you are using cannot decipher the vertical space code "\v", thus printing something else. On my terminal it prints:
foo:bar-this-is-more_text#
enus "some more text here to read."
To print the "\v" change or code to:
String s = "foo:bar-this-is-more_text#\\venus \"some more text here to read.\"";
What am I missing? How can I have my program output just a backslash v?
You are escaping the letter v. To print backslash and v, escape the backslash.
That is, print double backslash and a v.
\\v

Using c++ setw to try to align second column

I'm trying to make sure that the second column in the output is aligned and it seemed like setw would be the solution but not matter what I do the second column is always off. This is the output I get from the code below...
1> 123
10> 234
but I want it to be...
1> 123
10> 234
The only other thing I can think of is to actually get the number of digits of what the actual number of elements are and the index then do some sort of length calc from that. That seems like a lot of handling just to get the second column right aligned.
I also tried << right but since I'm printing line by line in a loop this won't make a difference
int main()
{
int array[2] = {123,234};
int array2[2] = {1, 10};
for(int i = 0; i < 2; i++){
cout << array2[i] << "> " << setw(4) << array[i] << endl;
}
return 0;
}
Using setw ():
You need to set width for the array2[i] element instead of the array[i] element to get the alignment you are looking for.
cout << setw (2) << array2[i] << "> " << array[i] << endl;
Alternative Method 1:
Use printf for formatted printing here.
printf ("%-2d> %4d\n", array2[i], array[i]);
%-2d - the -2 left aligns the integer with width 2.
%4d - the 4 right aligns the integer with width 4.
Alternative Method 2:
Use tabs, or the \t character.
cout << array2[i] << ">\t" << array[i] << endl;
The \t moves your cursor bar to the next tabstop and so you end up getting data aligned in columns like you need. I would not recommend that you use this method because tab widths are unpredictable.
Use a single '\t' character, it TABs automaticly the other column. Example:
for(int i = 0; i < 10; i++)
cout << "x=" << (rand()%10000000+2000)/30 << "\t\ty=" << rand()%2000 << endl;
The output was:
x=+231096 y=+1383
x=+154630 y=+777
x=+141344 y=+1793
x=+325416 y=+1386
x=+321447 y=+649
x=+16400 y=+362
x=+84068 y=+690
x=+250530 y=+1763
x=+12847 y=+540
x=+115257 y=+1172
NOTE: I'm using g++ with flag -std=c++11 for C++11, I don't know its affects results.

C++ output formatting using setw and setfill

In this code, I want to have numbers printed in special format starting from 0 to 1000 preceding a fixed text, like this:
Test 001
Test 002
Test 003
...
Test 999
But, I don't like to display it as
Test 1
Test 2
...
Test 10
...
Test 999
What is wrong with the following C++ program making it fail to do the aforementioned job?
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
const string TEXT = "Test: ";
int main()
{
const int MAX = 1000;
ofstream oFile;
oFile.open("output.txt");
for (int i = 0; i < MAX; i++) {
oFile << std::setfill('0')<< std::setw(3) ;
oFile << TEXT << i << endl;
}
return 0;
}
The setfill and setw manipulators is for the next output operation only. So in your case you set it for the output of TEXT.
Instead do e.g.
oFile << TEXT << std::setfill('0') << std::setw(3) << i << endl;