/*I'm learning C++ through the use of Stroustrup's Programming: Principles and Practice Using C++. I've ran into a few problems that have me wondering whether typos are to blame or if my xcode libraries need to be updated or if some other issue is to blame.
There are two similar sets of code below. The first produces the error: "'square' was not declared in this scope" on line 9. The second produces no problems and executes just fine. What gives?
If an update is what's needed, how would I go about doing that? Thank you.*/
#include <iostream>
#include<cmath>
using namespace std;
int main ()
{
for (int i=0; i<100; ++i)
{
cout <<i<<" "<<square(i)<<endl; //'square' was not declared in this scope
}
return 0;
}
/*Versus: This code which elicits a (correct) result:*/
#include <iostream>
#include<cmath>
using namespace std;
int main ()
{
for (int i=0; i<100; ++i)
{
cout <<i<<" "<<i*i<<endl;
}
return 0;
}
When the compiler says an identifier isn't within scope, it means it doesn't know what the identifier is; it's never seen it before; you've never told it what it is.
To fix this, you'll need to declare square(). However, it seems you want the function to compute the square root. For that, you'll need sqrt().