CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #1
    Join Date
    Apr 2010
    Location
    Ohio
    Posts
    27

    Post Little help with fstream outFile

    Hey all,

    Have a slight problem I can't seem to figure out. First, I am a beginner in C++ and this is an assignment due 4/2/2010. Forgive me if the solution is simple...it's just evading me at the moment.

    OK, my problem is in the FSTREAM portion of my code (near the bottom). I am trying to read 5 numbers from an inFile. I got this working, however, the 2nd part is writing the stored numbers from the inFile into a SECOND file....which I named "outFile_Reverse.open" is my dilemma.

    It does not read the correct numbers. I suspect it's reading numbers from the portion of the code titled "ARRAY OF INT" because I pasted the "for loop" into the FSTREAM logic. It displays "5 4 3 2 1" So, how can I make inFile_1 read the stored numbers_1 text file and display them in reversed order?

    Here is my code:

    Code:
    
    
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	
    	int number[5];
    	int input;
    	int counter;
    	string words[6];	
    	const string end = "end of array";
    
    	cout <<"\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-ARRAY OF INT-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
    	cout <<"\n\nThis portion of the program will ask you to type in 5 numbers." <<endl;
    	cout <<"It will then display them back to you in reverse order." <<endl;
    	cout <<"\nPlease enter 5 positive numbers with a space between each: "; 
    	
    		for (counter = 0; counter < 5; counter++)
    		{
    			cin >> number[counter];			
    		}
    	cout <<endl;
    		
        cout <<"\nThe numbers in reverse order are ===>  ";
    		for (counter = 4; counter >= 0; counter--)
    		{
    			cout <<number[counter] << " ";	
    		}
    
    cout <<"\n\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-ARRAY OF STRING=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
    cout <<"\n\nThis portion of the program will ask you to type in 5 words." <<endl;
    cout <<"It will then use a do while loop and display the 1st and 3rd letter of each" <<endl;
    cout <<"word using the substring function." <<endl;
    
    
    cout <<"\nPlease enter 5 words:\n"<<endl;
    
    	for (counter = 0; counter < 5; counter++)
    	{
    		cin >> words[counter];
    	}
    counter = 0;
    
    cout <<"\n\nThe 1st and 3rd letter of each word you entered are:\n" <<endl;
    do
    {
    	if(words[counter].size() >= 3)
    	{
    		cout << words[counter][0] << words[counter][2] << "\n\n";
    	}
    	else
    	{
    		cout <<"\n\n\tUH-OH ===> One or more words you entered have less than 3 characters." <<endl;
    		cout <<"\tOnly words containing 3 OR MORE characters will display a result.)" <<endl;
    	}
    	counter++;
    }
    while (counter < 5);
    
    cout <<"\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
    
    cout <<"\nThis portion of the program will:\n\n" <<endl;
    cout <<"1. Read 5 numbers FROM a text file" <<endl;
    cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
    cout <<"3. Read 5 words in from a THIRD file" <<endl;
    cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
    
        ifstream inFile_1;               	// File we are reading the numbers from
        ifstream inFile_2;                  // File we are reading the words from
        ofstream outFile_Reverse;           // File to use for output
        string numbers_1 ;                  // Name of number txt file
        string words_1;                     // Name of words txt fileLi
        int num1, num2, num3, num4, num5;   // Declaring variables to read numbers from stored file
        string w1, w2, w3, w4 ,w5;          // Declaring variables to read words from stored file									   
    	
    	inFile_1.open("numbers_1.txt");                // File stream variable to open numbers_1.txt file to read
    	inFile_2.open("words_1.txt");                  // File stream varibale to open words_1.txt file to read
    	outFile_Reverse.open("outFile_Reverse.txt");   // File stream variable to display reversed numbers into this outFile.
    
    
       /***********************************************************************************************
       	 outData file to display words_1 is not needed; displaying directly to console from inFile_2.
    	***********************************************************************************************/
    
    	inFile_1 >> numbers_1;
    
    	outFile_Reverse << numbers_1; 
    		for (counter = 4; counter >= 0; counter--)
    		{
    			cout <<number[counter] << " ";	
    		}
    
    //	inFile_2 >> words;
    
    //	cout <<"The words stored in the words.txt file are: "<<words<<;
    
    	inFile_1.close();         // Closes numbers_1.txt file
    	inFile_2.close();         // Closes words_1.txt file
    	outFile_Reverse.close();  // Closes outfile
    		
    cin.get();
    cin.get();
    return 0;
    }
    Last edited by SoloXX; April 30th, 2010 at 02:36 PM. Reason: code format

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured