question about ifstreams

This is a discussion on question about ifstreams within the C++ Programming forums, part of the General Programming Boards category; I was wondering if someone could tell me how I could do this: I had to write this program for ...

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    question about ifstreams

    I was wondering if someone could tell me how I could do this:
    I had to write this program for an exam last week and I was never clear how to do this, so I know I missed this question but I still need to know how it is done for future reference.
    The idea is that I have a file named "TheGrades.txt"
    So in this file, there are names with grade IDs and then grades written like this:

    Maria Final 82 Midterm 65 Quiz 98 STOP -1
    Tom Final 90 Midterm 84 Quiz 46 STOP -1
    John Final 54 Midterm 54 Quiz 87 STOP -1

    I have created have 3 classes in the program.
    The idea is to use ifstream to input each line and when it gets to STOP -1, that is the end of the grades for that particular student.
    I don't need to know how to write the program, I just want to know how I can input several things of different data types (strings and doubles) through ifstream because I feel like I have missed something in class pertaining to this.
    After this we are supposed to output each student and the grades they recieved. I know I haven't written any functions in the program, but I just want to know how to use the ifstream (or maybe I use istream& as a parameter placeholder??) anyway, any advice would be great! Thank you!

    I will try to demonstrate
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Grade
    {
          public:
                 
          private:
                  string ID;
                  double score;
                  
    };
    
    class Student
    {
          public:
                 
          private:
                  string mName;
                  vector <Grade> theGrades;
                  
    };
    
    class Seminar
    {
          public:
                 
          private:
                  vector <Student> theStudents;
                                
                  
    };
    
    int main()
    {
        
        return 0;
    }
    Last edited by Amyaayaa; 05-26-2008 at 12:47 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,826
    1. Suppose you are given an istream that has "Final 82". Write a function to read from the istream into a Grade object.

    2. Suppose you are given an istream that has "Maria Final 82 Midterm 65 Quiz 98 STOP -1". Using the function you wrote in #1, write a function to read from the istream into a Student object.

    3. Suppose you are given an istream that has the same input that you provided. Using the function you wrote in #2, write a function to read from the istream into a Seminar object.

    4. Write the code to get the filename, open the file, use the function you wrote in #3, close the file, and you're done with input.
    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
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    but I don't understand how to read two different objects from a file...??
    I don't understand how I would even read Final 82 ...?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,664
    I needed some C++ practice (I've been stuck in strictly-C world for far too long) so I did this program as a quick exercise. There are a number of ways to get the data in, but this is one way.
    Code:
    bool Seminar::loadData(const string &filename)
    {
        ifstream s;
        s.open(filename.c_str(), ios::in);
        if (!s.is_open())
        {
            cerr << "Unable to open " << filename << endl;
            return false;
        }
        
        while (!s.eof())
        {
            // First, get the student's name into a string.
            string name;
            s >> name;
            if (name == "")
                continue; //blank line...skip it
    
            // OK, create the student.
            Student st(name);
    
            while (true)
            {
                // Read the grades until you find the sentinel values.
                string classname;
                double grade;
                s >> classname >> grade;
                if (classname == STOP_SIGNAL && grade == STOP_GRADE)
                    break; // Found the sentinels, so get out.
    
                // Create a grade
                Grade g(classname, grade);
    
                // Add the grade to the student's record
                st.addGrade(g);
            }
    
            // Add the student to the seminar's class list.
            theStudents.push_back(st);
        }
        
        // Close the open file.
        s.close();
        return true;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,826
    I don't understand how I would even read Final 82 ...?
    That's the easy part , and is given by rags_to_riches' example as:
    Code:
    s >> classname >> grade;
    However, note that eof() should not be used to control a loop. The reason is the same as Why it's bad to use feof() to control a loop.
    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

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,664
    Right, Laserlight. It really should be (I should've known better)
    Code:
    bool Seminar::loadData(const string &filename)
    {
        ifstream s;
        s.open(filename.c_str(), ios::in);
        if (!s.is_open())
        {
            cerr << "Unable to open " << filename << endl;
            return false;
        }
        
        string name;
        while (s >> name)
        {
            if (name == "")
                continue;
                
            Student st(name);
            while (true)
            {
                string classname;
                double grade;
                s >> classname >> grade;
                if (classname == STOP_SIGNAL && grade == STOP_GRADE)
                    break;
                Grade g(classname, grade);
                st.addGrade(g);
            }
            theStudents.push_back(st);
        }
        
        s.close();
        return true;
    }

  7. #7
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32
    Thanks guys! That was what I needed!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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