#include <iostream>
bool isValid(int value)
{
return value > 0 && value < 100;
}
int main()
{
int num;
while(std::cout << "Please enter a number between 1 and 99: "
&& std::cin >> num
&& !isValid(num))
{
}
std::cout << "The number you entered was " << num << std::endl;
}
A function that returns a bool is not conceptually different from a function that returns any other type. Do you understand what it means for a function to return something? Do you understand how to use the value that a function returns?
int calc_add (int a, int b)
{
return a + b;
}
int main()
{
int x = 10;
int y = 5;
//calc_add (x, y); //Doesn't really do anything
std::cout << calc_add(x,y); //Prints out the returning value (a+b)
bool isValid(int value)
{
while (isValid = true)
{
value = 10;
value--;
}
return value //can i do return isValid?
}
int main()
{
int i;
while(isValid)
{
std::cout << isValid (i);
}
Your function has a boolean return type, so you can't return an integer(value).
isValid is the function name, you cannot do anything more with it than call the block of code it defines, so you should pass in a separate variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int isValid(int value, bool looping)
{
while (looping) // no need to include == true
{
std::cout << value << std::endl;
if (value > 0)
value--;
elsebreak;
}
return value;
}
When comparing, you'd want to use the equality operation rather than the assignment operation; unless assignment operation is your goal.
Also, i'm not sure what your trying to accomplish with the above code, but a simple bool function can be used as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
bool isValid(int value)
{
if(value == 10)
returntrue;
returnfalse;
}
int main()
{
int i;
std::cout<<"Enter a number: ";
std::cin>>i;
if(isValid(i))
std::cout<<"The value was valid: "<<i<<std::endl;
else
std::cout<<"The value was not valid: "<<i<<std::endl;
return 0;
}
Nathan, I don't mean to be rude, but if you don't yet understand what it means for a function to return a value, then you need to go back to your textbooks and read up on functions. This is fundamental, tutorial-level stuff, and there's no point asking people here to reproduce what you could find in any textbook or tutorial.
Of course I did. I'm telling you that the answers to your questions depend on you understanding the basics of how to call functions and how to return values from them. It's fairly clear that you don't yet have that understanding. Any attempts to answer your questions would involve us basically typing out things that you could find in any C++ textbook or tutorial, which would be a waste of our time.