Declare const with no type

I'm trying to follow an example from my book, and has made the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

//  1   Initiera summan till 0 och första termen till 1
    double sum(0), next_term(1);

//      Initiera epsilon med värdet 1 x 10^-6
    const double epsilon(1e-6);

//      Räknare k som håller reda på termens nummer
    int k(1);

//      Varannan negativ, varannan positiv
    double sign(1);

//      Om vi vill ändra decimalantalet behöver vi bara ändra count_dec och epsilon
    const count_dec = 5;

//  2   Om absolutbeloppet av nästa term >= 0.000001 (1 x 10^-6) så utför följande två steg:
    while(fabs(next_term) >= epsilon)
    {
//      2.1     Lägg nästa term till summan
        sum += next_term;

//      2.2     Beräkna en ny nästa term
        k++;

//      Växla sign mellan +1 och -1 för varje ny term
        sign = -sign;

//      och beräkna nästa term
        next_term = sign / (k*k);

    }
//  3   Skriv ut summan
    cout << "The sum is " << setprecision(count_dec) << sum << endl;


    return 0;
}


This gives me the error
ISO C++ forbids declaration of 'count_dec' with no type|


But in my book it says clearly const count_dec = 5;

What am I doing wrong?


PS. I use GNU GCC Compiler
Looks like the book is wrong.

On line 42 is setprecision(count_dec). Setprecision takes an integer as the parameter, so I'd suggest you put const int count_dec = 5;


Early when a variable has no type specifier the int was assumed. Now this convention is wrong according to C/C++ standards.
Topic archived. No new replies allowed.