If-Statement

The if-else-statement doesn't work correctly.

Page 1 of 1

4 Replies - 1384 Views - Last Post: 18 April 2009 - 09:22 AM Rate Topic: -----

#1 rentalmetard5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-March 09

If-Statement

Posted 17 April 2009 - 05:31 PM

I have made a program to calculate an answer of the quadratic equation. And in the solve step I have included an if-else statement so that it will not compute anything and exits if the discriminant is equal-to or less-than zero. But whenever I make it less than zero, it still calculates and just gives me some rubbish. Any help would be appreciated!

Definitions:
equations::equations()
{
	a = 0;
	b = 0;
	c = 0;
}

qEquations::qEquations(){}

void equations::setVariables(float newa, float newb, float newc)
{
	cout << "Based on the definition of the Quadratic formula above," << endl;
	cout << "please enter the value for the three variables." << endl;
	cout << "a = ?" << endl;
	cin >> newa;
	cout << "b = ?" << endl;
	cin >> newb;
	cout << "c = ?" << endl;
	cin >> newc;
	a = newa;
	b = newb;
	c = newc;
}

void qEquations::calculateNegative()
{
	float temp;
	temp = sqrt((b*b)-(4*a*c));
	cout << "The discriminant is " << temp << "." << endl;
	cout << "The value under the square root sign is " << (b*b)-(4*a*c) << "." << endl;
	if (temp < 0)
	{
		cout << "Cannot take the square root of a number equal-to or less-than 0." << endl;
		system("PAUSE");
		exit (1);
	}
	else
	{
	cout << "The discriminant is " << temp << endl;
	negAnswer = (-b-temp)/(2*a);
	}
}

void qEquations::calculatePositive()
{
	float temp;
	temp = sqrt((b*b)-(4*a*c));
	posAnswer = (-b+temp)/(2*a);
}

void qEquations::printAnswer() const
{
	cout << "The answer using the minus sign is " << negAnswer << endl;
	cout << "The answer using the plus sign is " << posAnswer << endl;
}



Main:
#include "Header.h"

	float newa = 0;
	float newb = 0;
	float newc = 0;

int main()
{
	equations equation;
	qEquations qEquation;

	qEquation.setVariables(newa, newb, newc);
	qEquation.calculateNegative();
	qEquation.calculatePositive();
	qEquation.printAnswer();

	system("PAUSE");
	return 0;
}



Header:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class equations
{
public:
	void printIntro() const;
	void printEnd() const;
	void setVariables(float, float, float);
	equations();

protected:
	float a;
	float b;
	float c;
};

class qEquations: public equations
{
public:
	void calculatePositive();
	void calculateNegative();
	qEquations();
	void printAnswer() const;

protected:
	float negAnswer;
	float posAnswer;
};



**I have purposely left out the "Print Intro" and "Print End," they are just cout's explaning the project.

Is This A Good Question/Topic? 0
  • +

Replies To: If-Statement

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: If-Statement

Posted 17 April 2009 - 05:49 PM

The call to exit() is not working as you expect it to.
Read some more here:
http://www.cplusplus...y/cstdlib/exit/

Consider either:
1 - using an assert() statement instead:
http://www.cplusplus...cassert/assert/
Simple to fit into your current code structure.

2 - Making major changes to your program structure and making the function return a value (instead of void) and that return value is used as a success/error flag that you evaluate before processing the results.
Was This Post Helpful? 0
  • +
  • -

#3 rentalmetard5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-March 09

Re: If-Statement

Posted 17 April 2009 - 06:18 PM

View Postjanotte, on 17 Apr, 2009 - 04:49 PM, said:

The call to exit() is not working as you expect it to.
Read some more here:
http://www.cplusplus...y/cstdlib/exit/

Consider either:
1 - using an assert() statement instead:
http://www.cplusplus...cassert/assert/
Simple to fit into your current code structure.

2 - Making major changes to your program structure and making the function return a value (instead of void) and that return value is used as a success/error flag that you evaluate before processing the results.



But it seems like the if-route isn't being taken at all. I've commented out the equation and just assigned a negative number, and it worked fine. But when I put the equation back in it screws up again. I don't know if it is only the exit(1) call. Because if it is less than zero, it doesn't give any of the statements in the if-route. Nor the system("PAUSE"). Any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: If-Statement

Posted 17 April 2009 - 07:12 PM

OK I weakened and compiled your code.

Having done that I discovered this output that you hadn't shared with us.
Always good to show us the actual output because it helps to know what is really happening and it is easier for you to cut and paste the real output instead of trying to describe the output.
"The discriminant is nan.'
"The value under the square root sign is -71.'

"nan" means not a number.

So of course the test inside the if() statement fails because you have corrupted temp earlier so the comparison to "0" is without meaning.

The reason you are getting "nan" is because the contents of the parens is negative and sqrt will return "nan" when you pass it a negative value.
Read about it here:
http://www.cplusplus...ary/cmath/sqrt/

So you need a test before using the sqrt function to ensure the param is > 0.

Having had to play with you code a bit to get this far I have a lot of questions about why you have global variables and why you have constructed things they way you have but if it makes you happy as it is then that's what is important for now.

Although my original guess about exit() was a wrong path (because I couldn't understand your problem description) I still want to suggest that you use assert() instead of exit() for achieving what you are doing here. It makes debugging etc much easier.

This post has been edited by janotte: 17 April 2009 - 09:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 rentalmetard5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-March 09

Re: If-Statement

Posted 18 April 2009 - 09:22 AM

View Postjanotte, on 17 Apr, 2009 - 06:12 PM, said:

OK I weakened and compiled your code.

Having done that I discovered this output that you hadn't shared with us.
Always good to show us the actual output because it helps to know what is really happening and it is easier for you to cut and paste the real output instead of trying to describe the output.
"The discriminant is nan.'
"The value under the square root sign is -71.'

"nan" means not a number.

So of course the test inside the if() statement fails because you have corrupted temp earlier so the comparison to "0" is without meaning.

The reason you are getting "nan" is because the contents of the parens is negative and sqrt will return "nan" when you pass it a negative value.
Read about it here:
http://www.cplusplus...ary/cmath/sqrt/

So you need a test before using the sqrt function to ensure the param is > 0.

Having had to play with you code a bit to get this far I have a lot of questions about why you have global variables and why you have constructed things they way you have but if it makes you happy as it is then that's what is important for now.

Although my original guess about exit() was a wrong path (because I couldn't understand your problem description) I still want to suggest that you use assert() instead of exit() for achieving what you are doing here. It makes debugging etc much easier.


Thank you for your advice. As for the way it is constructed, I forced it to follow a guideline for an assignment I was given. And plus I'm still a novice programmer. I haven't finished my first class of Object Oriented Programming yet in college. So bare with me please! But again, thank you for your advice, and I understand now why the if-statement was failing. It makes so much sense, but I just looked over the fact that temp was never a number.

This post has been edited by rentalmetard5: 18 April 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1