i/o stream problems

This is a discussion on i/o stream problems within the C++ Programming forums, part of the General Programming Boards category; Hi I'm writing a program that will ask the user for a filename and the number of Fibbonacci numbers they ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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.
    Last edited by hk_mp5kpdw; 11-11-2004 at 04:31 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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.
    Last edited by xviddivxoggmp3; 11-11-2004 at 05:25 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  2. I/O interface
    By bean66 in forum C Programming
    Replies: 0
    Last Post: 08-28-2007, 12:53 PM
  3. I/O stream files
    By malooch in forum C Programming
    Replies: 4
    Last Post: 12-12-2006, 08:58 AM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 03:46 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21