P
Protoman
Here's a program I wrote that calcs the fibonacci numbers and writes
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.
Code:
//FIB.hpp
#pragma once
#include <fstream>
using namespace std;
template<long long N>
class FIB
{
public:
static const long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};
template<>
class FIB<1>
{
public:
static const long long RET=1;
};
template<>
class FIB<0>
{
public:
static const long long RET=0;
};
template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};
template<>
class LOOP<0>
{
public:
static void f(){}
};
//main.cpp
#include <iostream>
#include <cstdlib>
#include "FIB.hpp"
using namespace std;
int main()
{
LOOP<50>::f();
system("PAUSE");
return EXIT_SUCCESS;
}
Could you help me figure out what's wrong? Thanks!!!!
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.
Code:
//FIB.hpp
#pragma once
#include <fstream>
using namespace std;
template<long long N>
class FIB
{
public:
static const long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};
template<>
class FIB<1>
{
public:
static const long long RET=1;
};
template<>
class FIB<0>
{
public:
static const long long RET=0;
};
template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};
template<>
class LOOP<0>
{
public:
static void f(){}
};
//main.cpp
#include <iostream>
#include <cstdlib>
#include "FIB.hpp"
using namespace std;
int main()
{
LOOP<50>::f();
system("PAUSE");
return EXIT_SUCCESS;
}
Could you help me figure out what's wrong? Thanks!!!!