-
i/o stream problems
Hi
I'm writing a program that will ask the user for a filename and the number of Fibbonacci numbers they would like to see, assign that name to a file, output those numbers to that file then display the file. My problem is that the numbers aren't being written to the file and therefore nothing is displayed. Could someone please point me in the direction where my problem is? Thank you.
Code:
#include <iostream>
#include <fstream>
using namespace std;
//function prototypes
void fibon(ofstream &fibFile, int num_of_fib);
int main ()
{
int num_fibonacci, validNum, validFile, display;
char filename[30];
ofstream outFile;
/
do
{
validFile=0;
cout << "\nEnter the name that you want the file to called"
<< " (up to thirty charaters):\n";
cin >> filename;
outFile.open("c:\\filename.dat");//take some stuff out
if (outFile.fail())
{
cout << "\nThe file did not open. Please input another name.\n";
validFile = 1;
}
} while (validFile == 1);
do
{
validNum=0;
cout << "\nHow many Fibonacci Numbers do you want? (You can have"
<< " between 2 and 47 numbers)\n";
cin >> num_fibonacci;
if((num_fibonacci < 2) || (num_fibonacci >47))
{
cout << "\nYou must have at least 2 Fibonacci Numbers and "
<< "no more than 47 numbers";
validNum=1;
}
}while (validNum==1);
void fibon(ofstream &outFile, int num_fibonacci);
outFile >> display;
cout << "Here are your "<< num_fibonacci <<" Fibonacci Numbers:\n";
<< display << endl:
outFile.close();
return 0;
}
////////////////////////////////////////////////////fibon function
void fibon(ofstream &fibFile, int num_of_fib)
{
int first = 0, second = 1, next;
if (num_of_fib == 2)
{
fibFile << first << second;
}
else
{
fibFile << endl << first << " "<< second;
num_of_fib = num_of_fib -2;
while(num_of_fib > 0)
{
next = first + second;
fibFile <<" "<< next;
second = first;
next = second;
num_of_fib--;
}
}
}
-
Is that '/' a typo:
Code:
ofstream outFile;
/
No matter what, your user won't get to choose the name for their file. It will always be "C:\filename.dat":
Code:
cout << "\nEnter the name that you want the file to called"
<< " (up to thirty charaters):\n";
cin >> filename;
outFile.open("c:\\filename.dat");//take some stuff out
On to your problem, you are calling the function wrong:
Code:
void fibon(ofstream &outFile, int num_fibonacci);
This is not what you want, try this:
Code:
fibon(outFile, num_fibonacci);
Also, your not displaying your file data properly, what is this:
Code:
outFile >> display;
cout << "Here are your "<< num_fibonacci <<" Fibonacci Numbers:\n";
<< display << endl:
It is nonsense.
-
you might want to declare filename as a string instead of a char array.
concatination will be easier with strings, which will be needed to append a path to the file name. see the following link.
http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
Code:
#include<string>
...
string path = "c:\somefile\path\";
string filename;
cout << "\nEnter the name that you want the file to called"
<< " (up to thirty charaters):\n";
cin >> filename;
path+=filename;
outFile.open(path);//take some stuff out
I think this works, but i do not have a compiler to check.
i hope this helps out a little.