Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-14-2006, 03:35 AM
|
#1
|
|
Member
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112
Rep:
|
squareroot program problem, i cant compile it ARGH!
#include <iostream>
#include <math>
using namepspace std;
int main(void)
{
double x;
cout << "squaroot program\n";
cout << "x=";
cin >> x;
root = sqrt(x);
cout << "x^(1/2)=" << root <<;
return 0;
}
here im trying to compile
ali@pullansreturn:~/programing$ g++ -o sqrt.o sqrt.cpp
sqrt.cpp:2:16: error: math: No such file or directory
sqrt.cpp:3: error: expected nested-name-specifier before 'namepspace'
sqrt.cpp:3: error: 'namepspace' has not been declared
sqrt.cpp:3: error: expected `;' before 'std'
sqrt.cpp:3: error: expected constructor, destructor, or type conversion before ';' token
sqrt.cpp: In function 'int main()':
sqrt.cpp:7: error: 'cout' was not declared in this scope
sqrt.cpp:9: error: 'cin' was not declared in this scope
sqrt.cpp:10: error: 'root' was not declared in this scope
sqrt.cpp:10: error: 'sqrt' was not declared in this scope
sqrt.cpp:11: error: expected primary-expression before ';' token
why dont I have the math libarary? aaah! it complains to much!
|
|
|
|
02-14-2006, 03:55 AM
|
#2
|
|
Senior Member
Registered: Aug 2005
Posts: 1,755
Rep:
|
Quote:
|
Originally Posted by inverted.gravity
#include <math>
|
use "<cmath>" instead; this is how you include standard C libraries in C++
Quote:
|
Originally Posted by inverted.gravity
using namepspace std;
|
you misspelled "namespace"
Quote:
|
Originally Posted by inverted.gravity
root = sqrt(x);
|
what is root? it was never declared
Quote:
|
Originally Posted by inverted.gravity
cout << "x^(1/2)=" << root <<;
|
you can't just have nothing after the last "<<" operator; either remove it or add something there, like "endl" would be appropriate
|
|
|
|
02-14-2006, 06:11 AM
|
#3
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
Just to add to the post from spoon. The math library was first developed for the C language, to reflect this inheritance of the older language the header file is changed from <math.h> to <cmath>. This is a little confusing to start with but after time it causes less problems.
What I would suggest is that you get a good C++ book to help you, since Thinking in C++ 2nd Edition by Bruce Eckel has already received a few mentions this past week let me suggest that. It is available for a free download from the above link.
All the best,
graeme.
|
|
|
|
02-14-2006, 08:40 AM
|
#4
|
|
Member
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112
Original Poster
Rep:
|
Haha lol, I suck! Well this is what i got now, and it seems to work, and thanks for the book tip, I have downloaded it now, but will see if I know how to unzip the zip file.. hah im a noob
#include <iostream>
#include <math.h>
using namespace std;
int main(void)
{
double x;
double root;
cout << "squareroot program\n";
cout << "x=";
cin >> x;
root = sqrt(x);
cout << "x^(1/2)=(+ and -)" << root << "\n";
return 0;
}
Last edited by inverted.gravity; 02-14-2006 at 08:55 AM.
|
|
|
|
02-14-2006, 10:20 AM
|
#5
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
Oh dear maybe I confused you... In C++ you want to use the <cmath> header not the <math.h> header. Whilst they both work the math.h has been depreciated, which probably means in ten years from now your program will fail to compile!
|
|
|
|
02-15-2006, 01:38 AM
|
#6
|
|
Member
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112
Original Poster
Rep:
|
Haha in 10 years? Well I guess I have change that immediately  Thanks anyway, Ill use cmath in the future programs.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|