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! |
Quote:
Quote:
Quote:
Quote:
|
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. |
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; } |
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!
|
Haha in 10 years? Well I guess I have change that immediately :) Thanks anyway, Ill use cmath in the future programs.
|
All times are GMT -5. The time now is 03:38 PM. |