LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-14-2006, 03:35 AM   #1
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Rep: Reputation: 15
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!
 
Old 02-14-2006, 03:55 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
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
 
Old 02-14-2006, 06:11 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-14-2006, 08:40 AM   #4
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
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.
 
Old 02-14-2006, 10:20 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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!
 
Old 02-15-2006, 01:38 AM   #6
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
Haha in 10 years? Well I guess I have change that immediately Thanks anyway, Ill use cmath in the future programs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Argh! SuSE 9.2 and 9.3 installer problem girlboxer5 Linux - Newbie 5 06-03-2005 11:32 AM
Argh.. GUI problem (Newb) Sherlok Slackware 13 10-14-2004 01:17 AM
Compile Program mattp Linux - Newbie 4 03-09-2004 11:13 AM
Argh, dependency problem Ztyx Debian 6 01-18-2004 02:07 PM
Compile qt program. hfawzy Linux - General 2 02-18-2003 10:25 AM


All times are GMT -5. The time now is 05:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration