Minimum number funtion

Good day, i am a first year student in Computer Programming. I have been tasked with writing a program that prompts a high school student for personal details and final year marks. On of the functions of that program are that it has to provide the minimum and maximum marks attained by a student using it. this is the function i wrote for that.

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
//MinMax


int max6(int a, int b, int c, int d, int e, int f)
{
	int max = a;
	if(b > max)
		max=b;
	if (c > max)
		max=c;
	if (d > max)
		max=d;
	if (e > max)
		max=e;
	if (f > max)
		max=f;
	return max;
}

int min6(int a, int b, int c, int d, int e, int f)
{
	int min = a;
	if(b < min)
		min=b;
	if (c < min)
		min=c;
	if (d < min)
		min=d;
	if (e < min)
		min=e;
	if (f < min)
		min=f;
	return min;
}


When I add the min6 funtion to the program and compile it. the program promps me for only 5 out of the six subjects its supposed to prompt me for and when giving an average of the 6 marks it makes the final mark that its meant to prompt me for equal to zero. Thus when i cout<<lowesMark i get given zero as the lowest mark even though i was not prompted for it. But as soon as i remove the min6 funtion for the program i get prompted for all 6 marks that i am meant to get prompted for. If anyone couldhelp it would go a long way. Ill attach the part of the program that prompts student for marks also below. Thank.

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
 int english, math, lifeO, history, comL, art;
    int highest, lowest;
    float avgYearMark;
    string name, schoolName;


         //studentDetails
         cout<<"Please key in your name: ";
         getline(cin,name,'\n');
         cout<<"Please key in the name of your school: ";
         getline(cin,schoolName,'\n');

         //getMarks

         while ( english < 0 || english > 100)
              {
                  cout<<"Key in your mark for English: ";
                  cin>>english;
              }

         while ( math < 0 || math > 100)
             {
                cout<<"Key in your mark for Mathematics: ";
                cin>>math;
             }


         while ( lifeO < 0 || lifeO > 100)
             {
                cout<<"Key in your mark for Life Orientation: ";
                cin>>lifeO;
             }


         while ( history < 0 || history > 100)
             {
               cout<<"Key in your mark for History: ";
               cin>>history;
             }


         while ( comL < 0 || comL > 100)
             {
               cout<<"Key in your mark for Computer Literacy: ";
               cin>>comL;
             }

         while ( art < 0 || art > 100)
             {
               cout<<"Key in your  mark for Art: ";
               cin>>art; 
Last edited on
Please post your code again using code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Thank you, just reposted it using code tags.
But where is the code showing the steps that fail?

Unless it's a class with a constructor, variables in C++ are not initialized. So the first time you hit the loop at line 48, art could be anything, including a valid value. The same is try for all the other loops. That's probably why it's prompting for only 5 items. You can solve this by changing your while loops into do loops. For example:
1
2
3
4
         do {
                  cout<<"Key in your mark for English: ";
                  cin>>english;
              } while ( english < 0 || english > 100)


But you should also consider using arrays for this instead. Here's a mostly-complete version that shows what I mean. You'll have to write the code for min(), max() and prompt() according to the comments I've given. For min() and max(), there are also versions in the standard library if you want to use those instead.
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
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;

// Given an array "grades" with "size" items in it, find the maximum item
int max(int grades[], unsigned size)
{

}

// Same as max(), but finds the maximum value.
int min(int grades[], unsigned size)
{
}

// Prompt the user for their grade in the given subject. Keep doing it
// until they enter a value between 0 and 100 (inclusive)
int getGrade(const char *subject)
{
}

int main()
{
    const unsigned NumSubjects = 6;
    enum {English, Math, Life0, History, ComL, Art };
    int grades[NumSubjects];
    const char *subjects[NumSubjects] = { "English", "Math", "Life0",
					  "History", "ComL", "Art" };
					 
    int highest, lowest;
    float avgYearMark;
    string name, schoolName;


    //studentDetails
    cout<<"Please key in your name: ";
    getline(cin,name,'\n');
    cout<<"Please key in the name of your school: ";
    getline(cin,schoolName,'\n');

    grades[0] = getGrade("English");
    for (unsigned i=0; i < NumSubjects; ++i) {
	grades[i] = getGrade(subjects[i]);
    }
    
    highest = max(grades, 6);
    lowest = min(grades, 6);

    cout << "Highest = " << highest << '\n';
    cout << "Lowest = " << lowest << '\n';
}

Thank you so much, using "do" loops instead of "while" loops did the trick.
Topic archived. No new replies allowed.