Again

Pages: 12
closed account (iAk3T05o)
It's not my code:
1
2
3
4
5
bool valid (surname, name)
{
cout << "You've entered a wrong name or surname"
. . .
}

I know it's a bool function but how would a full program use it.
I asked yersterday but didn't really get the bool functions explanation.
Assuming the code was completed, you could use it as a condition for if statements, loops, etc.
closed account (iAk3T05o)
Please could you use something similar in an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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;
}
closed account (iAk3T05o)
So !isValid means once the user enters a value less than 0 or greater than 100, the program should end.
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?
Last edited on
closed account (iAk3T05o)
1
2
3
4
5
6
7
8
9
10
int calc_add (int a, int b)
{
return a + b;
}
int main()
{
int x = 10;
int y = 5;
calc_add (x, y);
std::cout << calc_add;


I may/may not have made mistakes there.

closed account (iAk3T05o)
While checking the net, i saw this:
 
while(true)

and i have been seeing it several times and don't know what it does.
closed account (j3Rz8vqX)
The proper code might look somewhat like this:
1
2
3
4
5
6
7
8
9
10
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) 

closed account (NyqLy60M)
@Nathan2222:

While loops work off of a conditional check, such as
1
2
3
4
5
6
int x = 0;
while(x <= 15)
{
        std::cout << x << std::endl;
        x++;
}


If you insert a constant instead (true in your case), you've got an infinite loop - that is, until you manually break out.
closed account (j3Rz8vqX)
If you have a true comparison in a while, you must consider other means of exiting the loop; if not, you will have an infinite loop.

Examples:
1
2
3
4
5
6
7
int x = 0;
do
{
   if(x==10)
      break;
   x++;
}while(true);

1
2
3
4
5
6
7
int x = 0;
while(true)
{
   if(x==10)
      break;
   x++;
}

closed account (iAk3T05o)
So it may be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}

I have a feeling i've made mistakes.
closed account (NyqLy60M)
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--;
		else
			break;
	}

	return value;
}
Last edited on
closed account (j3Rz8vqX)
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)
      return true;
   return false;
}
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;
}

closed account (iAk3T05o)
1
2
return true;
return false;

means?
closed account (NyqLy60M)
http://www.cplusplus.com/doc/tutorial/
closed account (j3Rz8vqX)
In a function with return type of boolean, you must return a boolean otherwise you'd get undefined behavior or simply cannot be compiled.

1
2
3
4
5
6
bool isValid(int value)
{
   if(value == 10)
      return true;
   return false;
}

In the above case, isValid would return true if you entered 10, and false if you entered anything else.

http://en.cppreference.com/w/cpp/language/return
http://msdn.microsoft.com/en-us/library/k68ktdwf.aspx

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.
closed account (iAk3T05o)
@mikeyboy: did you read the question? Did you see the function code i wrote?
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.
Pages: 12