#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.
#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;
}
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;
}
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.
#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;
}
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";
}
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.