function use error...?

J

James Phillips

can anyone tell me how to correct the error in this code snippet: ?



#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <float.h>

using namespace std;

void end();
void begin();
void compute_x(float a, float b, float c);
void compute_y(float a, float b, float c);
void error(int num);

int main(int argc, char *argv[])
{
system("CLS");

begin();

return 0;
}
void begin(void)
{
float a, b, c, x, y;

cout << "The Quadratic Formula solver.\n\n";
cout << "Enter a value for A: ";
cin >> a;
cout << "Enter a value for B: ";
cin >> b;
cout << "Enter a value for C: ";
cin >> c;

if(_isnan(x) || _isnan(y)){
cout << "The answer is unreal!\n\n";
end();
} else {
cout << "The answer is: X= " << compute_x(a, b, c) << " and
Y= " << compute_y(a, b, c) << ".\n\n";
end();
}
}

void compute_x(float a, float b, float c)
{
float x;

x = (pow(b, 2) - 4 * a * c);
x = ((-b) + sqrt(x)) / (2/a);

return x;
}

void compute_y(float a, float b, float c)
{
float y;

y = (pow(b, 2) - 4 * a * c);
y = ((-b) - sqrt(y)) / (2/a);

return y;
}


void end(void)
{
int num;

cout << "Options\n\n";
cout << "1. Do another equation.\n";
cout << "2. Return to Mathematics Wizard Main Menu\n";
cout << "3. Exit\n";
cout << "\nEnter the number of your choice: ";
cin >> num;

if(num == 1) { begin(); }
else if(num == 2) { cout << "Function not yet implemented!"; }
else if(num == 3) { exit(0); }
else { error(num); }
}

void error(int num)
{
cout << "You idiot! " << num << " isn't a valid response!\n\n";
end();
}






Thanks
 
J

John Harrison

James Phillips said:
can anyone tell me how to correct the error in this code snippet: ?

Incredible, didn't you think to tell anyone what the error is! Nevertheless,
comments below.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <float.h>

using namespace std;

void end();
void begin();
void compute_x(float a, float b, float c);
void compute_y(float a, float b, float c);
void error(int num);

int main(int argc, char *argv[])
{
system("CLS");

begin();

return 0;
}
void begin(void)
{
float a, b, c, x, y;

cout << "The Quadratic Formula solver.\n\n";
cout << "Enter a value for A: ";
cin >> a;
cout << "Enter a value for B: ";
cin >> b;
cout << "Enter a value for C: ";
cin >> c;

if(_isnan(x) || _isnan(y)){

x and y are uninitialised variables.

You seem to be trying to use the results before you've calculated them.
cout << "The answer is unreal!\n\n";
end();
} else {
cout << "The answer is: X= " << compute_x(a, b, c) << " and
Y= " << compute_y(a, b, c) << ".\n\n";
end();
}
}

void compute_x(float a, float b, float c)
{
float x;

x = (pow(b, 2) - 4 * a * c);
x = ((-b) + sqrt(x)) / (2/a);

return x;
}

void compute_y(float a, float b, float c)
{
float y;

y = (pow(b, 2) - 4 * a * c);
y = ((-b) - sqrt(y)) / (2/a);

return y;
}


void end(void)
{
int num;

cout << "Options\n\n";
cout << "1. Do another equation.\n";
cout << "2. Return to Mathematics Wizard Main Menu\n";
cout << "3. Exit\n";
cout << "\nEnter the number of your choice: ";
cin >> num;

if(num == 1) { begin(); }
else if(num == 2) { cout << "Function not yet implemented!"; }
else if(num == 3) { exit(0); }
else { error(num); }
}

void error(int num)
{
cout << "You idiot! " << num << " isn't a valid response!\n\n";
end();
}

You should use a loop, instead of using recursion to loop. You should learn
about return values. Here's a quick sketch

int main()
{
float a, b, c;
float x, y;
bool quit = false;
do
{
get_values(a, b, c);
x = compute_x(a, b, c);
y = compute_y(a, b, c);
if(_isnan(x) || _isnan(y))
{
...
}
else
{
...
}
switch (get_option())
{
case 1:
...
break;
case 2:
...
break;
case 3:
quit = true;
break;
}
}
while (!quit);
}

void get_values(float& a, float& b, float& c)
{
...
}

int get_option()
{
...
}

Notice how I'm using the return values of compute_x and compute_y to assign
to variables x and y, and then checking x and y for NaN.

Also note the mechanism use to quit the loop, get_option returns the option
chosen, and if its 3 a variable is set so that the do while loop in main is
quit.

You obviously need to get familiar with the idea of storing a value in a
variables and returning a value from a function instead of trying to use the
value immediately, as you do most of the time in your code.

john
 
D

Dave Townsend

It seems to me that the problem is the compute_x and compute_y functions,
these are "void", so the << operator has no clue how to output them, should
these guys return a float or int instead ?
dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top