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

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.)

Related

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);

I want to align my results in columns based on the decimal place

I want to display my result aligned on the column by the decimal place.
I have tried to just put setw(7) and setw(6) in parts of the display but it seems to not change the output at all.
int main()
{
double x;
char more = 'y';
while(more=='y' || more=='Y')
{
cout << "\n\t\t\tInput x:";
cin >> x;
cout << "\n\n\t\t\t LibraryResult\tMyResult" << endl;
cout << setprecision(2) << fixed << "\n\t\tsin(" << x << ")\t"
<< setprecision(6) << sin(x) << "\t" << mySin(x) << endl;
cout << setprecision(2) << fixed << "\n\t\tcos(" << x << ")\t"
<< setprecision(6) << cos(x) << "\t" << myCos(x) << endl;
cout << setprecision(2) << fixed << "\n\t\texp(" << x << ")\t"
<< setprecision(6) << exp(x) << "\t" << myExp(x) << endl;
}
}
I want the resultants of the program to be aligned by decimal, so when you put in a number like 2 the decimals are all in the same column.
If you set a precision of 6, a width of 6 or 7 is simply not enough to contain the number. You have to either reduce the precision or increase the width.
Try playing around with the stream manipulators in the following snippet
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
int main()
{
const double period = 2 * 3.141592653589793;
const int steps = 20;
std::cout << std::string(44, '=') << '\n';
std::cout << std::setw(3) << "x" << std::setw(12) << "sin(x)"
<< std::setw(12) << "cos(x)" << std::setw(14) << "tan(x)" << '\n';
std::cout << std::string(44, '-') << '\n';
for(int i = 0; i <= steps; ++i)
{
double x = i * period / steps;
std::cout << std::setprecision(2) << std::fixed << x
<< std::setprecision(6) << std::setw(12) << std::sin(x)
<< std::setprecision(6) << std::setw(12) << std::cos(x)
<< std::setprecision(6)
<< std::setw(16) // <- Try to decrease it
<< std::scientific // <- Try to keep it as std::fixed
<< std::tan(x) << '\n';
}
std::cout << std::string(44, '-') << '\n';
}
You must use several manipulators (one is not enough)
You might want to try with the following combination, that right-aligns your numbers, with a fixed number of decimals.
std::cout.width(15);
std::cout << std::fixed << std::setprecision(6) << std::right << exp(x) << std::endl;
You can find information about manipulators here

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;
}

Scenario output gives wrong answer upon second loop

I'm pretty much done with this program except for this small problem.
The problem is when the first time I loop through the program with the following in put of 20.0 36.0 72.0 2.0 10.0 18.0 10.0 my output comes out to be 232.59 sec which is correct.
For the second time I loop around, I input 25.0 72.0 200.0 1.5 8.0 30.0 20.0 my output comes out to be 800.41 which is wrong when it should be 1141.63
Now if I reverse the inputs and take what I entered for the second loop for the first loop and first for the second loop. I do get my output of 1141.63 for the first loop, but the second loop, my answer is 1141.63 . I am very puzzled as to how this happening. One of my early solutions was to reset all variables back to 0 after each loop using another function, but that didn't seem to work and now I'm here.
EDIT: Problem figured out. Had to reset time and volFilled to 0 at the end of each loop.
void Well::timeReq()
{
bool exit = false;
while (!exit)
{
std::cout << "Enter 7 digits to evaluate or type 0 now to exit." << std::endl;
std::cin >> UP;
if (UP == 0)
{
exit = true;
break;
}
std::cin >> D >> L >> B >> P >> DOWN >> V;
double WL = L;
double radius = (D / 2);
while (volFilled < V)
{
time += UP;
time += sqrt(2 * (L) / (acceleration * 12.0));
time += (L) / (P);
L += (B * cuIns) / (pi*pow(radius, 2));
time += DOWN;
volFilled += B;
}
std::cout << std::endl;
std::cout << "Scenario " << scenario << ":" << std::endl;
std::cout << std::left << std::setw(25) << "\tUp Hill" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< UP << " sec" << std::endl;
std::cout << std::left << std::setw(25) << "\tWell Diamter" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< D << " in" << std::endl;
std::cout << std::left << std::setw(25) << "\tWater Level" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< WL << " in" << std::endl;
std::cout << std::left << std::setw(25) << "\tBucket Volume" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< B << " cu ft" << std::endl;
std::cout << std::left << std::setw(25) << "\tBucket Ascent Rate" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< P << " in/sec" << std::endl;
std::cout << std::left << std::setw(25) << "\tDownhill" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< DOWN << " sec" << std::endl;
std::cout << std::left << std::setw(25) << "\tRequired Volume" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< V << " cu ft" << std::endl;
std::cout << std::left << std::setw(25) << "\tTIME REQUIRED" << std::left << std::setw(30) << std::fixed << std::setprecision(2)
<< time << " sec" << std::endl;
scenario++;
}
}
While this may not completely fix your issue, you need to at least reinitialize your volFilled value back to 0.0 between scenarios. Otherwise you're just continuing from the previous scenario's volFilled, and that explains why the second test that you did with switching the scenarios yielded the same output. After the first scenario where volFilled reaches 20.0, it won't execute the logic inside the considion while (volFilled < V) in the second scenario because at this point, volFilled = 20 while V = 10.

Formatting Cout Output in 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;