Hey I just got this book Accelerated c++ and it seems pretty good, but I'm having one problem in chapter 4. When I compile my grading program in chapter 4. I can't get the final version to work. I believe I'm supposed to enter a students name followed by grades, but I don't know how to create a condition that makes the first while loop stop, so I can show I'm done entering names and grades. I included the files that I'm working with. If you have the book they're the exact same.

Recommended Answers

All 8 Replies

That's a lot of stuff to look at. Can you boil it down to something small enough to post in code tags?

int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;  // the length of the longest name
    
    // read and store all the students' data
    // Invariant:  students contains all the student records read so far
    //             maxlen contains the length of the longest name in students
    while (read(cin, record)) {
        // find length of longest name
        maxlen = max(maxlen, record.name.size());
        students.push_back(record);
    }
    
    // alphabetize the student records
    sort(students.begin(), students.end(), compare);
    
    // write the names and grades
    for (vector<Student_info>::size_type i = 0;
         i != students.size(); ++i) {
         
         // write the name, padded on the right to maxlen +1 characters
         cout << students[i].name
              << string(maxlen + 1 - students[i].name.size(), ' ');
         
         // compute and write the grade
         try {
             double final_grade = grade(students[i]);
             streamsize prec = cout.precision();
             cout << setprecision(3) << final_grade
                  << setprecision(prec);
         } catch (domain_error e) {
             cout << e.what();
         }
         cout << endl;

Here are the functions for the read(cin, record)

istream& read(istream& is, Student_info& s)
{
         // read and store the student's name and midterm and final exam grades
         is >> s.name >> s.midterm >> s.final;
         
         read_hw(is, s.homework);  // read and store all the student's homework grades
         return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
         if (in) {
                 // get rid of previous content
                 hw.clear();
                 
                 // read homework grades
                 double x;
                 while (in >> x)
                     hw.push_back(x);
                 
                 // clear the stream so that input will work for the next student
                 in.clear();
         }
         return in;
}

If you need anything else please let me know.

The read() function returns the stream, so you should be able to signal EOF to stop the first loop. On Linux it's a Ctrl+D key combination and on Windows it's Ctrl+Z.

commented: Quick help +1

Or pipe the input from a file.

How about using a sentinnel/flag? A little bit of a hassle to enter an additional keystroke, but it is completely portable, offers complete control without using magical combinations, etc.

bool moreInput = true;
char temp;
while(moreInput)
{
   //get input
   cout << "enter y to keep going, anything else to stop" << '\n';
   cin >> temp;
   if(temp == 'y')
     moreInput = false;
}

Alright when I push ctrl+z it displays this ^Z. Then I hit enter a few times and decided to just type in done or something random. Then it displayed the names and grades. Any ideas?

oh nvm I see I had too end two loops. Alright whew that works out. Thank you all for all your help.

bool moreInput = true;
char temp;
while(moreInput)
{
   //get input
   cout << "enter y to keep going, anything else to stop" << '\n';
   cin >> temp;
   if(temp == 'y')
     moreInput = false;
}

I always would write the following line: if(temp == 'y') as if( [B]tolower(temp)[/B] == 'y') because it also allows the user to enter an uppercase 'Y' :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.