C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 6th, 2005, 11:03 AM
earache earache is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 5 earache User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 50 sec
Reputation Power: 0
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 ;)

Reply With Quote
  #2  
Old July 6th, 2005, 12:35 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
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.

Reply With Quote
  #3  
Old July 6th, 2005, 03:01 PM
earache earache is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 5 earache User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by drayel
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.


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>?

Reply With Quote
  #4  
Old July 6th, 2005, 03:11 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
Quote:
Originally Posted by earache
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>?


I think math.h is just an older math library. What is that "gremlin" zapper all about?

Reply With Quote
  #5  
Old July 6th, 2005, 03:26 PM
anki607 anki607 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 anki607 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by drayel
I think math.h is just an older math library. What is that "gremlin" zapper all about?
well, <math.h> is a standard math library in compiler which contains all the math functions. it is also called the header file in which all the classes related to math functions are kept.

Reply With Quote
  #6  
Old July 6th, 2005, 03:41 PM
earache earache is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 5 earache User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by anki607
well, <math.h> is a standard math library in compiler which contains all the math functions. it is also called the header file in which all the classes related to math functions are kept.


So how does <math.h> differ from <cmath>?

Reply With Quote
  #7  
Old July 6th, 2005, 03:47 PM
earache earache is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 5 earache User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by drayel
What is that "gremlin" zapper all about?

"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.

Reply With Quote
  #8  
Old July 6th, 2005, 03:55 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Quote:
Originally Posted by earache
So how does <math.h> differ from <cmath>?
math.h is the C version, cmath is the C++ version.... in reality, they do the same thing for their corresponding languages, but when using iostream it's almost always best to use corresponding C++ headers because of compatability issues....

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.



Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Problem with programming assignment


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway