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 3 1207
James Phillips wrote: can anyone tell me how to correct the error in this code snippet: ?
Could you kindly tell us, which error you get?!
"James Phillips" <ja****@phreaker.net> wrote in message
news:17**************************@posting.google.c om... 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
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" <ja****@phreaker.net> wrote in message
news:17**************************@posting.google.c om... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel Hansen |
last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now...
Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
|
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: John Collyer |
last post by:
Hi,
In assembly language you can use a lookup table to call functions.
1. Lookup function address in table
2. Call the function
Like:
CALL FUNCTION
|
by: Piotre Ugrumov |
last post by:
I have declared some function virtual, but when I call this function I have
errors.
In a class Simulator I have defined this:
Nave *navi;
The virtual function is the function modifica().
I call...
|
by: Chuck Martin |
last post by:
I am having a most frustrating problem that references, web searches,
and other resources are no help so far in solving. Basically, I'm trying
to design a pop-up window to be called with a funciton...
|
by: jchao123 |
last post by:
Dear All,
I have an MDB file (Access 2000/XP) which contains generic routines I
use in various apps (eg, API calls, File access classes etc).
I have compiled into an MDE file which I reference...
|
by: atv |
last post by:
Whatis the proper way to handle errors from function calls?
For example, i normally have a main function, with calls to mine
or c functions. Should i check for errors in the functions called...
|
by: Mr Newbie |
last post by:
I am often in the situation where I want to act on the result of a function,
but a simple boolean is not enough. For example, I may have a function
called
isAuthorised ( User, Action ) as ?????...
|
by: f rom |
last post by:
----- Forwarded Message ----
From: Josiah Carlson <jcarlson@uci.edu>
To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org
Sent: Monday, December 4, 2006 10:03:28 PM
Subject: Re: ...
|
by: lasmith329 |
last post by:
I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |