C++ how to print union name integer [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to print integer in C++:
int n5x = 1;
int n5y = 2;
[...]
int value = 5;
cerr << n+value+x << n+value+y << endl;
Is this possible in C++?
Thanks

As already mentioned, you cannot somehow "build" some string at run time and use it as variable name. This is a compile time mechanism, and even if this was a compile time problem, it would be a bad idea.
You most likely want
std::vector<int> nx(someLength);
std::vector<int> ny(someLenght);
int value = 5;
cerr << nx[value] << ny[value] << endl;
instead. (Or the same thing with std::array<int, someLength> nx{} if someLength is known at compile time and not big.)

cerr << n+value+x << n+value+y << endl;
Uhhm what please? You mean to expand symbol names and bind to them at runtime?
No that's not possible.
The closest thing you can get is setting up a std::map<std::string,int> and generate the key string values as needed.

Related

How to add decimal to floating point number with non-decimal value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have following program and i want the output to be 10.0
what line of code I have to add in function fun so that i have desired output.
#include<bits/stdc++.h>
using namespace std;
float fun(float a)
{
return a;
}
int main()
{
float a = 10;
cout << fun(a);
return 0;
}
I tried using setprecision() but it is often used with cout. How it can be used when returning the output? I am stuck here.
Nothing to stop this
float fun(float a)
{
cout << fixed << setprecision(1);
return a;
}
But that's stupid code for a stupid puzzle. What does this have to do with real programming?

What is the quickest way to print a 2D array of chars? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to write a simple console game.
I want to refresh console 30 times per second. Usually this is not a problem, but this time I'm working with an array of size 30x30, and printing it using two loops is simply not fast enough.
I noticed that
<code>printf( "%s\n", myarray );</code>
is quick enough, but it doesn't work properly with 2d arrays.
Is there a function that will make my array appear "instantly" on screen?
I'm using this function to print my array:
void draw(char screen[32][31]){
for (int x=0;x<32;x++){
for (int y=0;y<31;y++){
cout<<screen[x][y];
}
cout<<endl;
}
}
This should be faster:
void draw(char screen[32][31]){
for (int x = 0; x < 32; x++){
cout.write(screen[x], 31);
cout << '\n';
}
cout << flush;
}
As noted in a comment above, endl is the wrong way to insert a newline, because it also flushes the stream and so removes the benefits of buffering done by the I/O library. See endl vs '\n' for more information.

How to display a list in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to print a list in C++.
My code is as follows:
void MovieFunctions::printMovieList(list<Movie> movies)
{
for (Movie m: movies)
{
cout << m << endl;
}
}
I am getting this error for the cout << m << endl line:
cannot convert 'm' (type 'Movie') to type 'const unsigned char*'
What's going wrong?
You need to implement the operator<<() (here) for this purpose.

C++ get variable from variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there another solution, please?
if(!http_piconpath && http_tpl)
{ http_piconpath = http_tpl; }
If not exist http_piconpath but exist http_tpl then assign the value from http_tpl to http_piconpath.
You provide very little information about what you are doing. Assuming that you use strings from your comment, the if statement you've got is not valid for strings, you'll see your compiler screaming that it can't convert string to bool. Below is a very basic example. Note that you must initialise http_piconpath, else it will have a garbage value and you won't know if its value is set or not.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string http_piconpath = "";
string http_tpl = "string";
if(http_piconpath == "" && http_tpl != "") {
http_piconpath = http_tpl;
}
cout << http_piconpath << endl;
return 0;
}
Supposing that both are pointers (of compatible types),
if(!http_piconpath) http_piconpath = http_tpl;
Or
http_piconpath = http_piconpath ? http_piconpath : http_tpl;
If picon is null, it gets the value of tpl; if both are null, nothing changes.

pointer to std::map fails to insert [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am very confused. Why does this work:
double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> th;
th.insert(std::make_pair(variable, doubleValue));
and this fails:
double doubleValue = 20;
NcVar variable = {some process obtaining an instance}
map<NcVar,double> *th = new map<NcVar,double>();
th->insert(std::make_pair(variable, doubleValue));
That means, the first variant ends up with one key/value-pair, while the second leaves the map unchanged (0 entries)?
Works for me:
#include <map>
#include <iostream>
using namespace std;
int main(){
typedef map<int,float> mapp;
mapp map1;
map1.insert(make_pair(1,1.1));
mapp * mp2 = new mapp();
mp2->insert(make_pair(2,2.2));
cout << map1.begin()->second << endl;
cout << mp2->begin()->second <<endl;
return 0;
}
And output:
$g++ map_test.cpp
$ ./a.out
1.1
2.2
Thanks for the help, guys. I feel kinda stupid now. The assumption that the map was empty was based on the appearance in the debugger. I am using XCode as IDE and when using a pointer to map, it would simply mess up and display the map as empty. Using cout revealed the truth.