Stupid NOOB needs help with easy bool while program termination.

Hi,
So I am trying to get my while loop to terminate the program with a Y/N question, but no matter what value is entered the loop keeps going :(

any help would be appreciated, also my quad finder is returning weird results (try 5, -6, 1).

thanks for looking!


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
// This function welcomes the user.
void welcomeStatement ()
{
cout << endl;
cout << "This program computes a quadratic equation. " << endl;
cout << endl;
cout << endl;
}

//------------------------------------------------------------------------------
// This function gets the data.
int getData (string prompt)
{
int result;

cout << prompt << endl;
cin >> result;

return result;
}

//------------------------------------------------------------------------------
// This function checks if there are real roots.
bool isRootReal (int a, int b, int c)
{
bool result;
if (b * b - 4 * a * c >= 0 )
{
result=true;
}
else
{
result=false;
}
return result;
}

//------------------------------------------------------------------------------
//This function computes the positive root.
double posRootFunc (int a, int b, int c)
{
double result;
result = (-1*b+sqrt(b-4*a*c))/(2*a);

return result;
}

//------------------------------------------------------------------------------
//This function computes the negative root.
double negRootFunc (int a, int b, int c)
{
double result;
result = (-1*b-sqrt(b-4*a*c))/(2*a);

return result;
}

//------------------------------------------------------------------------------
//This is the real root output function
void outputRealRoot ( double r1, double r2 )
{
cout << endl;
cout << fixed;
cout << setprecision (2);
cout << " The roots are " << r1 << " and " << r2 << endl;
cout << endl;
}

//------------------------------------------------------------------------------
//This is the non real root output function
void outputNonRealRoot ()
{
cout << endl;
cout << "There are no real roots." << endl;
cout << endl;
}

//------------------------------------------------------------------------------
// This is the continuation function.
bool cont ()
{
bool result;
char answer;

cout << "Do you want to geek some more? Y/N: " << endl;
cin >> answer;

if (answer == 'Y' || 'y' )
{
result=true;
}
else
{
result=false;
}
return result;
}

//------------------------------------------------------------------------------
int main ()
{
int a;
int b;
int c;
double r1;
double r2;


welcomeStatement ();

bool result(true);
while (result==true)
{
a = getData ("Enter the x^2 coefficient, it cannot be zero. ");
if (a == 0 )
{
cout << "X^2 coefficient cannot be zero" << endl;
cin >> a;
}

cout << endl;
b = getData ("Enter the x coefficient ");
cout << endl;
c = getData ("Enter the constant ");
cout << endl;


if (isRootReal (a, b, c) == true )
{
r1 = posRootFunc ( a, b, c);
r2 = negRootFunc ( a, b, c);
outputRealRoot (r1, r2);
}
else
{
outputNonRealRoot ();
}
result = cont();
}
system ("pause");
return 0;
}
/* Ending of Source */
First, please use code tags like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main ()
{
int a;
int b;
int c;
double r1;
double r2;


welcomeStatement ();

bool result(true);
while (result==true)
{
a = getData ("Enter the x^2 coefficient, it cannot be zero. ");
if (a == 0 )
{
cout << "X^2 coefficient cannot be zero" << endl;
cin >> a;
}

cout << endl; 
b = getData ("Enter the x coefficient ");
cout << endl;
c = getData ("Enter the constant ");
cout << endl;


if (isRootReal (a, b, c) == true )
{
r1 = posRootFunc ( a, b, c);
r2 = negRootFunc ( a, b, c);
outputRealRoot (r1, r2);
}
else
{
outputNonRealRoot ();
}
result = cont();
}
system ("pause"); 
return 0;
}

Second, please post only relevant code. In this case that's the main function and the cont () function.
Third, the problem is this:if (answer == 'Y' || 'y' )
You have to restate the variable, like so:if (answer == 'Y' || answer=='y')
Try that and see if it works.
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
double a,b,c,d;
char cont='y';
for(;cont=='y'||cont=='Y';)
{
cout<<"Enter the quadratic coefficients"<<endl;
cout<<"Enter a: ";
cin>>a;
cout<< "Enter b: ";
cin>>b;
cout<< "Enter c: ";
cin>>c;
printf ("The equation is %lfX2+%lfX+%lf\n",a,b,c);
d=b*b-4*a*c;
if (d==0)
{
cout<<"R1=R2="<<(-1*b/2*a)<<endl;
}
if (d>0)
{
cout<<"R1="<<(-1*b+sqrt(d))/2*a<<endl;
cout<<"R2="<<(-1*b+sqrt(d))/2*a<<endl;
}
else
{
d*=-1;
printf ("R1=%lf+%lfi\n",-1*b/2*a,sqrt(d)/2*a);
printf ("R1=%lf-%lfi\n",-1*b/2*a,sqrt(d)/2*a);
}
cout << "Want some more? :P Y/N ";
cin >> cont;
}
return 0;
}
Works.
Topic archived. No new replies allowed.