Declaring a boolean variable as NULL in C++ [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can I use null or nullptr as a bool value instead of 0/false?
Since declaring a bool as null means that the bool value is actually zero/false and that does not work in my case. I am using zero/false as another possible outcome. I would like to have a bool value that can be 'non existent' (neither true nor false).

You could take a look at boost::optional or boost::tribool

If you can use boost in your project, then boost::optional is for you. It works like Haskell's Maybe if you're familiar with that.
Simple examples of use (from http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/boost_optional/examples.html):
// return values
optional<char> get_async_input()
{
if ( !queue.empty() )
return optional<char>(queue.top());
else return optional<char>(); // uninitialized
}
void receive_async_message()
{
optional<char> rcv ;
// The safe boolean conversion from 'rcv' is used here.
while ( (rcv = get_async_input()) && !timeout() )
output(*rcv);
}
// local variables
optional<string> name ;
if ( database.open() )
{
name = database.lookup(employer_name) ;
}
else
{
if ( can_ask_user )
name = user.ask(employer_name) ;
}
if ( name )
print(*name);
else print("employer's name not found!");

Related

C++ Condition to check for lower number with a treshold [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
There surely has to be a better way to do this in C++ that this ?
if ( width_value < tipWidth )
{
if (tipWidth < threshold)
{
return threshold;
}
return tipWidth;
}
return width_value ;
return std::max(width_value, std::max(tipWidth, threshold))
Or since C++11:
return std::max({width_value, tipWidth, threshold})
That's assuming you #include <algorithm> and the type of width_value, tipWidth, threshold is the same. If not the same, you have to specify it explicitly, e.g. std::max<int>(...)

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.

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

Checking user input in Link List (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 9 years ago.
Improve this question
I have a link list here to check for user input to see whether user has input the word before or not.
ListNode *cur = head;
while ( cur != NULL )
{
if ( guess == cur->item )
{
return true;
}
cur = cur->next;
}
return false;
My problem is that even though the list is empty, it'll still enter the while loop. What's my mistake?
Do you initialize the empty list with head=NULL;? Otherwise head will most likely point to some random memory, and it will be impossible to detect that the list is empty.