|
-
April 27th, 2008, 02:05 PM
#1
A bit of help with a code...
Hello,
I am creating a program for a class in which we have to create a menu in which allows the function for option A to 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.
I have created this so far and it works, but I'm not sure that this is what he is asking for:
#include <iostream>
using namespace std;
char DoMenu();
void find_largest();
void find_smallest();
int main()
{
char choice;
do
{
choice = DoMenu();
switch (choice)
{
case 'a': // fall through
case 'A': find_largest(); break;
case 'b':
case 'B': find_smallest(); break;
}
} while ((choice != 'C') && (choice != 'c'));
return 0;
}
char DoMenu()
{
char MenuChoice;
cout << " ****************************************************************** " << endl;
cout << endl;
cout << " A - Find the largest # with a known quantity of numbers. " << endl;
cout << " B - Find the smallest # with an unknown quantity of numbers. " << endl;
cout << " C - Quit " << endl;
cout << endl;
cout << " ****************************************************************** " << endl;
cout << endl;
cout << endl;
cout < " Please enter your choice: ";
cin >> MenuChoice;
cout << endl;
cout << endl;
cout << " Excellent! You chose option " << MenuChoice << "! Let's continue." << endl;
return MenuChoice;
}
void find_largest()
{
int largest = 0;
int numbers=0, input, i=0;
cout << endl;
cout << " How many numbers would you like to enter?" << endl;
cin >> numbers;
for (i=0; i<numbers; i++)
{
cout << " Please enter number " << i+1 << ": ";
cin >> input;
if (input > largest)
largest = input;
}
cout << endl;
cout << " The largest number that is in the list you created is " << largest << " " << endl;
cout << endl;
cout << " ****************************************************************** " << endl;
cout << endl;
}
void find_smallest()
{
int smallest = 9999;
int input=0, i=1;
cout << endl;
cout << " Please enter a list of numbers. When you are finished, enter '-9999' to finish listing." << endl;
do
{
cout << " Enter number " << i << " please : ";
cin >> input;
i++;
if ((input < smallest) && (input != -9999))
smallest = input;
} while (input != -9999);
cout << endl;
cout << " The smallest number you input was " << smallest << " " << endl;
cout << endl;
cout << " ****************************************************************** " << endl;
cout << endl;
}
Any help would be greatly appreciated!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|