473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

c++ isValid Function always returning a true value.

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.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int isValid (int tri_a, int tri_b, int tri_c);
  5. int validate;
  6.  
  7.  
  8. int main ()
  9. {
  10. int a;
  11. int b;
  12. int c;
  13.  
  14.  // Prompt the user to enter 3 values 
  15.      cout << " Enter the 1st value:    ";
  16.      cin >> a;
  17.      cout << " Enter the 2nd value:    ";
  18.      cin >> b;
  19.      cout << " Enter the 3rd value:    "; 
  20.      cin >> c;  
  21.  
  22. // Call isValid Function 
  23.     void isValid ()
  24.  
  25.     {
  26.      if (validate = true) 
  27.      cout << "valid" << endl;
  28.      else
  29.      cout << "Invalid" << endl;
  30.      }
  31.  
  32.  system ("pause");
  33.  
  34. } //end main
  35.  
  36. // The Function Main is executed, then calls the isValid function: 
  37.  
  38. int isValid (int tri_a, int tri_b, int tri_c)  
  39.  
  40.   // Determine if the user inputs are valid.
  41. {  
  42.     if ((tri_a + tri_b > tri_c) && (tri_a + tri_c > tri_b) && (tri_b + tri_c > tri_a))
  43.     validate = true;
  44.      else
  45.     validate = false;
  46.  
  47.     return validate;       
  48.  
  49.  } // end isValid
  50.  
Oct 9 '07 #1
12 14339
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. int isValid (int tri_a, int tri_b, int tri_c);
  2. // Call isValid Function 
  3.     void isValid ()
  4. int isValid (int tri_a, int tri_b, int tri_c)  
  5.  
Do you see something wrong there? I'll give you a hint: One of these things is not like the other...
Oct 9 '07 #2
pyramid
18
Expand|Select|Wrap|Line Numbers
  1. int isValid (int tri_a, int tri_b, int tri_c);
  2. // Call isValid Function 
  3.     void isValid ()
  4. int isValid (int tri_a, int tri_b, int tri_c)  
  5.  
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.
Oct 9 '07 #3
Linara
3
Try changing line 26:

Expand|Select|Wrap|Line Numbers
  1. if (validate = true)
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. if (validate == true)
  2.  
Oct 9 '07 #4
sicarie
4,677 Expert Mod 4TB
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.
Oct 9 '07 #5
pyramid
18
Try changing line 26:

Expand|Select|Wrap|Line Numbers
  1. if (validate = true)
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. if (validate == true)
  2.  
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.
Oct 9 '07 #6
Ganon11
3,652 Expert 2GB
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()?
Oct 9 '07 #7
pyramid
18
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.
Oct 9 '07 #8
Ganon11
3,652 Expert 2GB
To call a function, you just use its name and supply it the proper arguments from main():

Expand|Select|Wrap|Line Numbers
  1. 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():

Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 9 '07 #9
pyramid
18
To call a function, you just use its name and supply it the proper arguments from main():

Expand|Select|Wrap|Line Numbers
  1. 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():

Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 9 '07 #10
sicarie
4,677 Expert Mod 4TB
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?
Oct 9 '07 #11
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
Oct 9 '07 #12
pyramid
18
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
Oct 9 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Tomlinson | last post by:
The keyword new is required on 'B.IsValid' because it hides inherited member 'A.IsValid' All I have a couple of classes which resemble (sort of!) this: public class A { public bool bIsValid =...
1
by: Jon Davis | last post by:
This is always returning True, even when fields are empty. Why? private bool ValidatePageOne() { bool ret = true; foreach (Control ctrl in PageOnePanel.Controls) { if...
3
by: Jon Davis | last post by:
This is always returning True, even when fields are empty. Why? private bool ValidatePageOne() { bool ret = true; foreach (Control ctrl in PageOnePanel.Controls) { if...
2
by: Jeremy | last post by:
The code is in a server control: If Me.Page.IsValid Then ExecuteSomething End If ExecuteSomething is always called and the validators always return True for IsValid, even though their...
13
by: Miro | last post by:
Maybe this is just some bad habits picked up... But why not write everything in VB as a Function ? -If required later on you can always pass back a value. -You can pass in variables as required...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
6
by: Mike Chen | last post by:
We know in server code, we can check the page validated by using Page.IsValid where we put some validator controls on aspx page. I want to set some value after validating user input values on...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
5
by: Travis | last post by:
I am using a function that returns a const char * that is usually a word, etc. How can I check to see if what it returns is empty? I tried if (function() == "") and (function() == NULL) and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.