Writing Functions for a school report

Some parts of my code are not showing the result 1)engMark and matMark what is wrong. and the lowest and highest mark in int main were m i going wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  //program to compile a student academic report
#include<iostream>
using namespace std;

void studentDetails(string & name,string & surname,string & schoolname)
 {
     cout<<"enter your name :";
     cin>>name;
     cout<<"enter your surname:";
     cin>>surname;
     cout<<"enter the name of your school:";
     cin>>schoolname;
 }

 float getMarks (float&engMark,float&matMark,float&LOMark,float&HisMark,float&CLMark,float&GeoMark)

 {

    if (engMark>0 && engMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>engMark;}

    if (matMark>0 && matMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>matMark;}

     if (LOMark>0 && LOMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>LOMark;}

     if (HisMark>0 && HisMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>HisMark;}

     if (CLMark>0 && CLMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>CLMark;}

     if (GeoMark>0 && GeoMark<=100)
    {cout<<" Please enter your marks   :";
     cin>>GeoMark;}
}

float averageyearmark(float engMark1,float matMark1,float LOMark1,float HisMark1,float CLMark1,float GeoMark1)
{
    return (engMark1+matMark1+LOMark1+HisMark1+CLMark1+GeoMark1)/6;
}
 float minMax(float engMark1,float matMark1,float LOMark1,float HisMark1,float CLMark1,float GeoMark1)
 {
     float highest =engMark1;
     if (matMark1>highest)
     highest=matMark1;
     if (LOMark1>highest)
     highest=LOMark1;
     if (HisMark1>highest)
     highest=HisMark1;
     if (CLMark1>highest)
     highest=CLMark1;
     if (GeoMark1>highest)
     highest=GeoMark1;
 cout<<"highest mark is"<<highest<<endl;
     float lowest =engMark1;
     if (matMark1<lowest)
     lowest=matMark1;
     if (LOMark1<lowest)
     lowest=LOMark1;
     if (HisMark1<lowest)
     lowest=HisMark1;
     if (CLMark1<lowest)
     lowest=CLMark1;
     if (GeoMark1<lowest)
     lowest=GeoMark1;
cout<<"lowest mark is"<<lowest<<endl;

      return lowest;

 }

int main()

{
    string name1,surname1,schoolname1;
    float engMark,matMark,LOMark,HisMark,CLMark,GeoMark,average,highest1,lowest1;


    studentDetails(name1,surname1,schoolname1);
    getMarks(engMark,matMark,LOMark,HisMark,CLMark,GeoMark);
    cout<<" marks  English mark:"<<engMark<< "Mathematics mark:"<<matMark<< "Life Orientation Mark"<<LOMark<<"History Mark:"<<HisMark<<"Computer Literacy Mark :"<<CLMark<<"Geography Mark"<<GeoMark<<endl;
    average=averageyearmark(engMark,matMark,LOMark,HisMark,CLMark,GeoMark);
    cout<<"average"<<average<<endl;
    minMax(engMark,matMark,LOMark,HisMark,CLMark,GeoMark);
    cout<<"the highest mark is:"<<highest1<<"the lowest mark is:"<<lowest1<<endl;
    return 0;
Some parts of my code are not showing the result 1)engMark and matMark

Be specific. What parts of the code are not showing these things, that you expect to be showing them?

You've only written code in one place that outputs these values, at line 88. Are you trying to tell us that it's not outputting those values at that line?

and the lowest and highest mark in int main

Do you mean that line 92 isn't displaying any output?

Please be clear, complete and precise when describing your problem.
You provide uninitialized values to getMarks(...). It does not make sense but you test these uninitialized values within getMarks(...), thus parts or all of the input may or may not work.
You also have functions of types, but they should be void functions.
what i mean is line 19 and line 23 do not ask for input of marks i just get 4 instead of 6 "please enter your marks:" therefore making line 88 show part of the marks not all so i realised that only matMark and engMark are not showing. when i run the program

