# calculating square of some numbers

This is a discussion on calculating square of some numbers within the C++ Programming forums, part of the General Programming Boards category; Hello , As a exercise I must calculate the square of the number 1 till 100. I have this so ...

1. ## calculating square of some numbers

Hello ,

As a exercise I must calculate the square of the number 1 till 100.

I have this so far:

main.cpp
Code:
```#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "square.h"

using namespace std;

int main()
{
vector<int> nummer;
int teller ;

// filling the vector with the numbers 1 till 100
for (teller=0; teller < 100; teller++ )
{
nummer.push_back(teller);
}

// compute and write the square
for (teller=0 ; teller < 100 ; teller++)
try {
double uitkomst  = wortel (nummer[teller]); // function where the square is computed.
streamsize prec = cout.precision();
cout << setprecision(3) << uitkomst
<< setprecision(prec);
} catch (domain_error e) {
cout << e.what();
}
cout << endl;
}
return 0;
}```
And square.h looks like this :
Code:
```#include <stdexcept>
#include <vector>
#include [itex]

using namespace std;

// compute the square
double wortel (int nummer)
{
uitkomst = sqrt (nummer) ;
return uitkomst
}```
But when compiling I get this error message :
C:\Users\wobben\Desktop\borland-source\chapter04\wortel\square.h|10|error: 'square' was not declared in this scope|

Im confused because I only use sqaure as name of the header file.

So where did I go wrong ?

Roelof

2. The first error that I get from the header: "math - no such file or directory".

The header is called "cmath", and it is the only standard header that you need to include for "square.h" as you have shown.

3. You don't really need to bother with vectors for this exercise. You can simply use a loop:

Code:
```for(int i = 1; i <=100; ++i) {
cout << i*i << endl;
}```
That will output all squares without storing the data.

4. Oke,

But I have to use setw for the output.
Therefore I use vectors.

Roelof

5. Originally Posted by roelof
Oke,

But I have to use setw for the output.
Therefore I use vectors.

Roelof
You can also use setw when using cout. You will probably want it left justified so I think it would be cout << setw(n) << left << i*i << endl;

6. Oke,

But then why this exercise in a chapter full of vectors and structs.
Then your solution does not seem logical for me.

Roelof

7. Why is difficult to say. Suffice to say that they did it for a reason. But you don't have to do it to complete the exercise.
Also, note that you shouldn't use "using namespace std" inside headers.

8. If you wanted to compute the sum of a simple series, like 1 + 2 + 3 + 4 + 5, there is no need to store those values in a vector first.

Code:
```int sum = 0;
for (int i = 1; i <= 5; ++i) {
sum += i;
}```
(Of course, this could be done mathematically without any loop at all.)

Also, I'm not at all sure, whether there is anything in the try block that can actually throw a domain_error. (It would make sense to me that the sqrt function, coming from a C library, doesn't use methods of error handling that are specific to C++ only.)

Your wortel() function has other errors. It is using an undeclared variable (uitkomst) and missing a semicolon.

The main code has mismatching braces.

9. Oke,

I think that the meaning of this exercise is a practise of using header files.
I solved most of the errors , but I still have one.

code main.cpp
Code:
```#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "square.h"

using namespace std;

int main()
{
vector<int> nummer;
int teller ;

// filling the vector with the numbers 1 till 100
for (teller=0; teller < 100; teller++ )
{
nummer.push_back(teller);
}

// compute and write the square
for (teller=0 ; teller < 100 ; teller++)
{
double uitkomst  = wortel (nummer[teller]); // function where the square is computed.
streamsize prec = cout.precision();
cout << setprecision(3) << uitkomst
<< setprecision(prec);
}
cout << endl;
return 0;
}```
code square.h
Code:
```#ifndef GUARD_square_h
#define GUARD_square_h

#include <vector>

int wortel(int);

#endif```
and as last square.cpp
Code:
```#include <vector>
#include "square.h"

int wortel (int nummer)
{
return sqrt(nummer);
}```
The only error I get now is :
C:\Users\wobben\Desktop\borland-source\chapter04\wortel\main.o:main.cpp|| undefined reference to `wortel(int)'|

What wrong here ?

Roelof

10. First,
Code:
`int wortel(int);`
should be
Code:
`int wortel(int number);`
Secondly, it seems you aren't compiling properly. You must compile all the source files, not just main.cpp.

11. Oke,

I changed that in square.h and if I now compiling square.cpp I get this message :
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|

on main.cpp I get still the old error message

Roelof

12. How are you compiling?

13. Hello,

Im first try to compile square.cpp bij choosing Build >> Choose current file.
After that I try to compile main.cpp on the same way in Code::blocks.

Roelof

14. If you're using C::B, you should make sure you have a project with both files in it. Then you compile the project, and not the files separately.
Also make sure that the project is of a console type, not a Windows project.

15. Oke,

That's did the trick.
Still another problem.
I have to use set(w) to make 2 collums.
So I use this :
Code:
```cout << left << setw(10) << teller << right << setw(10) << setprecision(3) << uitkomst
<< setprecision(prec) << endl ;```
But no 2 collums.

Roelof

Page 1 of 2 12 Last