How Do I Make A Bool Value Change In C++? - boolean

So I've been trying to figure out the bool function for a while now.
I'm trying to make it so after a specific value is entered, the bool will become true or false.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
bool dead = true;
}
Life();
return 0;
}
This is what I currently have, however it isn't changing the value of the bool. Any ideas?

You're redeclaring the variable 'dead' in main's if block. So now you have two 'bool dead', a global one, and a local one in main's if block. The statement 'bool dead = true' sets the local one, but the function 'Life()' uses the global one. Just remove the 'bool' from the latter and you will always be using the global one.
#include <iostream>
using namespace std;
int a;
bool dead;
void Life() {
if (dead == true) {
cout << "You ded.";
}
else {
cout << "You not ded.";
}
}
int main()
{
cin >> a;
if (a == 10)
{
dead = true;
}
Life();
return 0;
}

There are two variables called dead.
- a global
- a local which lifetime is the if block
You set only the local variable inside the if block. So you do not change the global variable. Remove the type inside the if block and your program may run as you intended. And don't forget to initialize your global dead variable. Depending on the OS and state your global variable may start with a true value.

Related

Destructor trouble

I've recently read that if you use an object of a class as a reciving parameter of a function, a copy of an object has to be created automaticly. Therefore, if the destructor is included in the class, both original object and it's copy will be vanished automaticly. However, when I tried to make a small code with the same conception destructor only activated once. What can cause the problem? Thanks in advance!
#include "stdafx.h"
#include <iostream>
using namespace std;
class MyClass {
int val;
public:
MyClass(int i)
{
val = i;
cout << "Constructor is in progress" << endl;
}
void SetVal(int i)
{
val = i;
}
int GetVal()
{
return val;
}
~MyClass()
{
cout << "Destructer is in progress" << endl;
}
};
void Display(MyClass obj)
{
cout << obj.GetVal();
}
int main()
{
MyClass a(10);
cout << "Before display()" << endl;
Display(a);
cout << "After display()" << endl;
system("pause");
return 0;
}
It is called after the return statement. The first message you are seeing is from the copied object. When you get to system("pause") your original object is still in scope, so the destructor is not called. It is called after the return statement is evaluated.
Is destructor called at the end of main(); strange behavior

Ordering in Priority_Queue

I am trying to learn how to use priority_queue vs queue. I have this code for a priority_queue that's supposed to get input from users. The input is a chore and the priority number of the chore. They can enter as many as they want and it's supposed to output them in order. My problem is that it's not outputting them in order. I'm not sure if it's in my main or in my overloaded < operator function. Here is my Chore.h code:
#include<iostream>
#include<fstream>
using namespace std;
//#ifndef CHORE_H;
//#define CHORE_H;
class Chore{
public:
Chore(){priorityNum=0;choreName="";}
std::string getName (Chore c1)const{return choreName;}
int getPriorityNum(Chore c1)const{return priorityNum;}
bool operator <(const Chore c1)const;
std::ostream& output(std::ostream& cout,const Chore c)const;
std::istream& input(std::istream& cin);
private:
std::string choreName;
int priorityNum;
};
//#endif
Here's my Chore.cc code:
#include"Chore.h"
using namespace std;
bool Chore::operator <(const Chore c1)const{
Chore c2;
int c1Num=c1.getPriorityNum(c1);
int c2Num=c2.getPriorityNum(c2);
return c1Num<c2Num;
/*if(c1Num<c2Num)
return true;
else if(c1Num==c2Num)
return true;
else
return false;*/
}
std::ostream& Chore::output(std::ostream& cout,const Chore c)const{
cout<<endl<<"Chore: "<<getName(c)<<endl;
cout<<"chore Priority: "<<getPriorityNum(c);
}
std::istream& Chore::input(std::istream& cin){
cout<<endl<< "Enter chore name:";
cin >>choreName;
cout<<endl<<"Enter priority number:";
cin >>priorityNum;
}
my main is below:
#include<queue>
#include<iostream>
#include"Chore.h"
using namespace std;
int main(){
std::priority_queue<Chore>myChores;
Chore tmp;
bool enterAnother=true;
//input loop
while(enterAnother){
char c;//checks if they want to continue
tmp.input(cin);
myChores.push(tmp);
cout<<endl<<"Want to enter another chore?(y for yes, n for no)";
cin >>c;
if(c=='y'|| c=='Y')
enterAnother=true;
else
enterAnother=false;
}
//output loop
while(!myChores.empty()){
tmp = myChores.top();
myChores.top().output(cout,tmp);
myChores.pop();
}
}
Any help would be greatly appreciated.

How to define the operator= in a class to make a variable in it to be assigned to an outside variable

I have example code and classs:
class a{
int x;
a(){
this->x = 335; /* example number*/
}
public:
void operator=(int);
};
void a::operator=(int source){
this->x = source;
}
main(){
int i = 100;
a example_class;
example_class = i; //works fine!
i = example_class; /*this is what I want to do.*/
}
the problem with this hole thing is that I can't
make the operator= be a friend function
therefore the command: "i = example_class"
can't be done because I can't create a function in //the the int class like I normally would with my own classes.
Finally:
How can I complete the command:
"i = example_class" when the
operator= can't have more than 1
parameter?
notes:
I know the code doesn't do anything.
And is only an example. The point
Is what actually matters.
Also, I need to make it clear that I
cannot create any functions in the
Target class(in this case int). Only in
the source class(in this case a).
I also want to make clear that I know
that it's impossible to declare the
operator= as a friend function.
I know that I could just create a function
to get a reference to int x or make
int x public but I didn't want to do that
because the real code involves complex
functions for converting between types
so it's vary important to me to be able
to write: "i = example_class;".
Thanks,
Ronen.
Working example.
#include <iostream>
class a {
int x = 355;
public:
void operator=(int);
operator int();
};
void a::operator=(int source){
x = source;
}
a::operator int() {
return x;
}
int main(int, char**) {
int i = 100;
a example_class;
example_class = i;
int j = example_class;
std::cout << j << std::endl;
}

Can someone explain what possible datatype this mean?

Can someone explain what possible datatype this refers to?
void (*sa_handler)(int)
from signal's man page.
It is a signal handler function in UNIX.
You declare it and then pass it to sigaction() system call.
Here untested code to catch USR1 signal which you can send to you process with kill command:
void my_function(int signal) {
continue_looping = 0;
}
volatile int continue_looping = 1;
int main(void) {
struct sigaction sa;
sigset_t mask;
sa.sa_handler = &my_function;
sa.sa_flags = SA_RESETHAND;
sigfillset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
while(continue_looping) {
printf("Hello world\n");
}
return 0;
}
You should be careful when calling any further functions from your action handler. I usually just set a flag there - as in the above example.

member function as callback

I would like to pass a member function of an instantiated object to another function. Example code is below. I am open for any strategy that works, including calling functional() from another function inside memberfuncpointertestclass using something like lambda or std::bind. Please note that I did not understand most of the threads I found with google about lambda or std::bind, so please, if possible, keep it simple. Also note that my cluster does not have C++ 11 and I would like to keep functional() as simple as it is. Thank you!
int functional( int (*testmem)(int arg) )
{
int a = 4;
int b = testmem(a);
return b;
}
class memberfuncpointertestclass
{
public:
int parm;
int member( int arg )
{
return(arg + parm);
}
};
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
int (*testf)(int) = &a.member;
std::cout << functional(testf);
}
int main()
{
funcpointertest();
return 0;
}
You cannot invoke a method on an object without an instance to refer to. So, you need to pass in both the instance as well as the method you want to invoke.
Try changing functional to:
template <typename T, typename M>
int functional(T *obj, M method)
{
int a = 4;
int b = (obj->*(method))(a);
return b;
}
And your funcpointertest to:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
std::cout << functional(&a, &memberfuncpointertestclass::member);
}
This is a job for std::function, a polymorphic function wrapper. Pass to functional(...) such a function object:
#include <functional>
typedef std::tr1::function<int(int)> CallbackFunction;
int functional(CallbackFunction testmem)
{
int a = 4;
int b = testmem(a);
return b;
}
then use std::bind to create a function object of the same type that wraps memberfuncpointertestclass::method() of instance a:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
CallbackFunction testf = std::bind(&memberfuncpointertestclass::member, &a, std::placeholders::_1);
std::cout << functional(testf);
}
Check this item for more details.