| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem with programming assignment
Hi,
I'm taking an online course in C++ programming, and of course, it's Windows-centric, and the instructor doesn't know from Xcode. I'm stumped on the following program and was wondering if anyone could help point out my mistake. Code:
#include <iostream>
#include <cmath>
using namespace std;
double num1, num2, num3; //Inputs - Coefficients a, b, c
double disc; // Output - discriminant
void instruction (); //function instruction prototype
double getDiscriminant (double, double, double); //function getDiscriminant prototype
void displayEquation (double, double, double); //function displayEquation prototype
int main ()//main function:
{
instruction (); // call instruction function
cin >> num1, num2, num3; // get input data
getDiscriminant (num1, num2, num3); //call function getDiscriminant
if (disc < 0)
{
cout << "There are no real solutions to the equation: " << endl;
displayEquation (num1, num2, num3); //call function displayEquation
}
else if (disc = 0)
{
double root1, root2;
root1 = (-num2 + sqrt(disc)) / (2 * num1);
root2 = (-num2 - sqrt(disc)) / (2 * num1);
cout << "There are two real solutions, " << root1 << " and " << root2 << ", to the equation: " << endl;
displayEquation (num1, num2, num3); // call function displayEquation
}
else
{
double root1;
root1 = (-num2 + sqrt(disc)) / (2 * num1);
cout << "There is one real solution, " << root1 << ", to the equation: " << endl;
displayEquation (num1, num2, num3);// call function displayEquation
}
return 0; //end of main function
}
//instruction function:
void instruction ()// Display instructions to user
{
cout << "Please enter coefficients a, b, and c of a quadratic equation." << endl;
cout << "The computer will calculate whether there are any solutions to the equation." << endl;
} //end instruction function
double getDiscriminant (double a, double b, double c)// function definition
{
disc = pow(b, 2) – 4 * a * c;
return disc;
} //end getDiscriminant function
void displayEquation (double a, double b, double c)//function definition
{
cout << a << "x^2 + " << b << "x + " << c << endl;
} // end displayEquation function
____________________________________________ I get the following errors on build: main.cpp:58: error: parse error before numeric constant main.cpp:58: error: stray '\223' in program main.cpp:58: error: stray '\200' in program main.cpp:58: error: stray '\342' in program which I don't understand - I've never defined a numeric constant. TIA Last edited by B-Con : July 6th, 2005 at 03:52 PM. Reason: don't forget your [code] tags ;) |
|
#2
|
|||
|
|||
|
Hmmm, it works for me. I changed the math library to #include <math.h>, although I did move the disc equation around first, compiled, and then put it back to what you have. It must be a compiler issue because your code is fine.
|
|
#3
|
|||
|
|||
|
Quote:
Thanks, Someone else suggested grabbing TextWrangler and doing a "Zap Gremlins" on the code - and that worked! Of course, I still had some other errors, but at least that got it to build. Question - what is the <math.h> directive? How is that different from <cmath>? |
|
#4
|
|||
|
|||
|
Quote:
I think math.h is just an older math library. What is that "gremlin" zapper all about? |
|
#5
|
|||
|
|||
|
Quote:
|
|
#6
|
|||
|
|||
|
Quote:
So how does <math.h> differ from <cmath>? |
|
#7
|
|||
|
|||
|
Quote:
"Zap Gremlins" command in TextWrangler allows you to remove or replace various non-printing characters, often known as "gremlins". Use this command when you have a file that may contain extreaneous control characters, or any non-ASCII characters, which you may wish to identify or remove. |
|
#8
|
||||
|
||||
|
Quote:
however, in this case, it appears that you were better served using a depreciated header....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Problem with programming assignment |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|