#include<iostream.h>
#include<fstream.h>//header file
#include<string>
#include<stdlib.h>

void main()
{
fstream pasca;
pasca.open("C:evaluate.dat",ios::out);

fstream display;
paparan.open("C:evaluate.dat",ios::in);



int lecturer_code;
int course_code;
char gred1, gred2, gred3, gred4, gred5;
float point1, point2, point3, point4, point5;
float total_point1, total_point2, total_point3, total_point4, total_point5;

char user;

if(!pasca || !display)
{
cout<<"File cannot open";
exit(1);
}



    cout<<"Enter Lecturer code (in number): ";
    cin>>lecturer_code;
    cout<<"Enter course code (in number): ";
    cin>>course_code;

int bil = 0;
do
 {
    cout<<"Grade question 1: ";
    cin>>gred1;
        if(gred1 == 'A' || gred1 == 'a')
        {point1 = 4;
        point1++;
        }
        else if(gred1 == 'B' || gred1 == 'b')
        {point1 = 3;
        point1++;
        }
        else if(gred1 == 'C' || gred1 == 'c')
        {point1 = 2;
        point1++;
        }
        else if(gred1 == 'D' || gred1 == 'd')
        {point1 = 1;
        point1++;
        }
        else if(gred1 == 'E' || gred1 == 'e')
        {point1 = 0;
        point1++;
        }
    cout<<"Grade question 2: ";
    cin>>gred2;
        if(gred2 == 'A' || gred2 == 'a')
        {point2 = 4;
        point2++;
        }
        else if(gred2 == 'B' || gred2 == 'b')
        {point2 = 3;
        point2++;
        }
        else if(gred2 == 'C' || gred2 == 'c')
        {point2 = 2;
        mata2++;
        }
        else if(gred2 == 'D' || gred2 == 'd')
        {point2 = 1;
        point2++;
        }
        else if(gred2 == 'E' || gred2 == 'e')
        {mata2 = 0;
        point2++;
        }
    cout<<"Grade question 3: ";
    cin>>gred3;
        if(gred3 == 'A' || gred3 == 'a')
        {point3 = 4;
        point3++;
        }
        else if(gred3 == 'B' || gred3 == 'b')
        {point3 = 3;
        point3++;
        }
        else if(gred3 == 'C' || gred3 == 'c')
        {point3 = 2;
        point3++;
        }
        else if(gred3 == 'D' || gred3 == 'd')
        {point3 = 1;
        point3++;
        }
        else if(gred3 == 'E' || gred3 == 'e')
        {point3 = 0;
        point3++;
        }
    cout<<"Grade question 4: ";
    cin>>gred4;
        if(gred4 == 'A' || gred4 == 'a')
        {point4 = 4;
        point4++;
        }
        else if(gred4 == 'B' || gred4 == 'b')
        {point4 = 3;
        point4++;
        }
        else if(gred4 == 'C' || gred4 == 'c')
        {point4 = 2;
        point4++;
        }
        else if(gred4 == 'D' || gred4 == 'd')
        {point4 = 1;
        point4++;
        }
        else if(gred4 == 'E' || gred4 == 'e')
        {point4 = 0;
        point4++;
        }
    cout<<"Grade question 5: ";
    cin>>gred5;
        if(gred5 == 'A' || gred5 == 'a')
        {point5 = 4;
        point5++;
        }
        else if(gred5 == 'B' || gred5 == 'b')
        {point5 = 3;
        point5++;
        }
        else if(gred5 == 'C' || gred5 == 'c')
        {point5 = 2;
        point5++;
        }
        else if(gred5 == 'D' || gred5 == 'd')
        {point5 = 1;
        point5++;
        }
        else if(gred5 == 'E' || gred5 == 'e')
        {point5 = 0;
        point5++;
        }
    pasca<<lecturer_code<<"  "<<course_code<<"  "<<gred1<<"  "<<gred2<<"  "<<gred3<<"  "<<gred4<<"  "<<gred5<<"\n"<<endl;
    bil++;
    cout<<"Do u want to continue (Y-yes / N-no): "<<endl;
    cin>>user;
    }while (user == 'Y' || user == 'y');


cout<<"\t\tEvaluation Details"<<endl;
cout<<"\n\nLecturer\t\t:"<<kod_pensyarah<<endl;
cout<<"Course\t\t\t:"<<kod_kursus<<endl;
cout<<"Bil student\t:"<<bil<<endl;
cout<<"Average:\t"<<endl;
total_point1 = (point1 / bil);
cout<<"\tquestion 1 \t:"<<total_point1<<endl;
total_point2 = (point2 / bil);
cout<<"\tquestion 2\t:"<<total_point2<<endl;
total_point3 = (point3 / bil);
cout<<"\tquestion 3\t:"<<total_point3<<endl;
total_point4 = (point4 / bil);
cout<<"\tquestion 4\t:"<<total_point4<<endl;
total_point5 = (point5 / bil);
cout<<"\tquestion 5\t:"<<total_point5<<endl;

display.close();
pasca.close();
}

