c++ isValid Function always returning a true value. | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| |
I am new to C++, and one of the homework we have pertains to Functions. The problem is:
Write a function called isValid that determines whether any 3 values can represent the sides of a triangle. The main function should prompt a user to enter 3 sides of a triangle, call isValid and output valid or invalid. The isValid function should have 3 parameters and return a bool. Given sides a, b, c; if a + b > c and a + c > b and b + c > a, then the sides represent a valid triangle.
This is the code I wrote. It compiles with no errors, but the result is not correct. Example data I used were: 2, 2, 2, which should result as "VALID", and 1, 9, 7, which should return INVALID. I would appreciate any help you can give me on this. Thank you. -
#include <iostream>
-
using namespace std;
-
-
int isValid (int tri_a, int tri_b, int tri_c);
-
int validate;
-
-
-
int main ()
-
{
-
int a;
-
int b;
-
int c;
-
-
// Prompt the user to enter 3 values
-
cout << " Enter the 1st value: ";
-
cin >> a;
-
cout << " Enter the 2nd value: ";
-
cin >> b;
-
cout << " Enter the 3rd value: ";
-
cin >> c;
-
-
// Call isValid Function
-
void isValid ()
-
-
{
-
if (validate = true)
-
cout << "valid" << endl;
-
else
-
cout << "Invalid" << endl;
-
}
-
-
system ("pause");
-
-
} //end main
-
-
// The Function Main is executed, then calls the isValid function:
-
-
int isValid (int tri_a, int tri_b, int tri_c)
-
-
// Determine if the user inputs are valid.
-
{
-
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
-
validate = true;
-
else
-
validate = false;
-
-
return validate;
-
-
} // end isValid
-
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: c++ isValid Function always returning a true value. -
int isValid (int tri_a, int tri_b, int tri_c);
-
// Call isValid Function
-
void isValid ()
-
int isValid (int tri_a, int tri_b, int tri_c)
-
Do you see something wrong there? I'll give you a hint: One of these things is not like the other...
| | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by sicarie -
int isValid (int tri_a, int tri_b, int tri_c);
-
// Call isValid Function
-
void isValid ()
-
int isValid (int tri_a, int tri_b, int tri_c)
-
Do you see something wrong there? I'll give you a hint: One of these things is not like the other... The only difference I can see is that the when I called the function, the return type i used is VOID, and the other is INT.
Thank you.
| | Newbie | | Join Date: Sep 2007
Posts: 3
| | | re: c++ isValid Function always returning a true value.
Try changing line 26:
to: |  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: c++ isValid Function always returning a true value.
I'm still confused why this function is declared in the main.
And you don't declare a return for main.
However, I'm also curious as to why your functions are returning ints, but you attempt to re-declare it as void (in main) and don't store that return variable.
PS - Another thing, you're also not passing it anything, which will also be a problem.
| | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by Linara Try changing line 26:
to: I tried the == instead of the assignment = but it still produced the same results. Thank you for catching that, I'm sure I need to use the equal == instead of the = assignment sign.
I have been working on this problem for 3 days now. We're only on our 9th day of class.
Thanks.
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: c++ isValid Function always returning a true value.
You don't need to declare an entirely new function inside your main() - which is what you're doing with all this void isValid() nonsense. You've already got a perfectly fine function isValid outside main() which will do just what you need. Are you familiar with how how to call functions you've made, especially in capturing their return values as variables in main()?
| | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by Ganon11 You don't need to declare an entirely new function inside your main() - which is what you're doing with all this void isValid() nonsense. You've already got a perfectly fine function isValid outside main() which will do just what you need. Are you familiar with how how to call functions you've made, especially in capturing their return values as variables in main()?
To call the isValid function to Main is my problem. What I learned in class (an hour’s worth) was nothing like I would use here, (or maybe I’m just trying to make something ‘simple’ so complicated).
To call isValid function to main:
isValid = ( the argument is what I am having problems with);
I didn’t think that I needed to declare a new function, but I didn’t how else to call the function into Main. It would be easy if it is something like this example: addNumbers (n1, n2), and output the result.
Thank you. I appreciate your help.
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: c++ isValid Function always returning a true value.
To call a function, you just use its name and supply it the proper arguments from main(): - isValid(/*What would go here? Think about the work you've already done in main*/);
Because your function is manipulating a global variable validate, you don't need to worry about a return value. (As an aside, why bother making validate an int? If you're working in C++, make it a bool. That will make your life simpler.)
You may want to make a cleaner solution by eliminating the global variable and using the return value. In this case, you will have to return either true or false from isValid() - do you know how to return values?
Back in main, you will set a variable equal to isValid(): - bool result = isValid(/*What would go here? Think about the work you've already done in main*/);
Now result contains the return value of isValid.
| | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by Ganon11 To call a function, you just use its name and supply it the proper arguments from main(): - isValid(/*What would go here? Think about the work you've already done in main*/);
Because your function is manipulating a global variable validate, you don't need to worry about a return value. (As an aside, why bother making validate an int? If you're working in C++, make it a bool. That will make your life simpler.)
You may want to make a cleaner solution by eliminating the global variable and using the return value. In this case, you will have to return either true or false from isValid() - do you know how to return values?
Back in main, you will set a variable equal to isValid(): - bool result = isValid(/*What would go here? Think about the work you've already done in main*/);
Now result contains the return value of isValid.
Thanks again.
I apologize, but I'm a little lost.
I understand that it's better to change the data type to BOOL instead of INT. But I will still need to have a variable to return the result to (whether it's true or false).
bool isValid (int tri_a, int tri_b, int tri_c)
{
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
bool triangle = true;
else
bool triangle = false;
return result;
} // end isValid
Main will execute the statements (prompting the user to enter the values), then it will call the function isValid.
isValid has the results "true" or "false"
So in Main, I will have to write the code:
bool result = isValid (if triangle == true
cout << "Valid" << if triangle == false, cout<< "Invalid" << endl)
If I don't write the if statement in Main, the output "Valid" or "Invalid" will not print into the console.
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by pyramid bool isValid (int tri_a, int tri_b, int tri_c) What are the tri_a, tri_b, and tri_c variables? What do they represent? Those are what you want to pass, where do you get them?
| | Newbie | | Join Date: Oct 2007
Posts: 10
| | | re: c++ isValid Function always returning a true value. Quote:
Originally Posted by pyramid Thanks again.
I apologize, but I'm a little lost.
I understand that it's better to change the data type to BOOL instead of INT. But I will still need to have a variable to return the result to (whether it's true or false).
bool isValid (int tri_a, int tri_b, int tri_c)
{
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
bool triangle = true;
else
bool triangle = false;
return result;
} // end isValid
Main will execute the statements (prompting the user to enter the values), then it will call the function isValid.
isValid has the results "true" or "false"
So in Main, I will have to write the code:
bool result = isValid (if triangle == true
cout << "Valid" << if triangle == false, cout<< "Invalid" << endl)
If I don't write the if statement in Main, the output "Valid" or "Invalid" will not print into the console.
you are getting confused...
from your function just return triangle.
and in main() set a bool variable result to hold the value returned formt he function like this
bool result = isValid(parameters go here);
then use the if/else statement on result.
and you're done
| | Newbie | | Join Date: Oct 2007 Location: Charlotte, NC
Posts: 18
| | | re: c++ isValid Function always returning a true value.
A BIG THANK YOU to all of you.... My codes compiled in DEV, and it passed the automatic grading system in my class. You guys made my day (scratch that...) my entire week! Thank you.
/* This program will determine if the user inputs can represent the sides of a triangle. An IF/ELSE statement is used to determine if the user input is valid or not. */
#include <iostream>
using namespace std;
bool isValid (int tri_a, int tri_b, int tri_c);
bool triangle;
int main ()
{
int a;
int b;
int c;
// Prompt the user to enter 3 values
cout << " Enter the 1st value: ";
cin >> a;
cout << " Enter the 2nd value: ";
cin >> b;
cout << " Enter the 3rd value: ";
cin >> c;
// Call isValid Function
bool triangle = isValid (a, b, c);
{
if (triangle == true)
cout << "valid" << endl;
else
cout << "invalid" << endl;
}
system ("pause");
return 0;
} //end main
// The Function Main is executed, then calls the isValid function:
bool isValid (int tri_a, int tri_b, int tri_c)
// Determine if the user inputs are valid.
{
if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
triangle = true;
else
triangle = false;
return triangle;
} // end isValid
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|