How to add a true false boolean

I have this program and I need to add a Boolean field called AnswersValid initially this should be false, but after you plug in the various answers change it to true. How would I go about this. Here is my .cpp followed by my.h file

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "header.h"
#include <iomanip>

using namespace std;




int main(void) 
{
   char Reply;
   coetype header;

   do 
      {
      cout << "Quadratic Equation Solver" << endl << endl;
      input(header);
      solution(header);  
      PrintSolutions(header);
      cout << "Solve another (y/n)? ";
      cin >> Reply;
	  }
   while (Reply == 'y');
}

// Given: Nothing
// Task: Ask the user for the values for the coefficients a, b, and c.
// Return: a, b, and c.
void input(coetype & header)
{
      cout << "Enter the value of coefficient a: ";
      cin >> header.a;
      cout << "Enter the value of coefficient b: ";
      cin >> header.b;
      cout << "Enter the value of coefficient c: ";
      cin >> header.c;
}

// Given: The coefficients a, b, c of the quadratic equation 
// Task: Normalize a, b, c. 
// Return: return a, b, c in their normalized form.
void normalize(coetype & header)
{
	float Largest;

      if (fabs(header.a) > fabs(header.b))
         Largest = fabs(header.a);
      else
         Largest = fabs(header.b);
      
      if (fabs(header.c) > Largest)
         Largest = fabs(header.c);

      header.a = header.a / Largest;
      header.b = header.b / Largest;
      header.c = header.c / Largest;
}

// Given: a, b, c   The coefficients of a quadratic equation.
// Task:  Calculates the discriminant for the quadratic equation with these coefficients.
// Return:  This discriminant is returned in the function name.
float Discriminant(coetype & header)
{
   return pow(header.b, 2) - 4.0f * header.a * header.c;
}

// Given: a, b, c, the coefficients of a quadratic equation.
// Task: Is to use a, b, and c to find the real solutions.
// Return: x1 and x2, the solutions, where x1 and x2 are both valid when NumSolutions is 1,
//         only x1 is valid if NumSolutions is 1, and neither is valid if NumSolutions is 0.
//         NumSolutions:  the number of real solutions to the quadratic equation.
void solution(coetype & header)
{
	float Root;

	normalize(header);
	header.discrim = Discriminant(header);
	
    if (header.discrim < 0)
	   header.NumSolutions = 0;
    else if (header.discrim == 0)
       {
       header.x1 = -header.b / (2.0f * header.a);
	   header.NumSolutions = 1;
       }
    else   // Discriminant must be positive
	   {
       Root = sqrt(header.discrim);
       header.NumSolutions = 2;
     
	   if (-header.b > 0)
          header.x1 = (-header.b + Root) / (2.0f * header.a);
       else
          header.x1 = (-header.b- Root) / (2.0f * header.a);
            
       header.x2 = header.c / (header.a * header.x1);
       }

}

// Given: NumSolutions for the number of solutions that we have,
//        we are also given our answers x1, and x2 (where x1 and x2 are valid as in
//        the previous function.)
// Task: Is to print the answers of the quadratic equation to the screen.
// Return: Nothing.
void PrintSolutions(coetype & header)
{
	if (header.NumSolutions == 0)
       cout << "No real solutions" << endl;
	else if (header.NumSolutions == 1)
	   cout << "One real solution: " << "x1:" << header.x1 << endl;
	else
	   cout << "Two real solutions: " << "x1:" << header.x1 << " and " << "x2:" << header.x2 << endl;
}


Here is my .h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath> // Needed for the sqrt function.

using namespace std;
//Plus the definition of your struct type and your function prototypes.

typedef struct
{
	 float a, b, c;
     float x1, x2, discrim;
     int NumSolutions;

}coetype;

inline void normalize(coetype & header);
void solution(coetype & header);
void input(coetype & header);
inline float Discriminant(coetype & header);
void PrintSolutions(coetype & header);


closed account (j2NvC542)
Is that what you mean?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(void) 
{
   char Reply;
   coetype header;
   bool AnswersValid;

   do 
      {
      cout << "Quadratic Equation Solver" << endl << endl;
      AnswersValid = false;
      input(header);
      solution(header);  
      PrintSolutions(header);
      AnswersValid = true;
      cout << "Solve another (y/n)? ";
      cin >> Reply;
	  }
   while (Reply == 'y');
}
You should think about what would produce a valid/invalid answer. Perhaps have one of your void functions return a bool based on that criteria.
Topic archived. No new replies allowed.