passing bool function result to another function

i need to write two separate functions

one is bool that checks if the input is valid
the other is a double function that does something else.

my professor wants that the double function returns to -1 if the input is invalid.
and he is going to grade the two functions by using assert()

in this case, should i bring the bool result from the bool function and write as "if (boolResult = 0) return -1"
or should I write the boolean expression for the second function and return -1 if it's invalid??

the bool function is really long so I prefer to bring bool function result to the double function
if i can bring the result, how can i write it??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>;
#include<string>;
#include<cctype>;

using namespace std;
 
bool a(string s);
double b(string s, string word);

(omitted function descriptions)

string command;
string targetAir;
int main() {
	cout << "Enter a string: ";
	getline(cin, command);
	cout << "Enter valid two alphabets to be detected in the string: ";
	getline(cin, targetWord);

	cout << "function a returns" << a(command) << endl;
	cout << "function b returns" << b(command, targetWord) << endl;

	return 0;
}
1
2
3
if (boolResult = 0)//assigning here, not checking for equality
//should be
if(boolResult == 0)


based on the information you've provided, here's one suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>

bool boolFunc(const std::string& s)
{
    return s.size()%2 == 0;
}
double doubleFunc(std::string& s, std::string word)
{
    std::cout << "Enter base string: \n";
    getline(std::cin, s);
    std::cout << "Enter second string: \n";
    getline(std::cin, word);
    if(boolFunc(s))
    {
        word += s;
        return word.size();
    }
    else
    {
        return -1;
    }
}
int main()
{
    std::string s{}, word{};
    double result = doubleFunc(s, word);
    assert(fmod(result,2) == 0);
    std::cout << result;
}

Also, if you haven't seen this already, here's a good discussion of the assert() function:
http://stackoverflow.com/questions/1571340/what-is-the-assert-function


Topic archived. No new replies allowed.