Question About Functions.

Hi, I'm only in my second term of college and still grasping everything about C++ programming. This week in week 5 of my term we have this assignment.

Functions

Modify your week 4 assignment to include a function for both A and B. The function for A should have the quantity of numbers passed in as a parameter and needs to return the largest number. The function for B should have no parameters and return the smallest number.

The thing I'm confused on is I don't understand how I'm supposed to change my code into a function. I've talked to my teacher and she says I have case statements in my code. Any advice, help on this would be awesome. Here is my code from last week...

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
#include <iostream>

using namespace std;

const int SENTINEL = -99;														//Sentinel Value set at -99 that will cause the loop to end

int main()
{																				//Variables
	int numbers;	
	int smallest;	
	int counter;	
	int largest;
	int next;
	char response;
	bool quit = false;

	while(!quit)																//While statement that never ends unless user inputs right commands
	{
		cout << "\nWelcome and please use the following menu.\n";
		cout << "\nEnter A -- Find the largest # with a set quantity of numbers. \n";
		cout << "Enter B -- Find the smallest # with an unknown quantity of numbers. \n";
		cout << "Enter C -- Quit Program. \n";
		cout << "\nPlease enter your choice: ";
		cin >> response;														//User Input for the menu

		switch (response)														//Switch Statement allows transitions between Inputs
		{
		case 'A':																//Switch Statement allows for either Uppercase or lowercase letters to be entered
		case 'a':
			cout << "\nIn this section you will enter numbers and compare them to figure out the \nlargest of the numbers you entered.\n";
			cout << "\nEnter the amount of numbers to be compared. \n";			//User enters numbers to be compared and find the largest of them
			cin >> numbers;
			cout << "\nEnter the numbers.\n";
			cin >> largest;
			for (counter = 0; counter < numbers-1; counter++)					//For loop figures out largetst number entered by the user
			{
				cin >> next;
				if (largest < next)
				{
					largest = next;
				}
			}
			cout << "\nThe largest number entered was. \n";
			cout << largest << endl;											//Output of largest number entered
			break;
		case 'B':
		case 'b':
			cout << "\nEnter any amount of numbers.\n";							//User Inputs numbers
			cout << "After that enter -99 to display the smallest number and exit.\n";	//User enters Sentinel Value -99 to stop
			cin >> smallest;
			next = smallest;
			while (next != -99)													//While statement sorts out all the numbers to figure out smallest
			{
				cin >> next;
				if (smallest > next && next != -99)
				{
					smallest = next;
				}
			}			
			cout << "\nThe smallest number entered was. \n";
			cout << smallest << endl;											//Output of smallest number entered
			break;
		case 'C':
		case 'c':
			quit = true;														//Ends the Program
			break;
		}																		// Ends Switch
	}																			// End While
	return 0;
}


she says I have case statements in my code
http://cplusplus.com/doc/tutorial/control/

I don't understand how I'm supposed to change my code into a function
http://cplusplus.com/doc/tutorial/functions/
I guess the problem is that I see how a function works. But I don't know how to take the code I already have an make it into a function.
Alright I've changed the code for A what do you think about this function.
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
#include <iostream>

using namespace std;

const int SENTINEL = -99;//Sentinel Value set at -99 that will cause the loop to end

int larger(int x, int y);

int main()
{	//Variables
	int numbers;	
	int smallest;	
	int counter;	
	int largest;
	int next;
	char response;
	bool quit = false;

	while(!quit)//While statement that never ends unless user inputs right commands
	{
		cout << "\nWelcome and please use the following menu.\n";
		cout << "\nEnter A -- Find the largest # with a set quantity of numbers. \n";
		cout << "Enter B -- Find the smallest # with an unknown quantity of numbers. \n";
		cout << "Enter C -- Quit Program. \n";
		cout << "\nPlease enter your choice: ";
		cin >> response;//User Input for the menu

		switch (response)//Switch Statement allows transitions between Inputs
		{
		case 'A'://Switch Statement allows for either Uppercase or lowercase letters to be entered
		case 'a':
			cout << "\nIn this section you will enter numbers and compare them to figure out the \nlargest of the numbers you entered.\n";
			cout << "\nEnter the amount of numbers to be compared. \n";//User enters numbers to be compared and find the largest of them
			cin >> numbers;
			cout << "\nEnter the numbers.\n";
			cin >> largest;			
			for (counter = 0; counter < numbers-1; counter++)//For loop figures out largetst number entered by the user
			{
				cin >> next;
				largest = larger(largest, next);				
			}
			cout << "\nThe largest number entered was. \n";
			cout << largest << endl;//Output of largest number entered
			break;
		case 'B':
		case 'b':
			cout << "\nEnter any amount of numbers.\n";//User Inputs numbers
			cout << "After that enter -99 to display the smallest number and exit.\n";//User enters Sentinel Value -99 to stop
			cin >> smallest;
			next = smallest;
			while (next != -99)//While statement sorts out all the numbers to figure out smallest
			{
				cin >> next;
				if (smallest > next && next != -99)
				{
					smallest = next;
				}
			}			
			cout << "\nThe smallest number entered was. \n";
			cout << smallest << endl;//Output of smallest number entered
			break;
		case 'C':
		case 'c':
			quit = true;//Ends the Program
			break;
		}// Ends Switch
	}// End While
	return 0;
}

int larger(int x, int y)// Larger Function
{
	if (x >= y)
		return x;
	else
		return y;
}


I think A now has a function like my assignment asks for.
Now I just think I need help with part B which I'm at a lost with.
Topic archived. No new replies allowed.