I have to take my current code which displays a menu and repeats until the option 'c' is chosen which quits the program. Now I have to modify that same code 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. I need to use void greatest() {

the example is

void greatest()
{

      //Initialize final result variable to largest number possible
      //Ask user How many numbers he would like to enter
      //Read number of numbers user plans to enter
      //start a loop, increment from 0 to number of numbers user plans to enter
      //Read next number
      //Compare number to final result variable
      //if number is greater than final result variable, then
      //store in final result variable
      //end of loop
}

Here is my working code and what I need to modify I don't know what to leave in the main body and move to the void body and what exactly the first comment in the example means to do someone please help me!!

#include <iostream>

using namespace std;

double larger(double x, double y);
double smaller(double x, double y);

const int SENTINEL = -99;

int main()
{

	bool done;
	char letter;
	char a, b, c;
	int qty, count;
	double max, low, num;

	done = false;

	while (!done)
	{
		cout << "Please choose one of the following choices.\n"
			 << "A - Find the largest # with a known quantity of numbers\n"
			 << "B - Find the smallest # with an unknown quantity of numbers\n"
			 << "C - Quit" << endl;
		
		cout << "Please enter your choice: ";
		cin >> letter;
		cout << endl;
	
		if (letter == 'a')
		{
			cout << "Enter quantity of numbers: ";
			cin >> qty;
			count = qty;

			cout << "Enter " << qty << " numbers: ";
			cin >> num;
			max = num;

			for (count = 1; count < qty; count++)
			{
				cout << "Enter another number: ";
				cin >> num;
				max = larger(max, num);
				done = false;
			}
			
			cout << "The largest number is: " << max << endl;
			done = false;
		}
		if (letter == 'b')
		{
			cout << "Enter a number, to stop enter -99: ";
				cin >> num;
				low = num;
				
				while (num != SENTINEL)
				{
					cout << "Enter a number, to stop enter -99: ";
					cin >> num;
					if (num != SENTINEL)
					{
						low = smaller(low, num);
					}
				}

				cout << "The smallest number is: " << low << endl;

		done = false;
		}
	if (letter == 'c')
	{
		done = true;
	}
	}
	return 0;
}
double larger (double x, double y)
{
	if (x >= y)
		return x;
	else
		return y;
}
double smaller (double x, double y)
{
	if (x <= y)
		return x;
	else
		return y;
}

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

>The function for A should have the quantity of numbers passed in as a parameter and needs to return the largest number


int largerNumber( int quantity);

Red indicates what type your function is returning, i.e what gets passed back to main(). If nothing is returned to change that to void. The green tells you what variables are being passed into your function.

I don't know if that helped?

The comments for void greatest more or less tells you exactly what you need to do.

So it would look something like this?

void greatest()
{
       int largerNumber(int quantity) // or would it be qty like in my code?

       for (count = 1; count > qty; count++)
       {
               cout << "Enter " << qty << " numbers: ";
               cin >> num;
               max = larger(max, num);
               done = false;
       }
              cout << "The largest number is: " << max;
}
Member Avatar for iamthwee

Try it.

ok I tried that I changed the code in my main so far to:

if (letter == 'a')
		{
			cout << "Enter quantity of numbers: ";
			cin >> qty;
			count = qty;
			larger (qty);
		}

and added this for void after my main and before my double larger function:

void greatest()
{
	int largerNumber(int qty)
		for (count = 1; count > qty; count++)
		{
			cout << "Enter " << qty << " numbers: ";
			cin >> num;
			max = larger(max, num);
			done = false;
		}
		cout << "The largest number is: " << max << endl;
}

but I get 7 errors and I am new to programming I dont even understand those errors they look like this:

C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(468): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, overloaded-function)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.