Formatting Cout Output in C++ - c++

This is the expected output:
COUNT | WORD
------+------
1 | .3
1 | .3.4
1 | 3
2 | 12.34
1 | test1.12.34
3 | this
This is my proper code:
std::cout << "COUNT | WORD" << '\n';
std::cout << "------+------" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3.4" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << "3" << '\n';
std::cout << std::setw(4) << "2" << std::setw(3) << '|' << std::setw(3) << "12.34" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << "test1.12.34" << '\n';
std::cout << std::setw(4) << "3" << std::setw(3) << '|' << std::setw(3) << "this" << '\n';
Unfortunately, my ouput's messy the WORD
COUNT | WORD
------+------
1 | .3
1 |.3.4
1 | 3
2 |12.34
1 |test1.12.34
2 |this
Could anyone suggest me a solution for that. Thanks

Instead of having
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3" << '\n';
For each line, add a space after the '|' character:
std::cout << std::setw(4) << "1" << std::setw(3) << "| " << std::setw(3) << ".3" << '\n';

Why not this ::
std::cout << "COUNT | WORD" << '\n';
std::cout << "------+------" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << ".3" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << ".3.4" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << "3" << '\n';
std::cout << std::setw(4) << "2" << std::setw(3) << '|' << ' ' << "12.34" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << "test1.12.34" << '\n';
std::cout << std::setw(4) << "3" << std::setw(3) << '|' << ' ' << "this" << '\n';

Doing this will set the left hand side filler character.
cout.fill('-');
cout.width(40);
cout<< "LINE1" <<endl;
cout.fill('-');
cout.width(40);
cout<< 3 <<endl;
cout.fill('-');
cout.width(40);
cout<< 3.4 <<endl;
cout.fill('-');
cout.width(40);
cout<< "TEST " << 12.34 <<endl;

Related

Why std::cout print a Octal Number without a 0 in the beginning of the number

If I run this snippet of code
int main()
{
int i = 8;
std::cout << std::oct << i;
}
The console shows 10.
Shouldn't give 010, in C++ Octal Numbers start with 0.
You can use std::showbase for this.
#include <iostream>
int main()
{
int i = 100;
std::cout << "100 without showbase:\n";
std::cout << "decimal: " << std::dec << i << "\n";
std::cout << " hex: " << std::hex << i << "\n";
std::cout << " octal: " << std::oct << i << "\n";
std::cout << "100 with showbase:\n";
std::cout << "decimal: " << std::showbase << std::dec << i << "\n";
std::cout << " hex: " << std::showbase << std::hex << i << "\n";
std::cout << " octal: " << std::showbase << std::oct << i << "\n";
return 0;
}
Output:
100 without showbase:
decimal: 100
hex: 64
octal: 144
100 with showbase:
decimal: 100
hex: 0x64
octal: 0144
Demo

setw working on alphabets but not on dashes and underscores

This is the code which gives my desired output using spaces, but I have to perform the task using setw which isn't working as I want(2nd code attached). Help please!
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << " _________________________________________________________" <<endl ;
cout << " / \\ " <<endl;
cout << " / \\ " <<endl;
cout << " / \\ " <<endl;
cout << " / \\ " <<endl;
cout << " / \\ " <<endl;
cout << " / \\ " <<endl;
return 0;
}
Desired code which doesn't works:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << setw(15) << "_________________________________________________________" <<endl ;
cout << setw(14)<< "/ \\ " <<endl;
cout << setw(13)<< "/ \\ " <<endl;
cout << setw(12)<< "/ \\ " <<endl;
cout << setw(11)<<"/ \\ " <<endl;
cout << setw(10)<<"/ \\ " <<endl;
cout << setw(9)<<"/ \\ " <<endl;
return 0;
}
The desired output:
_________________________________________________________
/ \
/ \
/ \
/ \
/ \
/ \
I don't understand what you expect setw to do. What it does is: It sets the width of the output. For example
std::cout << std::setw(5) << 12;
results in output
12
^ ^ 3 spaces
^ ^ total width = 5
This results in the desired output with more spaces in the front (because I was too lazy to count the exact amount, I'll leave that to you):
#include<iostream>
#include<iomanip>
int main()
{
std::cout << std::setw(70) << "_________________________________________________________" << std::endl ;
std::cout << std::setw(71) << "/ \\ " << std::endl;
std::cout << std::setw(72) << "/ \\ " << std::endl;
std::cout << std::setw(73) << "/ \\ " << std::endl;
std::cout << std::setw(74) << "/ \\ " <<std::endl;
std::cout << std::setw(75) << "/ \\ " <<std::endl;
std::cout << std::setw(76) << "/ \\ " <<std::endl;
}
Live Demo
For bonus points you can take a look at std::setfill too. It sets the character that is used to fill the output. For example:
std::cout << std::setfill('x') << std::setw(5) << 12 << std::setfill(' ');
results in output
xxx12
std::setfill is sticky, so you have to reset it when you want the default space fill character again. With a loop your code can be just
std::cout << std::setw(15) << "_" << std::setw(40) << std::setfill('_') << "_" <<endl ;
std::cout << std::setfill(' ');
for (int i=0;i<6;++i){
std::cout << setw(14-i) << "/" << std::setw(43+i*2) << "\\ " <<endl;
}
You probably understood how setw works now, so here is a minimal working code based on your question. Simply add a dedicated character (whitespace or else) as a target for each setw:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(14) << ' ' << setw(59) << setfill('_') << '_' << setfill(' ') << endl;
cout << setw(14) << '/' << setw(60) << '\\' << endl;
cout << setw(13) << '/' << setw(62) << '\\' << endl;
cout << setw(12) << '/' << setw(64) << '\\' << endl;
cout << setw(11) << '/' << setw(66) << '\\' << endl;
cout << setw(10) << '/' << setw(68) << '\\' << endl;
cout << setw(9) << '/' << setw(70) << '\\' << endl;
return 0;
}
I checked, this code produces exactly your desired output.

