This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 1 year ago.
code :
#include <iostream>
using namespace std;
int main()
{
int i = 1;
cout << "hello " << i << " end" ;
return 0;
}
OUTPUT : hello 1 end
this works fine but
#include <iostream>
using namespace std;
int main()
{
int i = 1;
cout << "hello " , i , " end" ;
return 0;
}
OUTPUT: hello
why the above terminates with printing hello only?
std::cout << "hello " , i , " end" ;
Because , has a lower operator precedence than <<, this will be evaluate to:
(std::cout << "hello "), i , " end" ;
First, (std::cout << "hello ") will be evaluate, printing hello .
Second, i would get evaluate. The value i will be return for the whole expression (std::cout << "hello "), i.
Third, " end" would get evaluate. The value " end" will be return for the whole expression ((std::cout << "hello "), i ), " end" ;
Related
How can I replace printf() with cout?
My Code In C++:
#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
char st[15]="United Kingdom";
printf("%5s\n",st);
printf("%15.6s\n",st);
printf("%-15.6s\n",st);
printf("%.3s\n",st); // prints "Uni"
return 0;
}
The Code Prints:
United Kingdom
United
United
Uni
How can I manipulate like this in C++?
The std::setw() I/O manipulator is the direct equivalent of printf()'s minimum width for strings, and the std::left and std::right I/O manipulators are the direct equivalent for justification within the output width. But there is no direct equivilent of printf()'s precision (max length) for strings, you have to truncate the string data manually.
Try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char st[15] = "United Kingdom";
cout << setw(15) << st << '\n'; // prints " United Kingdom"
cout << setw(5) << st << '\n'; // prints "United Kingdom"
cout << setw(15) << string(st, 6) << '\n'; // prints " United"
cout << left << setw(15) << string(st, 6) << '\n'; // prints "United "
cout << setw(15) << string(st, 0) << '\n'; // prints " "
cout << string(st, 3) << '\n'; // prints "Uni"
cout << st << '\n; // prints "United Kingdom"
return 0;
}
Live demo
The std::setw() iomanip will set the field width for insertions, that is, even if the isinsertions shorter than the specified lenth it will get padded to that amount, std::setfill() sets the padding character.
For example:
std::cout << std::setw(20) << std::setfil('0') << "hey you!" << std::endl;
would print "Hey you!000000000000\n" (possibly \r\n instead of just \n if you're using Windows). There is also std::left which would cause the padding to precede rather than follow the insert.
You are looking for Boost.Format.
#include <iostream>
#include <string>
#include <boost/format.hpp>
int main()
{
std::string st = "United Kingdom";
std::cout << boost::format("%15s\n") % st;
std::cout << boost::format("%5s\n") % st;
std::cout << boost::format("%15.6s\n") % st;
std::cout << boost::format("%-15.6s\n") % st;
std::cout << boost::format("%15.0s\n") % st;
std::cout << boost::format("%.3s\n") % st; // prints "Uni"
std::cout << boost::format("%s\n") % st;
}
Live example
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a simple program but it's running weirdly. Basically the code runs fine but when the numbering at the beginning of the line comes into play, int x++ displays the same number as the first line then continues. Why does this happen?
Code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include "logo.h"
int main()
{
SetConsoleTitle("plains.exe");
displayLogo();
int number;
int addTotal = 0;
int numbersEntered = 0;
std::cout << " [1] enter your first number: ";
std::cin >> number;
while (number != -1) {
addTotal = addTotal + number;
numbersEntered++;
std::cout << " [" << numbersEntered << "]" << " enter your next number or type '-1' to add them: ";
std::cin >> number;
}
if (number == -1) {
std::cout << " " << std::endl;
std::cout << " --------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " the sum of your numbers is " << addTotal << "." << std::endl;
std::cout << " you entered a total of " << numbersEntered << " numbers." << std::endl;
std::cout << " " << std::endl;
std::cout << " the average of your numbers is " << addTotal / numbersEntered << "." << std::endl;
std::cout << " " << std::endl;
}
return 0;
}
You initialized numbersEntered to 0. The first time through the while loop, it does numbersEntered++, which sets it to 1. So the first prompt in the loop contains [1]. This is the same as what you printed before the loop with:
std::cout << " [1] enter your first number: ";
so you see [1] twice.
To prevent this duplication, add 1 to the variable when displaying the prompt:
std::cout << " [" << (numbersEntered++) << "]" << " enter your next number or type '-1' to add them: ";
This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432
How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.
You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}
I need to tokenize (' ','\n','\t' as delimiter) a text with somethink like
std::string text = "foo bar";
boost::iterator_range<std::string::iterator> r = some_func_i_dont_know(text);
Later I want to get output with:
for (auto i: result)
std::cout << "distance: " << std::distance(text.begin(), i.begin())
<< "\nvalue: " << i << '\n';
What produces with example above:
distance: 0
value: foo
distance: 6
value: bar
Thanks for any help.
I would not use the ancient Tokenizer here. Just use String Algorithm's split offering:
Live On Coliru
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace boost;
int main()
{
std::string text = "foo bar";
boost::iterator_range<std::string::iterator> r(text.begin(), text.end());
std::vector<iterator_range<std::string::const_iterator> > result;
algorithm::split(result, r, is_any_of(" \n\t"), algorithm::token_compress_on);
for (auto i : result)
std::cout << "distance: " << distance(text.cbegin(), i.begin()) << ", "
<< "length: " << i.size() << ", "
<< "value: '" << i << "'\n";
}
Prints
distance: 0, length: 3, value: 'foo'
distance: 6, length: 3, value: 'bar'
This question already has answers here:
ostream chaining, output order
(5 answers)
Closed 8 years ago.
below is my practice code about c++11 lambda:
#include<iostream>
int d = 0;
int main()
{
int e = 1;
auto i = [&]() ->int {
e += 1;
d += 1;
return d;};
d += 1;
std::cout << "the value of d:" << d << std::endl;
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
return 0;
}
and i got result not as expected:
the value of d:1
the value of i():2
e:1 d:1
e:2 d:2
I just don't understand why
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
this two lines give different out put of e and d?
ps:forgive my bad english
The evaluation order of the operands of << isn't specified; so it's unspecified whether the values of e and d in the first line are taken before or after they're incremented by the call to i().
So you might get the values before or after they're incremented, depending on the whim of the compiler.
The second line is sequenced after the first, so you'll definitely get the incremented values there.
The order of evaluation of function arguments is unspecified. So function arguments can be evaluated right to left or left to right.
It seems that your compiler evaluates arguments from right to left. So in this statement
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << << std::endl;
the compiler at first evaluates d that is equal to 1 then e that is equal also to 1 and only after that it evaluates i() that returns 2. So you get the following output
the value of i():2
e:1 d:1
Take into account that in this statement using of operator << is equivalent to a call of an appropriate overloaded operator function with the corresponding arguments.
For example this code snippet
int a = 10, b = 20;
std::cout << a << ' ' << b;
is equivalent to
int a = 10, b = 20;
std::operator <<( std::cout.operator <<( a ), ' ' ).operator <<( b );
To get the expected result you should split the original statement into
std::cout << "the value of i():" << i() << std::endl
std::cout << " e:" << e << " d:" << d << << std::endl;