Errors during calculating answers.

Hello, I am trying to code a program that can solve quadratic equations, calculate the circumference or surface area, addition, subtraction, multiplication and division. I tried to make the program solve simultaneous equations to find out the answer of x and y. Unfortunately, when the program tries to calculate x and y, it returns an error (-1.#IND) for both x and y. I went through the code numerous times and I can't seem to find a problem with the code. I tested the other 7 modes and they all seem to function perfectly. Can you help me?
To understand what I need help with, try running the code provided, press 8, 2, 3, 3, 2, 130, 170 and look at the two answers. They should be "Your first answer is 10" and "Your second answer is 50" (or the other way round) but they return errors instead.
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace std;


void clearBuffer()
{
    cin.clear();
    cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}


int radius;

double pi = 3.14159265358979323846;

double a;
double b;
double c;
double v;
double w;
double x;
double y;

double x1;
double x2;
double x3;
double y11 = y * v;
double y2 = w * x;
double y3 = y11 - y2;


double answer1;
double answer2;
double answer3 = answer1 - answer2;

double yfinal = answer3 / y3;

double xtimesx = answer1 - (yfinal * y);

double finalanswer;
double xfinalanswer = xtimesx / x;


char choice;

double addition (double a, double b)
{
       return (a+b);
}

double subtraction (double a, double b)
{
       return (a-b);
}

double multiplication (double a, double b)
{
       return (a*b);
}

double division (double a, double b)
{
       return (a/b);
}

double quadratica (double a, double b, double c)
{
    return (-b + sqrt(b*b - 4*a*c))/(2 * a);
}

double quadraticb (double a, double b, double c)
{
    return (-b - sqrt(b*b - 4*a*c))/(2 * a);
}

double circle_area_function (double radius)
{
       return (pi*(radius * radius));
}

double circumference_function (double radius)
{
       return (2 * pi * radius);
}


int main(){

    cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
    cout << "Pick which kind of formulae do you want to use:" << endl;
    cout << "1) ax^2  +  bx  +  c  =  0" << endl;
    cout << "2) pi*r^2" << endl;
    cout << "3) 2 * pi * r" << endl;
    cout << "4) a+b" << endl;
    cout << "5) a-b" << endl;
    cout << "6) a*b" << endl;
    cout << "7) a/b" << endl;
    cout << "8) Simultaneous equations" << endl;

    cin >> choice;
    {
             clearBuffer();
             }
    
    if (choice == '1'){
    cout << "Input your a:" << endl;
    cin >> a;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Input your b:" << endl;
    cin >> b;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "input your c:" << endl;
    cin >> c;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
          {
          double x;
          x = quadratica (a,b,c);
          cout << "Your first answer is " << x << endl;
          }
     {
     double y;
     y = quadraticb (a,b,c);
     cout << "Your second answer is " << y << endl;
     }
}
     if (choice == '2'){
               cout << "Input your radius." << endl;
               cin >> radius;
               cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
               {
               double circle_area;
               circle_area = circle_area_function (radius);
               cout << "Your answer is " << circle_area << endl;
               }
}
     if (choice == '3'){
                cout << "Insert your radius." << endl;
                cin >> radius;
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                {
                double circumference;
                circumference = circumference_function (radius);
                cout << "Your answer is " << circumference << endl;
                }
}
     if (choice == '4'){
                cout << "Insert your a." << endl;
                cin >> a;
                cout << "Insert your b." << endl;
                cin >> b;
                {
                double addition_answer;
                addition_answer = addition (a, b);
                cout << "Your answer is " << addition_answer << endl;
                }
}
     if (choice == '5'){
                cout << "Insert your a." << endl;
                cin >> a;
                cout << "Insert your b." << endl;
                cin >> b;
                {
                double subtraction_answer;
                subtraction_answer = subtraction (a, b);
                cout << "Your answer is " << subtraction_answer << endl;
                }
}
     if (choice == '6'){
                cout << "Insert your a." << endl;
                cin >> a;
                cout << "Insert your b." << endl;
                cin >> b;
                {
                double multiplication_answer;
                multiplication_answer = multiplication (a, b);
                cout << "Your answer is " << multiplication_answer << endl;
                }
}
     if (choice == '7'){
                cout << "Insert your a." << endl;
                cin >> a;
                cout << "Insert your b." << endl;
                cin >> b;
                {
                double division_answer;
                division_answer = division (a, b);
                cout << "Your answer is " << division_answer << endl;
                }
}
     if (choice == '8'){
                cout << "Insert your first x." << endl;
                cin >> x;
                cout << "Insert your first y." << endl;
                cin >> y;
                cout << "Insert your second x." << endl;
                cin >> v;
                cout << "Insert your second y." << endl;
                cin >> w;
                cout << "Insert your first answer." << endl;
                cin >> answer1;
                cout << "Insert yout second answer." << endl;
                cin >> answer2;
                cout << "Your answer is " << xfinalanswer << " and " << yfinal << endl;
}
     
system("pause");
}


I will appreciate any help.
Thank you!
Last edited on
You have some ambiguity with variable names. Can you look at line 52 and know for sure if the compiler knows that a refers to the parameter a and not the global variable a?
I am not sure what you mean. I thought that if I had (double a, double b) that it related to the stated double a; and double b; (that was the idea). I would like to add, the addition works perfectly, its just the spontaneous equations that is causing the problem. Thanks for you help though!
It looks like you tried to use variables as functions on some lines between 31-45.

When you set a variable equal to something, it doesn't remember the formula on the right-hand side of the equals sign, it calculates the value at runtime instead. So answer3 is being set to equal answer1 - answer2 right when it is created (which will be some random garbage value). The value of answer3 does not change after that point, even if answer1 or answer2 change.

I'm not sure if I explained this well-enough.
Last edited on
You have, thank you! so I am guessing I should add the answer3 = answer1 - answer2 and the rest in the int main() AFTER ive asked them?
That's correct.
Topic archived. No new replies allowed.