This is the new one without syntax error..

#include<iostream.h>
#include<fstream.h>//header file
#include<string>
#include<stdlib.h>

void main()
{
fstream pasca;
pasca.open("C:evaluate.dat",ios::out);

fstream display;
display.open("C:evaluate.dat",ios::in);



int lecturer_code;
int course_code;
char gred1, gred2, gred3, gred4, gred5;
float point1, point2, point3, point4, point5;
float total_point1, total_point2, total_point3, total_point4, total_point5;

char user;

if(!pasca || !display)
{
cout<<"File cannot open";
exit(1);
}



    cout<<"Enter Lecturer code (in number): ";
    cin>>lecturer_code;
    cout<<"Enter course code (in number): ";
    cin>>course_code;

int bil = 0;
do
 {
    cout<<"Grade question 1: ";
    cin>>gred1;
        if(gred1 == 'A' || gred1 == 'a')
        {point1 = 4;
        point1++;
        }
        else if(gred1 == 'B' || gred1 == 'b')
        {point1 = 3;
        point1++;
        }
        else if(gred1 == 'C' || gred1 == 'c')
        {point1 = 2;
        point1++;
        }
        else if(gred1 == 'D' || gred1 == 'd')
        {point1 = 1;
        point1++;
        }
        else if(gred1 == 'E' || gred1 == 'e')
        {point1 = 0;
        point1++;
        }
    cout<<"Grade question 2: ";
    cin>>gred2;
        if(gred2 == 'A' || gred2 == 'a')
        {point2 = 4;
        point2++;
        }
        else if(gred2 == 'B' || gred2 == 'b')
        {point2 = 3;
        point2++;
        }
        else if(gred2 == 'C' || gred2 == 'c')
        {point2 = 2;
        point2++;
        }
        else if(gred2 == 'D' || gred2 == 'd')
        {point2 = 1;
        point2++;
        }
        else if(gred2 == 'E' || gred2 == 'e')
        {point2 = 0;
        point2++;
        }
    cout<<"Grade question 3: ";
    cin>>gred3;
        if(gred3 == 'A' || gred3 == 'a')
        {point3 = 4;
        point3++;
        }
        else if(gred3 == 'B' || gred3 == 'b')
        {point3 = 3;
        point3++;
        }
        else if(gred3 == 'C' || gred3 == 'c')
        {point3 = 2;
        point3++;
        }
        else if(gred3 == 'D' || gred3 == 'd')
        {point3 = 1;
        point3++;
        }
        else if(gred3 == 'E' || gred3 == 'e')
        {point3 = 0;
        point3++;
        }
    cout<<"Grade question 4: ";
    cin>>gred4;
        if(gred4 == 'A' || gred4 == 'a')
        {point4 = 4;
        point4++;
        }
        else if(gred4 == 'B' || gred4 == 'b')
        {point4 = 3;
        point4++;
        }
        else if(gred4 == 'C' || gred4 == 'c')
        {point4 = 2;
        point4++;
        }
        else if(gred4 == 'D' || gred4 == 'd')
        {point4 = 1;
        point4++;
        }
        else if(gred4 == 'E' || gred4 == 'e')
        {point4 = 0;
        point4++;
        }
    cout<<"Grade question 5: ";
    cin>>gred5;
        if(gred5 == 'A' || gred5 == 'a')
        {point5 = 4;
        point5++;
        }
        else if(gred5 == 'B' || gred5 == 'b')
        {point5 = 3;
        point5++;
        }
        else if(gred5 == 'C' || gred5 == 'c')
        {point5 = 2;
        point5++;
        }
        else if(gred5 == 'D' || gred5 == 'd')
        {point5 = 1;
        point5++;
        }
        else if(gred5 == 'E' || gred5 == 'e')
        {point5 = 0;
        point5++;
        }
    pasca<<lecturer_code<<"  "<<course_code<<"  "<<gred1<<"  "<<gred2<<"  "<<gred3<<"  "<<gred4<<"  "<<gred5<<"\n"<<endl;
    bil++;
    cout<<"Do u want to continue (Y-yes / N-no): "<<endl;
    cin>>user;
    }while (user == 'Y' || user == 'y');


cout<<"\t\tEvaluation Details"<<endl;
cout<<"\n\nLecturer\t\t:"<<lecturer_code<<endl;
cout<<"Course\t\t\t:"<<course_code<<endl;
cout<<"Bil student\t:"<<bil<<endl;
cout<<"Average:\t"<<endl;
total_point1 = (point1 / bil);
cout<<"\tquestion 1 \t:"<<total_point1<<endl;
total_point2 = (point2 / bil);
cout<<"\tquestion 2\t:"<<total_point2<<endl;
total_point3 = (point3 / bil);
cout<<"\tquestion 3\t:"<<total_point3<<endl;
total_point4 = (point4 / bil);
cout<<"\tquestion 4\t:"<<total_point4<<endl;
total_point5 = (point5 / bil);
cout<<"\tquestion 5\t:"<<total_point5<<endl;

display.close();
pasca.close();
}

