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.
Related
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
I currently have a function that takes in a vector of structs, including all floats, and should return some values after a simple calculation.
My cout function is simply
void taxPrint(std::vector<TaxPayer> &citizen)
{
int loops = 0;
std::cout << "\nTaxes due for this year: \n" << std::endl;
do
{
std::cout << "Tax Payer #" << loops << " : $" << citizen[loops].taxes << std::endl;
loops++;
}
while (loops + 1 <= SIZE);
and the resulting output in console is
Tax Payer #0 : $450000
Tax Payer #1 : $210000
That said, I want it to be
Tax Payer #0 : $4500.00
Tax Payer #1 : $2100.00
I've been messing around with setw() and setprecision() but I don't exactly understand how they work.
std::setw, actually has nothing to do with value precision, it is for padding string with prefixes like: 001-0123-9124 (Padded with 0)
Example: std::cout << std::setfill('0') << std::setw(5) << 5 << std::endl; will print 00005
Here is how to use it using std::fixed and std::setprecision:
void taxPrint(std::vector<TaxPayer> &citizen)
{
int loops = 0;
std::cout << "\nTaxes due for this year: \n" << std::endl;
do
{
std::cout << "Tax Payer #" << loops << " : $" << std::setprecision(2)
<< std::fixed << citizen[loops].taxes / 100. << std::endl;
loops++;
}
while (loops + 1 <= SIZE);
} // Don't miss this bracket!
Also, look at this question to know more about specifying a fixed precision to a value...
Take this a minimal working example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << "aaaaaaa"
<< setw(10) << "bbbb"
<< setw(10) << "ccc"
<< setw(10) << "ddd"
<< setw(10) << endl;
for(int i(0); i < 5; ++i){
char ch = ' ';
if ( i == 0 )
ch = '%';
cout << setw(10) << i
<< setw(10) << i << ch
<< setw(10) << i
<< setw(10) << i
<< setw(10) << endl;
}
return 0;
}
The output is
aaaaaaa bbbb ccc ddd
0 0% 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
What I would like to do is to include << i << ch in one field of setw(10) so that columns are aligned properly.
Since we are looking at either ' ' or '%' you can simply calculate statically.
cout << setw(10) << i
<< setw( 9) << i << ch
<< setw(10) << i
<< setw(10) << i
<< setw(10) << endl;
probably combine i and ch in one string, setw wouldn't accept this behavior natively
try this snippet
cout << setw(10) << i
<< setw(10) << std::to_string(i) + ch;
You need to concatenate them into one string, like this:
#include <string>
cout << setw(10) << std::to_string(i) + ch;
in general.
But if you know that i is one character you could use:
cout << setw(9) << i << ch;
which might the case for you, since i seems to be ' ' or '%'.
I'm not sure to understand your need.
You could use some std::ostringstream like e.g.
std::ostringstream os;
os << i << ch << std::flush;
std::cout << setw(10) << os.str();
You could build a string, like James Maa answered
For a lab of mine we need to display the output values in a column following a statement of what the value is for.
Example of what I need.
Amount of adult tickets: 16
Amount of children tickets: 12
Revenue from ticket sales: $140.22
I am trying to use setw like
cout << "Amount of adult tickets: " << setw(15) << ticketsAdult` << endl;
cout << "Amount of children tickets: " << setw(15) < ticketsChildren << endl;
I'm assuming either setw is the wrong thing to use for this or I'm using it wrong as it usually results in something like
Amount of adult tickets: 16
Amount of children tickets: 12
What can I use to make the values to the right all align like they did in the example no matter the length of the "Amount of..." statements before each of them?
It looks like there is right and left alignment too. Other than that it looks like it's a pain to use.
C++ iomanip Alignment
Combining everything said together
#include <iostream>
#include <iomanip>
int main()
{
int ticketsAdult = 16;
int ticketsChildren = 12;
double rev =140.22;
std::cout << std::setw(35) << std::left << "Amount of adult tickets: " << ticketsAdult << '\n';
std::cout << std::setw(35) << std::left << "Amount of children tickets: " << ticketsChildren << '\n';
std::cout << std::setw(35) << std::left << "Revenue from ticket sales: " << '$' << rev << '\n';
return 0;
}
I'm writing a program for a number theory proof but I have very little experience in writing code.
What i want to do is display all the results of the formula:
Answer = sqrt [4*n]
where;
n = 1,2,3,4,5,6,7,...50
But I want to display the results in 2 groups of columns, i.e.,
I want the 1st column to be the 1st half of n. So as there are 50 iterations, the 1st column will contain the numbers 0 --> 25.
The 2nd column should display the first 25 Answers from the equation.
The 3rd column has the second half of n. i.e., The numbers 26-->50
The 4th column should display the results from the last 25 values of n.
An example of what I'm trying to display is below*:
*(ignore the dots in the below example, they are only there for display purposes only)
n:............Answer:............n:...........Answer:
So far i have the code working but I just can't figure out how to split and display the n-values and Answer-values as I have shown above.
All I can manage to do is display the values in only 2 columns as follows:
n:...........Answer:
This is what I've got so far:
#include <iostream>
#include <iomanip>
#include <cmath> //preprocesser directives
using namespace std; //avoids having to use std:: with cout/cin
int main (int argc, char* argv[])
{
int n;
float Ans;
cout << setw(4) << "n:" << "\t\t" << setw(4) << "Answer:" << "\n" << endl;
for (int n = 0; n<=50; n++)
{
Ans = sqrt ((4)*(n));
cout << setw(4) << n << "\t\t" << setprecision(4) << setw(4) << Ans << endl;
}
cout << "\n\nPress enter to end" << endl;
cin.get();
}
I really have no idea how to split it into 4 separate columns, but I know the \t function must have something to do with it!??
Any help is appreciated!
Thanks
Change your for loop to:
for (int n = 1; n<=25; n++)
{
Ans = sqrt ((4)*(n));
cout << setw(4) << n << "\t\t" << setprecision(4) << setw(4) << Ans << "\t\t";
Ans = sqrt ((4)*(n + 25));
cout << setw(4) << n + 25 << "\t\t" << setprecision(4) << setw(4) << Ans << endl;
}
Loop from 1 to 25 and in the loop body, calculate two results: the one for n and the one for n + 25.
Here is a very simple compilable and runnable example showing you the basic idea:
#include <iostream>
#include <math.h>
int main()
{
for (int n = 1; n <= 25; ++n)
{
std::cout << n << "\t" << sqrt(n) << "\t"
<< (n + 25) << "\t" << sqrt(n + 25) << "\n";
}
}
This may already be sufficient for your needs. However, notice that I've used tabulators. That's easily written but makes correct output depend on tab size, i.e. you may get incorrectly aligned columns on some displays or editors.
One solution for this problem would be to create HTML output, use the <table> element and let a web browser take care of displaying an actual table:
#include <iostream>
#include <math.h>
int main()
{
std::cout << "<table>\n"; // and other HTML stuff
for (int n = 1; n <= 25; ++n)
{
std::cout << "<tr>";
std::cout << "<td>" << n << "</td><td>" << sqrt(n) << "</td><td>"
<< (n + 25) << "</td><td>" << sqrt(n + 25);
std::cout << "</tr>\n";
}
std::cout << "</table>\n";
}
If you are on Windows, then compile this into, say, output.exe, and call it as follows to create an HTML file:
output.exe >output.html
If you want pure text output, things get harder. You'd have to calculate all results in advance, store them in a std::vector, convert each of them to std::strings, look at the strings' sizes, pick the longest, then output all items with appropriate padding spaces. In fact, you'd have to learn a lot of new things for that, which would probably result in one or two additional, more specific questions on Stack Overflow.
Edit: You are using setw(4). Actually, that's another reasonable solution to the problem if you are happy with a fixed width.
This solution solves your problem and has the advantage of solving similar problems where you have to output (x,y) pairs to a four column table. You aren't locked into inputs increasing 1,2,3 etc.
Add this to top of file, with other includes
#include <vector>
Add this to body
cout << setw(4) << "n:" << "\t\t" << setw(4) << "Answer:" << "\n" << endl;
std::vector<double> inputs;
std::vector<double> answers;
for (int n = 0; n<=50; n++){
inputs.push_back(n);
inputs.push_back(sqrt(4*n));
}
for (int n = 1; n<=25; n++){
cout << setw(4) << Inputs[n] << "\t\t" << setprecision(4) << setw(4) << Answers[n] << "\t\t" << setw(4) << Inputs[n+25] << "\t\t" << setprecision(4) << setw(4) << Answers[n+25] << endl;
}