Line 92 does show some numbers that dont change even if you change your marks therefore making my output wrong. For lowest and highest mark.

Were am i going wrong
As coder777 has already explained, in your getMarks() function, you only prompt for input for each variable, if the initial value of that variable is already between 0 and 100. But you don't actually initialise those variables to anything, so the value could be anything.

For example, at line 19, you only ask the user to input a value for engMark, if engMark is initially between 0 and 100. But you have no control over whether or not that will be true, because you haven't initialised the variable that you pass in as engMark.
Last edited on
I think what you meant to do was limit the range of your variables to a number between 0 and 100. You do this with a loop that checks user input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  //program to compile a student academic report
#include<iostream>
using namespace std;

void studentDetails(string & name,string & surname,string & schoolname)
 {
     cout<<"enter your name :";
     cin>>name;
     cout<<"enter your surname:";
     cin>>surname;
     cout<<"enter the name of your school:";
     cin>>schoolname;
 }

 void getMarks (float&engMark,float&matMark,float&LOMark,float&HisMark,float&CLMark,float&GeoMark)
 {
        do{
        cout << "Enter your marks: ";
        cin >> engMark;}
        while(engMark<0 || engMark >100);
        
         do{
        cout << "Enter your marks: ";
        cin >> matMark;}
        while(matMark<0 || matMark >100);
        
         do{
        cout << "Enter your marks: ";
        cin >> LOMark;}
        while(LOMark<0 || LOMark >100);
   
        do{
        cout << "Enter your marks: ";
        cin >> HisMark;}
        while(HisMark<0 || HisMark >100);
        
         do{
        cout << "Enter your marks: ";
        cin >> CLMark;}
        while(CLMark<0 || CLMark >100);
        
         do{
        cout << "Enter your marks: ";
        cin >> GeoMark;}
        while(GeoMark<0 || GeoMark >100);
}

float averageyearmark(float engMark1,float matMark1,float LOMark1,float HisMark1,float CLMark1,float GeoMark1)
{
    return (engMark1+matMark1+LOMark1+HisMark1+CLMark1+GeoMark1)/6;
}
 void minMax(float engMark1,float matMark1,float LOMark1,float HisMark1,float CLMark1,float GeoMark1, float &highest, float &lowest)
 {
     highest =engMark1;
     if (matMark1>highest)
     highest=matMark1;
     if (LOMark1>highest)
     highest=LOMark1;
     if (HisMark1>highest)
     highest=HisMark1;
     if (CLMark1>highest)
     highest=CLMark1;
     if (GeoMark1>highest)
     highest=GeoMark1;
     
     lowest =engMark1;
     if (matMark1<lowest)
     lowest=matMark1;
     if (LOMark1<lowest)
     lowest=LOMark1;
     if (HisMark1<lowest)
     lowest=HisMark1;
     if (CLMark1<lowest)
     lowest=CLMark1;
     if (GeoMark1<lowest)
     lowest=GeoMark1;


 }

int main()

{
    string name1,surname1,schoolname1;
    float engMark,matMark,LOMark,HisMark,CLMark,GeoMark,average,highest1,lowest1;


    studentDetails(name1,surname1,schoolname1);
    getMarks(engMark,matMark,LOMark,HisMark,CLMark,GeoMark);
    cout<<" marks  English mark:"<<engMark<< "Mathematics mark:"<<matMark<< "Life Orientation Mark"<<LOMark<<"History Mark:"<<HisMark<<"Computer Literacy Mark :"<<CLMark<<"Geography Mark"<<GeoMark<<endl;
    average=averageyearmark(engMark,matMark,LOMark,HisMark,CLMark,GeoMark);
    cout<<"average"<<average<<endl;
    minMax(engMark,matMark,LOMark,HisMark,CLMark,GeoMark, highest1, lowest1);
    cout<<"The highest mark is:"<<highest1<<"\nThe lowest mark is:"<<lowest1<<endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.