How to transform this code into [Functions]?

I need to change my previous program (below) to use functions such as:

int getIntSum(string s, int exitVal)

quizSum = getIntSum("Enter a quiz score", 0);

The purpose of this function is encapsulate the code to add up a series of user-entered values and return their sum. This function will display the string passed as the first argument along with the second value which is the special value to be entered by the user to signal that there are no more values to enter.


int getPromptedInt(string s, int min, int max)

maxTestPoints = getPromptedInt("Enter the maximum possible test points", 0, 200);

This function will ask for (via the first argument) and return an integer value between max and min.


I have no idea how to even start this ... any help would be great.
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
int main ()
{
        cout << fixed << setprecision(2);
	int progave = 1;
	int quizave = 1; 
	int testave = 1; 
	double examave = 1; 
	double prog, test, quiz; 
		
	double score1, score2, score3, maxquiz, maxtest, maxprog;
	string choice;

	cout << "*** 240 Course Average Calculator ***" << endl << endl;
	do
	{
		cout << "Enter the maximum possible program points so far: ";
		cin >> maxprog;
	
		do
		{	
				cout << "Enter a program score (-1 to quit): ";
				cin >> score1;
			    progave += score1;				
		}
		while (score1 != -1);
		
		cout << "Enter the maximum possible quiz points so far: ";
		cin >> maxquiz;

		do
		{
				cout << "Enter a quiz score (-1 to quit): ";
				cin >> score2;
				quizave += score2;		
		}
		while (score2 != -1);
		
		cout << "Enter the maximum possible test points so far: ";
		cin >> maxtest;	 
	  
	    if (maxtest != 0)
		{
		do
		{				
				cout << "Enter a test score (-1 to quit): ";
				cin >> score3; 
				testave += score3;		
		}
		while (score3 != -1);
	    }
		else
		{
		 cout << "No tests entered so no test average calculated" << endl;
		}
	
		if (maxtest != 0)
		{	
			test = (((testave)/maxtest)*100);
    		quiz = (((quizave)/maxquiz)*100);
      		prog = (((progave)/maxprog)*100);
			examave = (((((testave/maxtest)*100)*3)+((quizave/maxquiz)*100))/4);
			cout << " " << endl;
			cout << "Program average is:  " << prog << endl;
			cout << "Quiz average is:     " << quiz << endl;
			cout << "Test average is:     " << test << endl;
			cout << "Exam average is:     " << examave << endl;
			cout << "Overall average is:  " << ((prog*0.40)+(quiz*0.15)+(test*0.45)) << endl << endl;
		}
		else
		{
			test = (((testave)/maxtest)*100);
    		quiz = (((quizave)/maxquiz)*100);
      		prog = (((progave)/maxprog)*100);
			cout << " " << endl;
			cout << "Program average is:  " << prog << endl;
			cout << "Quiz average is:     " << quiz << endl;
			cout << "No test score was entered! "<< endl;
			cout << "Exam average is:     " << quiz << endl;
			cout << "Overall average is:  " << ((prog*0.40)+(quiz*0.60)) << endl << endl;
		}
	    if (examave < 55)
		{
			cout << "*** Your Exam average is below 55%!!! ***" << endl << endl;
		}	
		cout << "Another? (y/n)";
		cin >> choice;
		cout << " " << endl;
	
		progave = 1;
		quizave = 1; 
		testave = 1; 
		examave = 1;	   
  	 }
	 while (choice == "y" || choice == "Y");	
return 0;
}


why do you need all the loops in there? is that required or are you just doing it. The trick to functions is in int main() you'd say


double score1, score2, score3, maxquiz, maxtest, maxprog;
string choice;

then you'd say

cout<<"Enter the 1st score"<<endl;
cin>>score1;

keep doing that until you get all your input.
to input it into a function you'd say

getIntSum(choice, 0) //you have to change it to an int 0 being the exit number so while (choice!=0)

Basically, to get it into a function you have to drop the void/int/double/string/ etc...
heres an example

void viewnumsum(int Number1, int Number2)
int main()
{
int num, num1;
cout<<"enter a number"<<endl;
cin>>num;
cout<<"enter another number"<<endl;
cin>>num1;
viewnumsum(num, num1);//drop the void and the int signs put your input in
}
void viewnumsum(int Number1, int Number2)//use the orignal function declared
{
cout<<Number1+Number2;
}
Hope this helps

Topic archived. No new replies allowed.