Thread: Input/Output Text File Manipulation not working!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    Input/Output Text File Manipulation not working!

    Hi guys; I was wondering if you can tell me why my output screen for this program does not want to stay open. It only opens for a split of a second and it's gone. The program is supposed to take numbers from a input file and display and save the manipulation in the output file. Here is the program. Thanks guys.....
    Code:
     .#include<iostream>
    #include<fstream>
    #include<iomanip>
    usingnamespace std;
     
    int main()
    {
    //Declares Necessary Variables
    ifstream infile;
    ofstream outfile;
    int inputNumber=0;
    int highestNum=-999999;
    int lowestNum=999999;
    float sum=0;
    float average=0;
    int currentlineNum=1;
    //Opens The Input File
        infile.open("Input_File.txt", ios::in);
    if (!infile)
    {
            cout << "Sorry, Can Not Open The File" << endl;
            exit (1);
        }
    //Opens The Output File
        outfile.open("Output_file.txt", ios::out);
    do
        {
    //Loops Thru File, Until We Get To The Last Number
            cout << "THE INPUT DATA FOR LINE #" << currentlineNum << " is: ";
            outfile << "THE OUTPUT DATA FOR LINE #" << currentlineNum << " is: ";
    for (int counter=1; counter <= 7; counter++)
            {
                infile >> inputNumber;
                sum += inputNumber;//Calculates The Total Sum of #'s For Each Line
    //Which Number Is Highest/Lowest
    if (inputNumber > highestNum)
                {
                    highestNum = inputNumber;
                }
    if (inputNumber < lowestNum)
                {
                    lowestNum = inputNumber;
                }
    //Displays Current Number To The Output Screen
                cout << inputNumber << "\t";
                outfile << inputNumber << "\t";
            }
    //Finds The Total Average For Each Line
            average = sum / 7;
            cout << endl;
    //Displays Data To The Output Screen
            cout << "\nTHE HIGHEST NUMBER IS: " << highestNum;
            cout << "\nTHE LOWEST NUMBER IS: " << lowestNum;
            cout << "\nTHE TOTAL OF THE NUMBERS IS: " << sum;
            cout << "\nTHE AVERAGE OF THE NUMBERS IS: " << average;
    //Outfile Section....Saves Data To The Output File
            outfile << endl;
            outfile << "\nTHE HIGHEST NUMBER IS: " << highestNum;
            outfile << "\nTHE LOWEST NUMBER IS: " << lowestNum;
            outfile << "\nTHE TOTAL OF THE NUMBERS IS: " << sum;
            outfile << "\nTHE AVERAGE OF THE NUMBERS IS: " << average << endl << endl;
    //Resets Variables Back To Default Values
            highestNum=-999999;
    lowestNum=999999;
    sum=0;
    average=0;
    currentlineNum++;
            cout << endl << endl;   //Places Data On New Line
        }
    while (!infile.eof()); //Loop Stops When Reaches The End Of File
        cout << endl << endl << "\tWE HAVE REACHED THE END OF THE FILE." << endl;
        outfile << endl << endl << "\tWE HAVE REACHED THE END OF THE FILE." << endl << endl;
    //Closes Input And Output Files
        infile.close();
        outfile.close();
        system ("pause");
    return 0;
    }


  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    what is your environment and toolchain? what is the exit status of the program
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Frank1234 View Post
    Code:
    while (!infile.eof());
    this is the wrong way to detect the end of a file. eof() will only trigger after you reach the end of the file and try to read again. the better way is to check the return value of operator>>.

    so instead of
    Code:
    do
    {
    } while (!infile.eof());
    it would be better to do something more like this
    Code:
    while (infile >> inputNumber)
    {
    }
    this may be the source of your trouble.

    also, when pasting code from visual studio, you should right click and select "paste as plain text" if you have that option. otherwise your code gets polluted with color, font, and size tags, and makes it very difficult to manage when replying.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Hello Elkvis; I tried what you said and switched the do-while loop to while loop and nothing changed. I still get the output screen for a milli-second and it vanishes. Can you think of some thing else? Thanks.
    Last edited by Frank1234; 03-25-2013 at 06:46 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    cin.get();

    add this at the end

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by pratras View Post
    cin.get();

    add this at the end
    certainly the better solution, but the OP already has this:
    Code:
    system ("pause");
    which does exactly the same thing.

    I suspect an exception is being thrown, and you're simply running the program instead of starting it in the debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple output/input program not working?
    By tmac619619 in forum C Programming
    Replies: 3
    Last Post: 10-07-2012, 10:31 PM
  2. Having an output issue working with input file, golf game.
    By douglas481 in forum C Programming
    Replies: 10
    Last Post: 03-29-2012, 05:43 PM
  3. Speech input to text output
    By Joealem in forum C Programming
    Replies: 2
    Last Post: 12-27-2010, 12:16 AM
  4. File and text manipulation
    By A.C Milan in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:10 PM
  5. Text file manipulation problem
    By ferretman in forum C Programming
    Replies: 3
    Last Post: 12-20-2001, 02:40 AM