Question:
In C++, what is the point of flags/bools?
Anya
2011-06-05 14:45:09 UTC
I'm doing a C++ programming assignment and one of the requirements is to use flags. However, I've written the entire program which is about 600 lines long, and it executes exactly the way it should. But I never used flags. Can someone point me in the right direction by telling me when to use flags and when to use only if/else statements without flags? Thanks.

PS will using flags/bools make my program shorter? The professor says the sample program is at 500 lines with comments, but mine is 600 lines WITHOUT comments. eek!

Thanks.
Six answers:
AnalProgrammer
2011-06-05 15:18:22 UTC
A flag is an on/off switch. It is a place where 1/0, y/n, yes/no or x/y can be stored.

It is boolean because this is a storage location that only holds 1 bit and can hold the 1/0 value.

But it can also be a single char or an int or a string.



Note: whilst a boolean will only take up a bit of storage the compiler will probably align all storage outside of a structure to a word boundary. So a boolean will actually take up at least a byte of storage. This is very much dependant on the compiler though.



If you want something in a program to be done only on the first execution of a loop then a flag is ideal. If you want something done first one way then another and then the first way etc. then a flag is ideal.



It is all about program design.

The key thing to remember about program design is that there is no right way to write or code a program. As long as the program does what the user requests and what the user needs then the program is correct. It is important to note that what the user wants may not be what they need. However what the user needs is always what the program should do.

You can only find out what the user needs by talking to the user. Years of experience can also point you towards how to approach what the user needs.



Go back to your program and draw a flowchart.

This should point you at where you may have gone wrong.



Have fun.
Damien Bell
2011-06-05 15:22:31 UTC
Well, flags and bools are pretty amazing overall, let me show you why.



Let's say you want a program to run unless one certain thing happens, under normal circumstances, that would be pretty tricky... example:



while (statement x!= variable or constant y){

if statement (x == something else){

//do stuff

}

//do stuff

//do stuff

//do stuff

}



Whereas the alternative allows you a little more control



bool isDone = false;

while (isDone==false){

//do stuff

//do stuff

//do stuff

if (certainCondition == true){

isDone=true;

}

}

As you can see it makes ending a loop a LOT easier and more precise.



Bools can only have 2 states, 'true' and 'false' and they must be set as such. If I recall correctly bools start off as true unless you explicitly state otherwise (Don't quote me on that, since I always initialize them anyways). In this case, a bool is known as a 'flag' since a flag only has 2 states "up" or "Down" (hence the nickname).



Just as a sidenote about syntax: you can do while(boolname) for while(true) or while(!boolname) for while(false).



If you posted your code I could give it a quick skim and tell you where a bool might help things out. Typically a program can be shortened a bit by using case over complex if/else, and bool over uncontrolled while.
Chris C
2011-06-05 18:42:15 UTC
Well, a simple flag/bool is no different than performing any other check.

Now, the thing is that using flags/bools, often will increase the number of lines of code.



Here's an example where the flag/bool makes the program longer:



int main()

{

  bool myflag = false;

  int iSomeVal = 10;



  if (iSomeVal == 10)

    myflag = true;



  if (myflag)

    cout << "iSomeVal = 10" << endl;

  else

    cout << "iSomeVal != 10" << endl;



  return 0;

}



Here's the same thing done without using a flag/bool:

int main()

{

  int iSomeVal = 10;



  if (iSomeVal == 10)

    cout << "iSomeVal = 10" << endl;

  else

    cout << "iSomeVal != 10" << endl;



  return 0;

}



From the sounds of it, you need to simplify some things. So, if you're code is repeating the same mini-process more than one time that takes several lines of code or more, you can make it simpler by moving those lines into a function/method of their own.



An example of that would be like this:

int main()

{

  int iSomeVal = 10;



  if (iSomeVal == 10)

    cout << "iSomeVal = 10" << endl;

  else

    cout << "iSomeVal != 10" << endl;



  iSomeVal = 50;

  if (iSomeVal == 10)

    cout << "iSomeVal = 10" << endl;

  else

    cout << "iSomeVal != 10" << endl;



  return 0;

}



You'll notice that the same 4 lines are repeated. For our example, this can be a function call:

void showResult(int val)

{

  if (val == 10)

    cout << "val = 10" << endl;

  else

    cout << "val != 10" << endl;

}



int main()

{

  int iSomeVal = 10;



  showResult(iSomeVal);



  iSomeVal = 50;

  showResult(iSomeVal);



  iSomeVal = 65;

  showResult(iSomeVal);



  return 0;

}
SomeGuy
2011-06-05 15:23:33 UTC
bools and flags help your program run faster. They take up less space in memory and especially in larger programs you'll need stuff to take up less memory so that your program will run more effeciently.



Now I can't say whether bools and flags would make your code short because I cannot see your code because you haven't posted it. Sometimes adding bools and flags can make your program shorter and easier and sometimes it can't. If your professor said that it will make it easier and shorter then it most likely will make the code shorter and since his/her code is 500 lines with comments and yours is 600 without comments then your code is definately too long



So use flags and bools



Hope This Helped :D
wasi
2011-06-05 15:17:42 UTC
Flags are used to check for error cases.. If a flag is raised then you just have to exit the program with and errror message (or do something else). It is a good style to program beacuse you may have less bugs in the end.

For example:



...

bool check;

check = my_func();

// my_func may do something useful and would return false upon failure

if (check == false)

// cout error message

return -1;

else

// Continue with the program
shults
2016-11-09 01:50:00 UTC
I majored in lit in college. i might say for this poem, you opt for some historic past on the poet. It starts off out kinda unhappy, with the view. 'Futures i've got divorced' maximum like refers to abandoned hopes and desires. The 'do no longer look lower back' and knife reference is probably approximately getting over painful thoughts. That it no longer cuts implies wish. Or probably numbness. The stuff on complacency probably skill he feels he permit somebody down by skill of no longer doing something he might desire to have accomplished. to that end the accusatory voices. The final paragraph is a little extra complicated. My experience is that the narrator discovered his existence training and acted subsequently, to that end the vast advantages. This guy feels like a soldier to me. one that believes that no longer taking action at some needed 2nd led to others harm. Now he's became that attitude around, and located some peace. He gave up old and futile tries and took a distinctive way of living, and he's commencing to discover his existence extra priceless. i do no longer generally like poems, yet my mothers and fathers spents hundreds that they did no longer have so i might desire to evaluate to interpret then. i'm extra approximately novels, nonetheless. lower back, you will likely earnings from understanding something of this author's existence. This one is fantastically very own, previous the final experience of the ask your self and cruelty of the international I frequently see in lesser poems.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...