C++ get variable from variable [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 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.

Related

How to decide is it a string or not? [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 10 months ago.
Improve this question
I have an array of strings and I'd like to assort the elements by real datatype ("1" -> int; "abc" -> string; "1a" -> string) :
#include <iostream>
#include<vector>
int main() {
std::vector<std::string> number;
std::vector<std::string> str;
std::string arr[5] = {"1","1a","ab","10.1","a2"};
for(int i = 0;i<5;i++){
if(arr[i] /* is string */){
str.push_back(arr[i]);
}
else if(arr[i] /* is int */){
number.push_back(arr[i]);
}
}
return 0;
}
What is the simplest way?
Thanks!
If your goal is to only examine whether a string contains integer/decimal or not, you can try a solution with regexes:
if(std::regex_match(arr[i], std::regex("[-|+]?[0-9]*[.,]?[0-9]+")))
number.push_back(arr[i]);
else
str.push_back(arr[i]);
I'm assuming you're not taking overflow into account because you're only using strings, otherwise if you wanted to convert strings to numbers, you'd have to take that into account and use other solutions.

incompatible types in assignment null [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I Keep getting this error when I try to set the first char in firstname and lastname to a null and return it to a 1.
my code is...
int DeregisterStudent(int SID, struct studentdata SRecord[])
{
int index;
index = SRecordSearch(SID, MAXRECS, SRecord);
if(index >= 0)
{
SRecord[index].sid = 0;
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
return 1;
}
return 0;
}
and the error I get is error:incompatible type in assignment
for these two lines
SRecord[index].lastname = '\0';
SRecord[index].firstname = '\0';
You are not writing to the string correctly, it should be
SRecord[index].lastname[0] = '\0';
or
SRecord[index].lastname = "";
depending on how the struct was declared. In the second case you might be overwriting a dynamically allocated string pointer, in which case it should be free()ed.

how to retrieve value from map c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have map<string, inode_ptr> directories
I want to retrieve the value associated with a certain key.
so I did this: inode_ptr i = directories.find("string");
But it is returning the string key, what should I do?
Use the following approach
inode_ptr i = NULL;
auto it = directories.find("string");
if ( it != directories.end() ) i = it->second;
Maybe it is even better to write
inode_ptr i = {};
instead of
inode_ptr i = NULL;
Give this a go:
inode_ptr myInode = NULL;
map<string, inode_ptr>::iterator i = directories.find("string");
if ( i != directories.end() )
{
myInode = i->second;
}
else
{
cerr << "no 'String' in the map, I should be "
<< "sure to check for null before using myInode" << endl;
}
Here is a link to that fine manual referenced in the above comments:
http://www.cplusplus.com/reference/map/map/find/

if statement doesn't work with function [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
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}
void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill

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.