4-6. Rewrite the Student_info structure to calculate the grades immediately and store only the final grade.

What I proceed:-
I doubt that data structure can 'hold' function or expression, so I give a look on net to check out, it seems, the tutorial(on net) just show that structure used for declaring variable ONLY,

So, I try declare a variable using expression(double a = b + c), it seems to be not working too.[error : 'Student_info::final' cannot appear in a constant-expression.]

so, how can I told a function "to calculate the grades immediately and store only the final grade" ? are there any 'secret' way?

btw, I included the attachment in compressed file so that you can see the code.

thnx 4 read, hope anyone can give a hand..

>>Rewrite the Student_info structure to calculate the grades immediately and store only the final grade.

Seems like you want to do this in the constructor. Here is an example.

#include <iostream>

struct Sum{
 int total;
 Sum() : total(0){}
 Sum(int max): total( _calculate(max) ) { /*sum calculated already */}
 int _calculate(int m){
    return (m*m + m) / 2;
 }
};

int main(){
 using namespace std;
 Sum s(100);
 cout << s.total << endl;
 return 0;
}

I hope I can do it with something that I already learned, but the real problem is:

1. barely have any base knowledge of what is data structure other than
- create a user-defined type
- to store multiple data in a variable(getting multiple value from "return" functionality)
- function is enabled to be put into it(where I thought it wasn't)

2. thnx to my lack of knowledge of struct, when I'm trying to put function and using the declared variable inside the struct itself, error is un-avoidable(decide to reply for this)

let's see:-

In code below, I'm editing the line 57 and 58(originals you can see in the attachment)

// main.cpp
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;

int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;   // the lenght 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);

    // seperating result
    cout << endl << endl << endl;

    // 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 {
            record.counter = i;
            double final_grade = record.overall;
            streamsize prec = cout.precision();
            cout << setprecision(3) << final_grade <<
                    setprecision(prec);
        } catch (domain_error e) {
            cout << e.what();
        }
        cout << endl;
    }
    return 0;
}

and, this one, I edit the line 12 and 14

#ifndef STUDENT_INFO_H_INCLUDED
#define STUDENT_INFO_H_INCLUDED

// Student_info.h header file
#include <iostream>
#include <string>
#include <vector>

struct Student_info {
        std::string name;
        double midterm, final;
        unsigned int& counter;
        std::vector<double> homework;
        double overall = grade(students[counter]);
};

bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);

#endif // STUDENT_INFO_H_INCLUDED

hope anyone can give a hand for this...

For line 12, you declare counter as a reference to int . References are similar to pointers in that they do not hold a value in themselves, but instead refer to another variable. Thus, counter won't work unless you assign a reference to it.

I suspect that for counter (and overall ) what you actually wanted to do was declare it as static , which makes a variable that is shared between all of the instances of the structure.

For line 14, you cannot have you cannot have an initializer inside of a struct , and in any case, you cannot have a non-constant initializer.

BTW, have you studied classes at all yet? The main differences between a struct and a class are a) class members are private by default, while structure members are public by default, and b) structures cannot inherit from other structures or classes. However, by convention you normally would use a class for something that has member functions, even though structures can have them as well.

about classes, not even know what is it. whatsoever, I'll pass this question until I have any idea/knowledge on how to control the structure, proceeding to the next chapter first, stay on this question for days already... -,-"

for line 12, should I remove the reference symbol?

thnx for reply btw...

wait a minute, if I can't have an initializer, then, I will get "'variable' was not declared in this scope" error. so, how can I put the function there?!

btw, static is not learnt yet too, seriously, are there any possible solution for my current knowledge now?

The initializer issue should not cause a variable not to be declared; you'd still have

struct Student_info {
        std::string name;
        double overall;
};

It simply wouldn't have an initial value, so you'd have to calculate the values before storing them in overall .

I think you may be taking the part about the structure calculating the grade too literally; I suspect that what is intended is for you to change the input functions so that, rather than storing the grades directly, they read in the grades, compute the totals, then store it. So all you really need to do is simplify the structure as I have done above, and modify read() .

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.