This question already has answers here:
What's the exact step of macro expanding?
(1 answer)
How do I show the value of a #define at compile-time?
(14 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I'd like to print for a debug reasons the namespace of the variable/function it is defined in. I'm trying to use the define directive:
#include <iostream>
#define NS Static
#define MAKESTRING(x) #x
namespace NS {
int global_x = 8;
void print_global_x() {
//print global_x here:
//std::cout << NS << ": " << global_x << std::endl;
std::cout << MAKESTRING(NS) << "::global_x: " << global_x << std::endl;
}
}
but at the #1 line I'm getting a namespace name is not allowed error while with the second approach (line #2) I'm getting just NS::global_x: 8 instead of Static::global_x: 8
How can I correct the code to get the desired result?
Related
This question already has answers here:
C++ std::get<variable> fails
(4 answers)
Why std::get does not work with variables?
(3 answers)
Cpp Tuple use get with a variable
(1 answer)
Closed 4 months ago.
#include <iostream>
#include <string>
#include <tuple>
int main()
{
auto t = std::make_tuple(1, "Foo", 3.14);
// index-based access
std::cout << "(" << std::get<0>(t) << ", " << std::get<1>(t)
<< ", " << std::get<2>(t) << ")\n";
}
Is it possible to use a variable for the index to access a tuple element instead of a constant?
This question already has answers here:
C++ alignment when printing cout <<
(7 answers)
How can I easily format my data table in C++?
(8 answers)
Align columns output c++
(2 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I am looking for a simple CPP solution to align a string to the right size.
What do I mean by this?
here is an example:
the normal output would be
hello [message]
foo [message]
I would like it to output like this:
hello [message]
foo [message]
note: this is not for aligning a table but rather to know how to align a message by adding certain spaces to the left (or smth).
Like this ?
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(10) << "Hello" << " [message]" << std::endl ;
std::cout << std::setw(10) << "foo" << " [message]" << std::endl ;
}
In the library, you can find std:right and std:setw which can help to justify O/P to the right.
If 'i' is your string, then below is a snippet that you can use :-
#include <iomanip>
cout << right
<< setw(20)
<< fixed
<< i << endl;
This question already has answers here:
What is the # for when formatting using %s
(7 answers)
Closed 2 years ago.
In the macro definition below, what does '#' symbol do? What is this syntax (#x) here?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
# is the stringify operator. It turns the macro argument x into a string literal.
I can see no reason for the extra parens, #define print(x) cout << #x <<" : " << x << endl; would work just as well. Even better would be #define print(x) cout << #x <<" : " << (x) << endl; because the second use of x might require parens to be parsed correctly.
This question already has answers here:
How to output array of doubles to hard drive?
(6 answers)
Closed 6 years ago.
Here is the snippet of code that prints the output
void PrintFunc(double Y[])
{
for(int i=0 ; i<=N+1 ; i++)
{
cout << x_min+i*r << "\t" << Y[i] << endl;
}
}
I want to write it to a text file, how do I do it? i ranges from 1 to 2000.
#include <fstream>
std::ofstream ofs("filename.txt")
// then, in the loop:
ofs << ... << std::endl;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Programmatic way to get variable name in C?
I have checked some of the blogs before posting this here. I have tried the following snippet...
int a=21;
int main()
{
cout<<#a<<a<<endl;
return 0;
}
I am using g++ compiler on ubuntu 10.04. And I am getting the following error:
sample.cpp:17: error: stray ‘#’ in program.
Please suggest me how to print the variables name .
The # stringifying macro thing only works inside macros.
You could do something like this:
#include <iostream>
#define VNAME(x) #x
#define VDUMP(x) std::cout << #x << " " << x << std::endl
int main()
{
int i = 0;
std::cout << VNAME(i) << " " << i << std::endl;
VDUMP(i);
return 0;
}
The # is for if you are writing a macro.
If your cout line were a macro, it would work the way you expect.
If you're not in a macro, you just type "a".