Is it possible to get the valid hex address of CHAR in C++?

I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);

How to understand C++ std::setw 's inconsistent behaviour?

Given the following code:
/*Formatting Output
**Goal: practice using cout to format output to console
**Print the variables in three columns:
**Ints, Floats, Doubles
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 45;
float b = 45.323;
double c = 45.5468;
int aa = a + 9;
float bb = b + 9;
double cc = c + 9;
int aaa = aa + 9;
float bbb = bb + 9;
double ccc = cc + 9;
// 1st attempt :>
cout << "\n\n\n" << "// 1st attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << a << setw(15) << b << setw(15) << c << "\n";
cout << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
// 2nd attempt :>
cout << "\n\n\n" << "// 2nd attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << a << setw(15) << b << setw(15) << c << "\n";
cout << std::left << std::setfill(' ') << setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
// 3rd attempt :>
cout << "\n\n\n" << "// 3rd attempt :>" << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << a << setw(15) << b << setw(15) << c << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << std::left << std::setfill(' ') << std::setw(15) << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
cout << "12345678901234567890123456789012345678901234567890" << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << "Ints" << setw(15) << "Floats" << setw(15) << "Doubles" << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << a << setw(15) << b << setw(15) << c << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << aa << setw(15) << bb << setw(15) << cc << "\n";
cout << std::right << std::setfill(' ') << std::setw(15) << aaa << setw(15) << bbb << setw(15) << ccc << "\n";
return 0;
}
// https://repl.it/#Tredekka/Cpp-Understanding-stdsetw
... I get the following output:
gcc version 4.6.3
// 1st attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
// 2nd attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
4545.323 45.5468
54 54.323 54.5468
6363.323 63.5468
// 3rd attempt :>
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
12345678901234567890123456789012345678901234567890
Ints Floats Doubles
45 45.323 45.5468
54 54.323 54.5468
63 63.323 63.5468
... note: I was intentionally "inconsistent" with the code, "because" I'm trying to understand the behavior of the <iomanip> && std::setw() code.
If you look at the output from the 1st attempt, you'll notice that the header row "string" output is offset from the data rows "numerical" output... and while it's "mostly" accomplishing the purpose of aligning things in columns, it's both not accurate and not consistent.
In the 2nd attempt, you'll see that I've discovered that if I prepend my row output with:
<< std::left << std::setfill(' ') << setw(15)
... then I get the row to look correctly (as seen in the header & 2nd data row) ... but now you'll notice that the 1st & 3rd data rows are very wrong:
4545.323 45.5468
...
6363.323 63.5468
... how does "using/executing" ...
<< std::left << std::setfill(' ') << setw(15)
... affect "future" executions of setw()?
For completeness' sake, I've shown in the 3rd Attempt that it's possible to correctly & accurately align columns of data (either left or right) using <iomanip> && std::setw() ... but why the inconsistencies?
(#WhozCraig's answer helped me get to where I am, but did not delve deep enough to help me understand either: (a) why it 'pseudo' works || (b) why after you make it work correctly the 'first' time, it then breaks the 'pseudo' functionality.)
Here's your issue: std::setw() doesn't act as a buffer, it modifies the way the next expression gets evaluated.
You need to understand the "range" that each of these expressions has. Specifically, std::setfill(int) and std::left/std::right change the default behaviour, and seem last until they are overwritten by another setfill() or std::left/std::right. std::setw(int) on the other hand seems to only affect whatever is passed right after it (which is weird, because I feel like I've also seen it behave like std::setfill and the others before)
So, to sum up, what you want is something more akin to this:
int a = 45;
float b = 45.323;
double c = 45.5468;
int aa = a + 9;
float bb = b + 9;
double cc = c + 9;
int aaa = aa + 9;
float bbb = bb + 9;
double ccc = cc + 9;
std::cout << std::endl << std::endl << std::endl;
std::cout << std::left << std::setfill('~');
// 1st attempt :>
std::cout << "// 1st attempt :>" << std::endl;
std::cout << "12345678901234567890123456789012345678901234567890" << std::endl;
std::cout << std::setw(10) << "Ints" << std::setw(10) << "Floats" << std::setw(10) << "Doubles" << std::endl;
std::cout << std::setw(10) << a << std::setw(10) << b << std::setw(10) << c << std::endl;
std::cout << std::setw(10) << aa << std::setw(10) << bb << std::setw(10) << cc << std::endl;
std::cout << std::setw(10) << aaa << std::setw(10) << bbb << std::setw(10) << ccc << std::endl;
std::cout << std::endl << std::endl << std::endl << std::setfill('*');
// 2nd attempt :>
std::cout << "// 2nd attempt :>" << std::endl;
std::cout << "12345678901234567890123456789012345678901234567890" << std::endl;
std::cout << std::setw(10) << "Ints" << std::setw(10) << "Floats" << std::setw(10) << "Doubles" << std::endl;
std::cout << std::setw(10) << a << std::setw(10) << b << std::setw(10) << c << std::endl;
std::cout << std::setw(10) << aa << std::setw(10) << bb << std::setw(10) << cc << std::endl;
std::cout << std::setw(10) << aaa << std::setw(10) << bbb << std::setw(10) << ccc << std::endl;
std::cout << std::endl << std::endl << std::endl;
// 3rd attempt :>
std::cout << "// 3rd attempt :>" << std::endl;
std::cout << "12345678901234567890123456789012345678901234567890" << std::endl;
std::cout << std::setw(10) << "Ints" << std::setw(10) << "Floats" << std::setw(10) << "Doubles" << std::endl;
std::cout << std::setw(10) << a << std::setw(10) << b << std::setw(10) << c << std::endl;
std::cout << std::setw(10) << aa << std::setw(10) << bb << std::setw(10) << cc << std::endl;
std::cout << std::setw(10) << aaa << std::setw(10) << bbb << std::setw(10) << ccc << std::endl;
std::cout << "12345678901234567890123456789012345678901234567890" << std::endl;
std::cout << std::right << std::setfill(' ');
std::cout << std::setw(10) << "Ints" << std::setw(10) << "Floats" << std::setw(10) << "Doubles" << std::endl;
std::cout << std::setw(10) << a << std::setw(10) << b << std::setw(10) << c << std::endl;
std::cout << std::setw(10) << aa << std::setw(10) << bb << std::setw(10) << cc << std::endl;
std::cout << std::setw(10) << aaa << std::setw(10) << bbb << std::setw(10) << ccc << std::endl;
std::cout << std::endl << std::endl << std::endl;
(You'll notice I also changed "\n" to std::endl which additionally flushes the buffer after each line.)

Manipulators, C++ what order should I use them in?

I am trying to learn about manipulators...is there a specific order for them?
For ex does std::setw come after or before std::setfill and should they be in separate lines?
There's no specific order, just make sure you include the <iomanip> library.
Example on your setw/setfil question:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << setfill('*');
cout << 123;
}
There is not specific order. But please note this, for example, if you want to use std::left and std::right, or write everything in one line then things can get bit tricky.
For example this will not print expected output (prints just: 7
):
std::cout << std::setw(10) << std::left << 7 << std::setfill('x') << std::endl;
Because you need to set attributes first, then print whatever you want. So all three lines below will work, no matter their places change (prints: xxxxxxxxx7):
std::cout << std::setw(10) << std::setfill('x') << std::right << 7 << std::endl;
std::cout << std::right << std::setw(10) << std::setfill('x') << 7 << std::endl;
std::cout << std::setfill('x') << std::right << std::setw(10) << 7 << std::endl;
And the code below is just to clarify things.
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(15) << std::setfill('-') << "PRODUCT" << std::setw(15) << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Brush" << std::setw(15) << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Paste" << std::setw(15) << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "PRODUCT" << std::setw(15) << std::left << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Brush" << std::setw(15) << std::left << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Paste" << std::setw(15) << std::left << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "PRODUCT" << std::setw(15) << std::right << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Brush" << std::setw(15) << std::right << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Paste" << std::setw(15) << std::right << std::setfill('-') << 8 << std::endl << std::endl;
return 0;
}