I have never seen an example of this, but I need to call a function or another bool from inside a bool! If anyone knows how this might go, please let me know. P.S. This website is awsome! Thanks, Paul
//something like this, however this is not working...

bool getEmployeeData(int ID)
{
    cout << "Enter employee ID: " << endl;
    cin >> ID;

        if (ID >= 0)
        {   
            return true;
            bool isValidStatus;

        }

        else
        {   
            cout << "*** Program Terminated ***" << endl;
            exit (0);
            return false;
        }
}

bool isValidStatus (int status)
{
    cout << "Enter payroll status: " << endl;
    cin >> status;

    if (status == 's' || status == 'S' || status == 'c' || status == 'C' || status == 'h' || status == 'H')
    {
        cout << status;
        return tryAgain;
    }

    else
    {
        bool tryAgain (int quit);
        return false;
    }
}

Dani AI

Generated

Short summary and practical fix for the examples in this thread.

Several issues appear in 's snippet: function parameters are passed by value so cin >> ID only changes a local copy; code after return or exit is unreachable; status is treated with the wrong type and compared to character literals; and there are references to names that don't exist (for example tryAgain). is correct to insist on precise terminology, and 's post demonstrates a clean separation of concerns (input vs validation). A robust pattern is: (1) validation routines return bool and do not perform I/O, (2) I/O routines read input and either return the value or write into a reference, and (3) a loop at the call site repeats input until validation succeeds.

Example of that pattern (different from earlier samples):

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

bool isValidStatus(char c) {
  c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
  return c=='s' || c=='c' || c=='h';
}

bool readIntLine(const std::string& prompt, int& out) {
  std::string line;
  std::cout << prompt;
  if (!std::getline(std::cin, line)) return false;
  std::stringstream ss(line);
  return bool(ss >> out);
}

char readStatusLine(const std::string& prompt) {
  std::string line;
  while (std::cout << prompt, std::getline(std::cin, line)) {
    for (char ch : line) if (!std::isspace(static_cast<unsigned char>(ch)))
      if (isValidStatus(ch)) return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
    std::cout << "invalid status, try s/c/h\n";
  }
  return '\0';
}

Troubleshooting notes: avoid returning before needed work is done; prefer returning error codes or false from helper functions instead of calling exit deep in the code; handle std::cin errors (clear/ignore) or use std::getline+parsing; compile with warnings enabled (-Wall -Wextra) to catch unreachable code and mismatched types. This keeps logic clear and makes calls like if (isValidStatus(s)) straightforward and reliable.

Recommended Answers

All 4 Replies

>but I need to call a function or another bool from inside a bool!
Work on your terminology first, because I have no clue what you want. Let's get this straight, bool is a type. bool has two values: true and false. You don't call squat from inside a bool because all it does is represent true or false, nothing else. You can have functions that return bool, or statements that result in bool, but they're not bool and shouldn't be referred to as such if you want to have meaningful conversation with intelligent parties.

Yes, you can call other functions from inside a bool. I may be a newbie, but I would appreciate some respect if you respond to me! Otherwise don't bother responding.

I do not really understand the problem that you have. But a few explanations might help

if (ID >= 0) {
    return true;            // you're returning from the call here
    bool isValidStatus;     // this statement is never reached
                            // here you are declaring a boolean variable isValidStatus. 
                            // this is not a call to the function of the same name;
}
else{
    cout << "*** Program Terminated ***" << endl;
    exit (0);              // same problem is here. the prog is terminated here
    return false;          // this statement is never reached
}

maybe you want something like this

#include <iostream>
using namespace std;

bool isValidStatus( char status ) {
  if (status == 's' || status == 'S' || status == 'c' || status == 'C' || status == 'h' || status == 'H'){
    return true;
  }
  cout << status << " is not valid ! try again" << endl;
  return false;
}

char getStatus() {
  char status;
  do {
     cout << endl << "Enter payroll status: ";
     cin >> status;
  } while ( ! isValidStatus(status) ); // repeat until a valid status is entered
  return status;
}

int getEmployeeData() {
  int ID = 0;
  cout << "Enter employee ID (0 to exit program ): ";
  cin >> ID;
  if ( ID == 0 ) {
    cout << "bye.." << endl;
    exit (0);     
  }         // terminate program
  cout << "employee ID= " << ID << endl;
  return ID;
}

int main() {
   int ID = getEmployeeData();   // read employee ID
   char status = getStatus();    // read Status
   cout << "the status entered was : " << status << endl; 
   return 0;              // main should return an exit-code
}

Hope this helps.
btw. Naru seems to be here to insult people.
K.

>Yes, you can call other functions from inside a bool.
Then you're not using the proper terminology. A bool is true or false, that's it. There's no way to call a function from inside a value. I suspect that what you're thinking of is calling a function in a conditional test:

#include <iostream>

using namespace std;

bool f()
{
  // Do something and return true or false
}

int main()
{
  if ( f() )
    cout<<"f() was true"<<endl;
}

or calling a function that returns bool from another function that returns bool:

#include <iostream>

using namespace std;

bool f1()
{
  // Do something and return true or false
}

bool f2()
{
  if ( f1() && something else )
    return true;
  else
    return false;
}

int main()
{
  if ( f() )
    cout<<"f() was true"<<endl;
}

>but I would appreciate some respect if you respond to me!
I do respect you for various reasons, but I don't respect your complete lack of an attempt to express yourself in a way that is conducive to getting help. I'm not going to guess what you want simply because you don't know how to ask the question in a way that we can understand. So let's go over this one more time.

A bool is a data type with two values. Functions can return bool, expressions can result in bool, but the only thing that can be inside bool is true or false that map to non-zero and zero integral values, respectively. Tell me how to call a function from within 0 and I'll reconsider this explanation.

>btw. Naru seems to be here to insult people.
I'm not responsible for your misconceptions. Though I don't like the insinuation that I don't help people here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.