boolalpha

Hello, I tried to change the number 1 or 0 to true and false. No matter where I put it, seem not to be working.

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
#include <iostream>

bool equal_or_not(int a, int b)
{

    return (a == b);
}

int get_input_from_user()
{
    int input{0};
    std::cout << "Your number please: ";
    std::cin >> input;

    return input;
}

void print_result(int a)
{
    std::cout << std::boolalpha << "is " << a ;
}

int main()
{
    int a{get_input_from_user()};
    int b{get_input_from_user()};
    int sum{equal_or_not(a, b)};
    print_result(sum);
    return 0;
}


Other thing, I tried to bring the line 27 to 20. Before I post this, I had line 20 using two parameter. But I realized, I have no idea how to put line 27 to line 20 since its one parameter to two parameter. What is the suggest to move line 27 without removing the function.

So I tried something else, I can feel I'm close to it but, this is the idea of what I'm trying to do. I know line 27 is wrong.

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
#include <iostream>

bool equal_or_not(int a, int b)
{

    return (a == b);
}

int get_input_from_user()
{
    int input{0};
    std::cout << "Your number please: ";
    std::cin >> input;

    return input;
}

void print_result(int a, int b)
{
    std::cout << "Is " << a << "equal to " << b << "? \n";
}

int main()
{
    int a{get_input_from_user()};
    int b{get_input_from_user()};
    print_result(equal_or_not(a, b), equal_or_not(a, b));
    return 0;
}
Last edited on
int is not a bool . You're using int when you should be using a bool. ie a==b returns type bool, not type int. boolalpha works for type bool.

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
include <iostream>

bool equal_or_not(int a, int b)
{
	return (a == b);
}

int get_input_from_user()
{
	int input;
	std::cout << "Your number please: ";
	std::cin >> input;

	return input;
}

void print_result(bool a)
{
	std::cout << std::boolalpha << "is " << a;
}

int main()
{
	int a {get_input_from_user()};
	int b {get_input_from_user()};
	bool sum {equal_or_not(a, b)};

	print_result(sum);
	return 0;
}

OO I see it now thank!

fixed it!

Im still working on the second idea. I think I need to review the old chapter again. I'm really bad at reading but great at looking at example. The tutorial I have didn't show enough example about function call in function call.

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
#include <iostream>

bool equal_or_not(int a, int b)
{

    return (a == b);
}

int get_input_from_user()
{
    int input{0};
    std::cout << "Your number please: ";
    std::cin >> input;

    return input;
}

void print_result(bool a)
{
    std::cout << std::boolalpha << "Is " << a;
}

int main()
{
    int a{get_input_from_user()};
    int b{get_input_from_user()};
    int sum{equal_or_not(a,b)};
    print_result(sum);
    return 0;
}
Last edited on
L9 should be

 
int get_input_from_user()



Isn't this flagged as an error by the compiler?
I revisit the tutorial just now, you are right... For some reason there no error from my compiler. Do you have any idea what I can do to change line 18 to two parameter?

1
2
3
4
void print_result(bool a, bool b)
{
    std::cout << std::boolalpha << "Is " << a << "equal to " << b << "?\n";
}


the int main is part I'm stuck on.
Last edited on
I finally got it, I was so focusing on one small part, my apologize. this is the solution I found.

Please let me know if there is anything I missed.

any way to shorter it will be great lesson for me. Thank in advance!

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
33
34
35
36
37
38
39
#include <iostream>

int get_input_from_user()
{
    int integer{0};
    std::cin >> integer;

    return integer;
}

bool number_equal(int first_number, int second_number)
{
    return first_number == second_number;
}

void output_answer(bool result)
{
    std::cout << std::boolalpha << result;
}

void output_answer2(int first_number, int second_number)
{
    std::cout << "Is " << first_number << " equal to " << second_number << "?\n";
}

int main()
{
    std::cout << "First integer please: ";
    int first_input{get_input_from_user()};
    std::cout << "Second integer please: ";
    int second_input{get_input_from_user()};

    output_answer2(first_input, second_input);

    int sum{number_equal(first_input, second_input)};
    output_answer(sum);

    return 0;
}
Last edited on
I got to next chapter about if and else.

I made this and it seem to work perfectly fine. I didnt know you could have two return in one function.

I tried the two return just to see if it work. Turn out it work.

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
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

int get_input_from_user()
{
    int integer{0};
    std::cin >> integer;
    if (integer > 0)
    return integer;
    else
        std::cout << "Please pick number above zero \n";
    std::cin >> integer;
    return integer;
}

bool number_equal(int first_number, int second_number)
{
    return first_number == second_number;
}

void output_answer(bool result)
{
    std::cout << std::boolalpha << result;
}

void output_answer2(int first_number, int second_number)
{
    std::cout << "Is " << first_number << " equal to " << second_number << "?\n";
}

int main()
{
    std::cout << "First integer please: ";
    int first_input{get_input_from_user()};
    std::cout << "Second integer please: ";
    int second_input{get_input_from_user()};

    output_answer2(first_input, second_input);

    int sum{number_equal(first_input, second_input)};
    output_answer(sum);

    return 0;
}
Last edited on
Formatting this gives:

1
2
3
4
5
6
7
if (integer > 0)
    return integer;
else
    std::cout << "Please pick number above zero \n";

std::cin >> integer;
return integer;


The only statement controlled by the else is std::cout << "please.... The following std::cin >> will be executed whether the if condition is true or false. However in this case, the if true condition is a return statement, so the std::cin is not executed if the if condition is true.

In this case, you don't need the else statement and can do:

1
2
3
4
5
6
if (integer > 0)
    return integer;

std::cout << "Please pick number above zero \n";
std::cin >> integer;
return integer;


To control more than one statement, you use {} eg

1
2
3
4
5
6
7
if (integer > 0)
    return integer;
 else {
    std::cout << "Please pick number above zero \n";
    std::cin >> integer;
    return integer;
}


However, you would probably code this as:

1
2
3
4
5
6
if (integer <= 0) {
    std::cout << "Please pick number above zero \n";
    std::cin >> integer;
}

return integer;


Note that in all cases if a number less than 0 is entered a second time it is accepted and not rejected.

Last edited on
Topic archived. No new replies allowed.