LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   squareroot program problem, i cant compile it ARGH! (https://www.linuxquestions.org/questions/programming-9/squareroot-program-problem-i-cant-compile-it-argh-415206/)

inverted.gravity 02-14-2006 03:35 AM

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!

spooon 02-14-2006 03:55 AM

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

graemef 02-14-2006 06:11 AM

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.

inverted.gravity 02-14-2006 08:40 AM

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;
}

graemef 02-14-2006 10:20 AM

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!

inverted.gravity 02-15-2006 01:38 AM

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.