473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fibonacci Printer Program won't print to file correctly

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!!!!

Feb 25 '06 #1
5 1574

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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!!!!


Just a guess, you are using "static ofstream fout("C:\\FIB.txt");", I think
if you use
just plain cout you will see the entire sequence. I think what is happening
is that you
are constructing a new ofstream as you run each templates code which will
clober what
was in the file and start a new stream. If you want to write to a file,
you will need to open
the stream once and share it with each instance of the template.

dave
Feb 25 '06 #2
"Protoman" <Pr**********@gmail.com> wrote in
news:11**********************@i40g2000cwc.googlegr oups.com:
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!!!!


Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.
Feb 25 '06 #3

Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:11**********************@i40g2000cwc.googlegr oups.com:
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!!!!


Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.


And how do I get there to only be ONE open ofstream? Make fout global?
I really want it class scope, so no one'll mess w/it.

Feb 25 '06 #4
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
:
: Andre Kostur wrote:
: > "Protoman" <Pr**********@gmail.com> wrote in
: > news:11**********************@i40g2000cwc.googlegr oups.com:
: >
....
: > > 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!!!!
: >
: > Same problem you had last time. You've got 50 different ofstreams.
All
: > of them opening the same file. Only the last change stays. Same
: > solution as the last time too.
:
: And how do I get there to only be ONE open ofstream? Make fout global?
: I really want it class scope, so no one'll mess w/it.
A clean design would be to open the ofstream in main(),
and to pass it as a parameter to LOOP<N>::f :

static void f( std::ostream output )
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Feb 25 '06 #5
Protoman wrote:
Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:11**********************@i40g2000cwc.googlegr oups.com:
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.

[snipped part of the code]

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;
}
};

[snip]

Could you help me figure out what's wrong? Thanks!!!!

Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.


And how do I get there to only be ONE open ofstream? Make fout global?
I really want it class scope, so no one'll mess w/it.


Why don't you change 'LOOP<T>::f()' to 'LOOP<T>::f(std::ostream & out)'
and pass 'out' to 'LOOP<T-1>::f(std::ostream &)'?
Feb 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2580
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
7
6861
by: Steve M | last post by:
Hello, I'm having problems sending information from a python script to a printer. I was wondering if someone might send me in the right direction. I wasn't able to find much by Google TIA...
3
3336
by: 2D Rick | last post by:
I have multiple reports that use a "specific printer" because the default printer is tied up doing other chores. This is a machine shop environment using Dells' running Win2000 and XP with...
4
3140
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
2595
by: notregister | last post by:
ref : article ID 298141 Hi i have implement this part into my program, how do i recieve information whether the printer has printed out correctly so that i can carry on to my next phase? is...
3
2055
by: Bob | last post by:
I need to create a program that is essentially a special fax sender using multi line Dialogic cards. I figure that the best way to do this so that it can be used from any app is to create someting...
8
10963
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
2
8515
by: Terry Olsen | last post by:
Can anyone give me some guidance on installing a local printer with VB 2005? I'm guessing I would probably do this with WMI, but i'm not finding anything obvious. I see how to add a networked...
1
8822
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.