Thread: Problem with ifstream

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    16

    Problem with ifstream

    I made this program to read the id, grade and name of a student and to create a file with this information and than to open that same file and display the info on the file created, but my problem is this, i for example put in:

    Grade Id Name
    50 1 Tim
    75 2 John

    It writes the above to the file but when it displays on the screen it shows the second line twice like such

    Grade Id Name
    75 2 John
    75 2 John


    Here is what i wrote

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>
    
    using namespace std;
    
    int main()
    {
    int id, b;
    double grade;
    string filename, name;
    ifstream III;
    ofstream OOO;
    
    cout << "Please enter name for the output file" << endl;
    cin >> filename;
    
    OOO.open(filename.c_str());
    OOO << setw(10) << "Grade" << setw(10) << "Id" << setw(10) << "Name" << endl;
    
    cout << "Enter number os students that you are going to input information" << endl;
    cin >> b;
    
    for (int f=1; f<=b; f++)
    {
    cout << "Enter grade, id and name for student number " << f << endl;
    cin >> grade >> id >> name;
    OOO << setw(10) << grade << setw(10) << id << setw(10) << name << endl;
    }
    
    OOO.close();
    
    III.open(filename.c_str());
    
    cout << setw(10) << "Grade" << setw(10) << "Id" << setw(10) << "Name" << "\n";
    
    for (int f=1; f<=b; f++)
    {
    III >> grade >> id >> name;
    cout << setw(10) << grade << setw(10) << id << setw(10) << name << "\n" ;
    }
    
    III.close();
    
    system ("pause");
    return 0;
    }
    I cant figure out whats wrong,

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that when you read from the input stream, you only retain the last set of values entered. What you probably want to do is to open the output file stream, and every time after you read in a set of input from the user, you immediately write it to the file. Alternatively, you save the input entered into an array, and later use that array to write to file.

    Oh, and you should indent your code consistently. III and OOO are not very good names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    im sorry, i am still learning i am a noob but i am not understand really well what you are try to say,
    when i do the ofstream it writes the data correctly in order per line but when it does the ifstream it only reads the last line of the ofstream file

    and thanks for helping me

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, sorry, I misread your unindented code.

    It looks like the problem is that you print the header to the file, but did not account for it when reading back in.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    so what would i have to add or change?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends. One option is to simply not write the header to file. Another option is to read in the first line from the file and discard it.

    In addition, you should perform some error checking of your reading from file, which in turn would have alerted you as to the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    i havent learned about the error checking yet and if i delete the header from the ofstream it works but it wont display like the names on the file and that was one of the requirements

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by peste19
    i havent learned about the error checking yet and if i delete the header from the ofstream it works but it wont display like the names on the file and that was one of the requirements
    What do you mean? What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    c++

    are you referring to the code cerr or to the output on the bottom of visual studio?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I am not asking what language your current code is written in
    I am asking as to what is the code itself, so we can look at what you have updated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    Its written in c++

    i know how to do stuff i just dont know their terms lol
    Last edited by peste19; 02-25-2009 at 11:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM