What's new

Closed Need Help

Status
Not open for further replies.
C++:
#include <iostream>
#include <iomanip>
#include <stdio.h>

using namespace std;

int students = 3;

// student name, subject, remarks
string sn[3], sub[3], rem; // version-safe declaration

// prelim, midterm, endterm
float pl[3], mt[3], et[3]; // version-safe declaration

// ariths
float sum;
float avg;

int main() {
    // counter
    for(int ctr = 0; ctr < students; ctr++) {
      
        cout << "Enter student name: ";
        getline(cin, sn[ctr]);
        cout << sn[ctr] << endl;
      
        cout << "Enter subject: ";
        getline(cin, sub[ctr]);
        cout << sub[ctr] << endl;
      
        cout << "Enter Prelim Grade: ";
        cin >> pl[ctr];
        cout << "" << pl[ctr] << endl; // concat
      
        cout << "Enter Midterm Grade: ";
        cin >> mt[ctr];
        cout << "" << mt[ctr] << endl; // concat
      
        cout << "Enter Endterm Grade: ";
        cin >> et[ctr];
        cout << "" << et[ctr]  << "\n\n"; // concat
      
    cin.ignore(); // unstream cin
    }

    cout << "Computing Data..." << "\n\n"; // notifier

    // frame template
    cout << setw(16) << "Student Name";
    cout << setw(18) << "Subject";
    cout << setw(20) << "Final Grade";
    cout << setw(15) << "Remarks";

    cout << endl; // separator

    for(int ctr = 0; ctr < students; ctr++) {

        // ariths
        sum = pl[ctr] + mt[ctr] + et[ctr];
        avg = sum / (float) students;

        if (avg > 5) {
            rem = "PASSED";
        } else {
            rem = "FAILED";
        }

        cout << setw(10) << sn[ctr];
        cout << setw(17) << sub[ctr];
        cout << setw(13) << avg;
        cout << setw(10) << rem;

        cout << endl; // separator
    }


}

eto version ko, working 'yan, hindi ako gumagamit ng setw() kaya baka may mali ako sa alignment.

kung gusto mo ma-calculate pati fraction (e.g. 16.667) , just use float data type sa calculation mo, pati sa container variable :)
Thanks po dito kunan ko lang po based yung code mo po maraming salamat
 
Pag nag-input ka dapat hindi na ini-echo back yung value.

Try mo ito:

C++:
#include <iostream>
#include <iomanip>

class Student {
    public:
        Student() {};
        Student(std::string _name, std::string _subject, float _prelim, float _midterm, float _endterm) : name(_name), prelim(_prelim), midterm(_midterm), endterm(_endterm) {
            std::cout << "Creating record for student: " << name << std::endl;
            num_students++;
        }

        void set_data(std::string _name, std::string _subject, float _prelim, float _midterm, float _endterm) {
            name = _name;
            subject = _subject;
            prelim = _prelim;
            midterm = _midterm;
            endterm = _endterm;
        }

        float get_avg() {
            return ((prelim+midterm+endterm)/3);
        }

        void get_remarks() {
            float avg = get_avg();
            std::ios_base::fmtflags prev = std::cout.flags();
            std::cout.setf(std::cout.left);
                std::cout << " " << std::setw(19) << name;
                std::cout << std::setw(16) << subject;
                std::cout << std::setw(14) << avg;
             if (avg >= 5.00) {
                std::cout << std::setw(14) << "PASSED" << std::endl;
             }
            else {
                std::cout << std::setw(14) << "FAILED!" << std::endl;
            }
            std::cout.flags(prev);
        }

    protected:
        static unsigned int num_students;
        std::string name, subject;
        float prelim, midterm, endterm;
};
unsigned int Student::num_students {0};

int main(void) {
    unsigned short num_students {0};
    std::cout << "\nProgram that computes student's grades" << std::endl;
    std::cout << "--------------------------------------\n" << std::endl;
    std::cout << "Enter number of students: ";
    std::cin >> num_students;
    std::cin.ignore();
    Student student[num_students];
    for (int i=0; i<num_students; i++) {
        std::string _name, _subject;
        float _prelim, _midterm, _endterm;
        std::cout << "\nEnter student name: ";
        std::getline(std::cin, _name);
        std::cout << "Enter subject: ";
        std::getline(std::cin, _subject);
        std::cout << "Enter prelim grade: ";
        std::cin >> _prelim;
        std::cout << "Enter midterm grade: ";
        std::cin >> _midterm;
        std::cout << "Enter endterm grade: ";
        std::cin >> _endterm;
        std::cin.ignore();
        
        student[i].set_data(_name, _subject, _prelim, _midterm, _endterm);
    }

    std::ios_base::fmtflags prev = std::cout.flags();
    std::cout.setf(std::cout.left);
    std::cout << "\n---------------------------------------------------------" << std::endl;
    std::cout << std::setw(20) << " STUDENT NAME";
    std::cout << std::setw(16) << "SUBJECT";
    std::cout << std::setw(14) << "FINAL GRADE";
    std::cout << "REMARKS" << std::endl;
    std::cout << "---------------------------------------------------------" << std::endl;
    std::cout.flags(prev);

    for (int i=0; i<num_students; i++) {
        student[i].get_remarks();
    }

    std::cout << std::endl;
    return 0;
}
 
#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <string.h>

using namespace std;

int main()
{
/*SN = Student Name
SUB = Subject
REM = Remarks
PL = PreLim
MT = MidTerm
ET = EndTerm
SUM = Summation
AVE = Average*/

string SN,SUB,REM;
float PL,MT,ET;
int SUM,AVE;

for (int CTR=0; CTR<3; CTR++)
{
//Input Operation
cout <<"Enter Student Name:";
getline(cin,SN);
cout << "" << SN <<endl;
cout <<"Enter Subject:";
getline(cin,SUB);
cout << "" << SUB <<endl;
cout <<"Enter Prelim Grade:";
cin >>PL;
cin.ignore();
cout << "" << PL <<endl;
cout <<"Enter Midterm Grade:";
cin >>MT;
cin.ignore();
cout << "" << MT <<endl;
cout <<"Enter Endterm Grade:";
cin >>ET;
cin.ignore();
cout << "" << ET << "\n" <<endl;

}
{
cout <<"Computing Data......" << "\n" <<endl;
}
{
//Display output data
cout << setw(16) <<"Student Name";
cout << setw(18) <<"Subject";
cout << setw(20) <<"Final Grade";
cout << setw(15) <<"Remark"<<endl;
}
{
//Process
SUM = PL+MT+ET;
AVE = SUM/3.0;
AVE = SUM/3;
}
//if else section
if(AVE>=5.0)
{
cout << setw(10) << SN;
cout << setw(17) << SUB;
cout << setw(13) << AVE;
cout << setw(20) << "PASSED"<<endl;
}
else
if(AVE<=5.0)
{
cout << setw(10) << SN;
cout << setw(17) << SUB;
cout << setw(13) << AVE;
cout << setw(20) << "FAILED"<<endl;
}
else
if(AVE>=5)
{
cout << setw(10) << SN;
cout << setw(17) << SUB;
cout << setw(13) << AVE;
cout << setw(20) << "PASSED"<<endl;
}
else
if(AVE<=5)
{
cout << setw(10) << SN;
cout << setw(17) << SUB;
cout << setw(13) << AVE;
cout << setw(20) << "FAILED"<<endl;
}
else

return 0;
}

Sir/Ma'am!

Next time gamitin mo ang BBcode tag na "CODE" kung mag po post ka ng programming code para ma preserve ang indentations/formatting. ;)
 
  1. Kasi iisa lang ng set ng variables ang ginagamit mo sa input per person na tatlong beses mo inulit. Kaya na overwrite yong values at yong lang huli ang nanatili.
  2. Walang ka loop sa output portion ng code mo kaya kahit ilang beses ka pa mag i input ng data. Iisa lang talaga ang output na lalabas. ;)
At tsaka bonus :
Pakitingin sa mga dinagdag kung comments sa code mo.;)

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <string.h>

using namespace std;

int main()
{
    /*SN = Student Name
    SUB = Subject
    REM = Remarks
    PL = PreLim
    MT = MidTerm
    ET = EndTerm
    SUM = Summation
    AVE = Average*/

    string SN,SUB,REM;
    float PL,MT,ET;
    int SUM,AVE;

    for (int CTR=0; CTR<3; CTR++)
    {
//Input Operation
        cout <<"Enter Student Name:";
        getline(cin,SN);
        cout << "" << SN <<endl;
        cout <<"Enter Subject:";
        getline(cin,SUB);
        cout << "" << SUB <<endl;
        cout <<"Enter Prelim Grade:";
        cin >>PL;
        cin.ignore();
        cout << "" << PL <<endl;
        cout <<"Enter Midterm Grade:";
        cin >>MT;
        cin.ignore();
        cout << "" << MT <<endl;
        cout <<"Enter Endterm Grade:";
        cin >>ET;
        cin.ignore();
        cout << "" << ET << "\n" <<endl;

    }


    { // <-- NO PURPOSE
        cout <<"Computing Data......" << "\n" <<endl;
    } // <-- NO PURPOSE
    { // <-- NO PURPOSE
//Display output data
        cout << setw(16) <<"Student Name";
        cout << setw(18) <<"Subject";
        cout << setw(20) <<"Final Grade";
        cout << setw(15) <<"Remark"<<endl;
    } // <-- NO PURPOSE
    { // <-- NO PURPOSE
//Process
        SUM = PL+MT+ET;
        AVE = SUM/3.0;
        AVE = SUM/3;
    }// <-- NO PURPOSE


//if else section
    if(AVE>=5.0)
    {
        cout << setw(10) << SN;
        cout << setw(17) << SUB;
        cout << setw(13) << AVE;
        cout << setw(20) << "PASSED"<<endl;
    }
    else if(AVE<=5.0)
    {
        cout << setw(10) << SN;
        cout << setw(17) << SUB;
        cout << setw(13) << AVE;
        cout << setw(20) << "FAILED"<<endl;
    }
    else if(AVE>=5)
    {
        cout << setw(10) << SN;
        cout << setw(17) << SUB;
        cout << setw(13) << AVE;
        cout << setw(20) << "PASSED"<<endl;
    }
    else if(AVE<=5)
    {
        cout << setw(10) << SN;
        cout << setw(17) << SUB;
        cout << setw(13) << AVE;
        cout << setw(20) << "FAILED"<<endl;
    }
    else

        return 0;
}
 
Declare ave as float or double.
Perform calc
Ave=sum/3.0; //this will ensure decimal output
 
Status
Not open for further replies.

Similar threads

Back
Top