controlling the number of digits being displayed c++ - c++

I want to display the values (in hex) of these certain registers and counters but I want to limit the number of digits being displayed.
cout << "Acc register : " << hex << Acc << ","; //display 2 digits
cout << " X register : " << hex << X << ","; //display 3 digits
cout << " Program counter : " << hex << PC << ","; //display 3 digits
I also want to display preceding zeros if the value was only 1 digit long, for example if
program counter = 4
PC should display as Program counter : 004
I have searched the internet to try and find a solution but I can not seem to find something that works. Can anybody explain how to do this please. Many thanks.

The STL comes with a header called iomanip, which can modify in the way you want and even more.
A short example would be:
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setfill ('x');
std::cout << "PC" << std::setw(3) << 4 << std::endl;
}
An additional example can be found here:
http://www.cplusplus.com/reference/iomanip/setfill/
An overview over all options here:
http://www.cplusplus.com/reference/iomanip/

Related

How can you use std::setw() with array output?

I am making a blackjack game in C++, and I am trying to print out the players and dealers cards in addition to their sums, capital and so on. However I'm running into an issue with std::setw() when printing out the vector of cards. Here is a code snippet:
int width = 18;
std::cout << std::left << std::setw(width) << "Your cards:";
std::cout << std::left << std::setw(width * 2) << "arr";
std::cout << std::left << std::setw(width) << "Dealers cards:";
std::cout << "arr2" << std::endl;
std::cout << std::left << std::setw(width) << "Your sum:";
std::cout << std::left << std::setw(width*2) << player.sum();
std::cout << std::left << std::setw(width) << "Dealers sum:";
std::cout << dealer.sum() << std::endl;
Where arr and arr2 is there should be number values like 5 2 6 1, but if I print each element separately the with alignment will break. I think for setw() to work it needs to be one block or string, or else the vertical alignment will mess up once the values change. I tried myString.push_back() for each vector value and then printing that, with no luck. I assume I need to find a way to print the string into one element.
This is what it should look like:
Your cards: 5 7 1 2 Dealers cards: 2 1 7 5
Your sum: 21 Dealers sum: 21
Your capital: 100 Dealers capital: 100
I have found a solution. You can use stringstream to add int values to a string with no errors in conversion, this is how I fixed my code:
#include <sstream>
std::stringstream playerCards{};
for (int i{}; i < player.cards.size(); i++) {
playerCards << player.cards[i] << " ";
}
int width = 18;
std::cout << std::left << std::setw(width) << "Your cards:";
std::cout << std::left << std::setw(width * 2) << playerCards.str();
This way the array will get put into a string and will count as one block, which is what I was looking for.

Trouble with alignment using iomanip

I'm not great at using the iomanip library and have tried looking around for similar problems, but haven't been able to implement a good solution. I'm trying to display words that are used most frequently and least frequently. My code is (abridged version showing what I'm having problems with):
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string word1 = "helloooo";
string word2 = "hey";
cout << "MOST FREQUENT: ";
cout << setw(7) << left << word1;
cout << setw(5) << right << "(" << 5 << ")" << endl;
cout << "LEAST FREQUENT: ";
cout << setw(9) << left << word2;
cout << setw(5) << right << "(" << 3 << ")" << endl;
}
This is only a simpler version of the code I'm working on. In reality, the words will be pulled from a vector and the numbers will be different. The words should be center aligned and the right side should show the number in a column. I want it to look like:
MOST FREQUENT: hellooooo (5)
LEAST FREQUENT: hey (3)
But instead I get:
MOST FREQUENT: helloooo (5)
LEAST FREQUENT: hey (3)
I'm just not good using iomanip and any help would be appreciated!

inconsistent behavior of <iomanip>

I have the following code
cout << setfill('0') << setw(4) << hex << 100 << 100 << std::endl;
The output is:
006464
If I want to let every number with width 4, I have to use
out << setfill('0') << setw(4) << hex << 100 << sew(4) << 100 << std::endl;
But if I want to print every number with hex and setfill('0'), I only need to set setfill('0') and std::hex once.
Does c++ design this on purpose? what is its intention?
Yes it is on purpose. The stream operations are internally peppered with resets of the field width, specified by the standard. I think there's no good answer as to why.

C++ how to show exact digits of fraction part

Is there any way in C++ (or boost lib) to show a given number digits of fraction part? But I don't want to print the trailing 0 in fraction part (eg. 1.000, 1.500). See this case:
cout << std::setprecision(3) << 5.0/7.0 << endl; // 0.714
cout << std::setprecision(3) << 12.0/7.0 << endl; // 1.71
cout << std::setprecision(3) << 7.0/7.0 << endl; // 1
cout << std::setprecision(3) << 10.5/7.0 << endl; // 1.5
The problem is setprecision prints line 1 and line 2 differently, where I want to have both lines print 0.714 and 1.714. And still keep line 3 and line 4 1 and 1.5.
How about something like:
#include <cmath>
using namespace std;
cout << setprecision(ceil(log10(floor(x))+3) << x;
Not exactly fast, but the idea is to figure out how many digits the integer part of x requires, then add the number of decimal places you're interested in to that. You could even write your own manipulator to do that if you were really serious about it.

Align cout format as table's columns

I'm pretty sure this is a simple question in regards to formatting but here's what I want to accomplish:
I want to output data onto the screen using cout. I want to output this in the form of a table format. What I mean by this is the columns and rows should be properly aligned. Example:
Test 1
Test2 2
Iamlongverylongblah 2
Etc 1
I am only concerned with the individual line so my line to output now (not working) is
cout << var1 << "\t\t" << var2 << endl;
Which gives me something like:
Test 1
Test2 2
Iamlongverylongblah 2
Etc 1
setw.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << setw(21) << left << "Test" << 1 << endl;
cout << setw(21) << left << "Test2" << 2 << endl;
cout << setw(21) << left << "Iamlongverylongblah" << 2 << endl;
cout << setw(21) << left << "Etc" << 1 << endl;
return 0;
}
I advise using Boost Format. Use something like this:
cout << format("%|1$30| %2%") % var1 % var2;
You must find the length of the longest string in the first column. Then you need to output each string in the first column in a field with the length being that of that longest string. This necessarily means you can't write anything until you've read each and every string.
you can do it with
string str = "somthing";
printf ("%10s",str);
printf ("%10s\n",str);
printf ("%10s",str);
printf ("%10s\n",str);