Connecting Tech Pros Worldwide Forums | Help | Site Map

function use error...?

James Phillips
Guest
 
Posts: n/a
#1: Jul 22 '05
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

joey tribbiani
Guest
 
Posts: n/a
#2: Jul 22 '05

re: function use error...?


James Phillips wrote:
[color=blue]
> can anyone tell me how to correct the error in this code snippet: ?[/color]

Could you kindly tell us, which error you get?!
John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: function use error...?



"James Phillips" <jamesp@phreaker.net> wrote in message
news:17a630c5.0405182115.58bc14d8@posting.google.c om...[color=blue]
> can anyone tell me how to correct the error in this code snippet: ?
>[/color]

Incredible, didn't you think to tell anyone what the error is! Nevertheless,
comments below.
[color=blue]
>
>
> #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)){[/color]

x and y are uninitialised variables.

You seem to be trying to use the results before you've calculated them.
[color=blue]
> 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();
> }
>[/color]

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


Dave Townsend
Guest
 
Posts: n/a
#4: Jul 22 '05

re: function use error...?


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


"James Phillips" <jamesp@phreaker.net> wrote in message
news:17a630c5.0405182115.58bc14d8@posting.google.c om...[color=blue]
> 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[/color]


Closed Thread