the output should be like this..but the counting for the average of total point was wrong.. OUTPUT

Forget the computer for a bit. Think of the algorithm. How would you do it with pencil and paper? What steps would you take? Write the steps out exactly. Follow the steps exactly to make sure they are right for all scenarios.

Now go back to the code and make sure that the code follows the steps you wrote out. Notice the difference from what you have posted here.

Hi Hayati_mahamad, the problem is, the total for point1 to point 5 are not getting accumulated, in the sense, as you can see in the optput, the average is the points entered for the last student divided by number of students, as per the attachment - (points of student 2/2).

When the do{...} loop runs, the previous value of point gets overwritten by new point value.

I'll suggest removing point1++, point2++, point3++, point4++,and point5++ because they are not implementing intended logic they just increment the value by 1, and then replacing that line where you accumulate the values.

Some suggestions:

  • Try using arrays, they can make your program smaller and more readable
  • Initialise variables

I hope it is somewhat clear to you what's going wrong here. Don't hesistate if you have more questions.

can u give me example for that..

OK, here's one for you:

int total =0;
int marks[5] = {10, 20, 30, 40, 50};

for(int i=0; i< 5; i++){
    total = total + marks[i];
}
// here

In the example above, first the total is initialised to 0, next, an array containing 5 elements is declared.

Inside the for loop, we access every array element value and add it to the total value. The for loop runs 5 times and below is the what variable values be every time:

1st iteration: total = 0 + 10 = 10
2nd iteratoin: total = 10 + 20 = 30
3rd iteratoin: total = 30 + 30 = 60
4th iteration: total = 60 + 40 = 100
5th iteration: total = 100 + 50 = 150

So, after the end of for loop, the sum of all numbers can be used with the total variable.

In your problem, you can use some variable that stores sum of all point1s, sum of all point2s and so on, and dividing these numbers with bil would give you average points.

so, how to use this if i want to combine with nested if?

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.