2 Replies - 936 Views - Last Post: 15 February 2009 - 03:58 PM Rate Topic: -----

#1 qazwsxed22   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-February 09

user defined functions

Posted 15 February 2009 - 03:41 PM

If anyone can spot my errors please help...
#include <iostream>

using namespace std;

const int SENTINEL = -99;
int greatest(int i);
int smallest();

int main ()
{
	// Declaring variables
	char letter;	//letter chosen from the menu
	int i;			//quantity of numbers user will enter	
	int counter;	//loop control variables
	int num;	 // variable to store new number
	int largest;	 // variable to store largest number
	int least; 

	// Initializing variables
	largest = -9999999;
	least = 9999999;
	// Big loop here for the condition of the Application running
	do
	{
		//displaying Menu
		cout << "Please Select One of the Following Choices" << endl;
		cout << "A: Find the largest number with a known quantity of number " << endl;
		cout << "B: Find the smallest number with an unknown quantity of number " << endl;
		cout << "C: Quit" << endl;	
		cout << "Please enter your choice: ";
		cin >> letter;
		cout << endl;
		
		switch (letter)
		{
		case 'A':
		case 'a':
				cout << "How many numbers would you like to enter"<< " ";
				cin >> i;
				cout << endl; 
				cout << "Please enter your numbers" << endl;
				cin >> num;
				cout << "The largest number you entered is" << " " << greatest(i);
				cout << " ";
				cout << endl;
				break;

		case 'B':
		case 'b':
			cout << "Please enter your numbers with your last number being" << " " << SENTINEL << endl;
			cin >> num;
			while (num != SENTINEL)
			{
					cout << smallest();
			}
			cout << "The smallest number you entered is" << " " << smallest() << endl;
		}
	}
	while (letter != 'C' && letter != 'c');

	cout << endl;

	return 0;
}
int Greatest(int i) 

{
int large;
int num;
int count;
	large = -999999;	  // Initialize final result variable to largest number possible
for (count = 0, count <= i, count++;)	  // start a loop, increment from 0 to count 
{
	  cin << num;//	   Read next number
if 
 num > large  //	   Compare number to final result variable
large = num;   //	   if number is greater than final result variable, then
				//	   store in final result variable

}	 // end of loop
return large;//   return final result
}// end of function greatest

smallest()
{
least = 9999999   // Initialize final result variable to smallest number possible

int num, int least; // Declare all variables
  while (number != SENTINEL)
			{
					
					if (num < least)   //code for smallest goes here
						least = num;
					cin >> num;
			}

return  least;



}


This post has been edited by qazwsxed22: 15 February 2009 - 05:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: user defined functions

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: user defined functions

Posted 15 February 2009 - 03:51 PM

Two I can see with a quick glance:

if
 num > large 


//needs to be 

if( num > large )



Another:

int smallest() //function need return identifier
{
least = 9999999   // Initialize final result variable to smallest number possible

int num, least; //only need 'int' once
  while (number != SENTINEL)
			{
				   
					if (num < least)   //code for smallest goes here
						least = num;
					cin >> num;
			}
return  least;
}


This post has been edited by KYA: 15 February 2009 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 bszmyd   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: user defined functions

Posted 15 February 2009 - 03:58 PM

View Postqazwsxed22, on 15 Feb, 2009 - 02:41 PM, said:

case 'A':
case 'a':
   cout << "How many numbers would you like to enter"<< " ";
   cin >> i;
   cout << endl; 
   cout << "Please enter your numbers" << endl;
   cin >> num;
   cout << "The largest number you entered is" << " " << greatest(i);

Here your only reading in one number into num...you need to do a for loop from 0 to i-1 each time reading into an array of numbers from cin. Declare num like: int num[MAX_NUMBERS] Where MAX_NUMBERS is some constant. Then in your case statement:
   cout << "How many numbers would you like to enter"<< " ";
   cin >> i;
   cout << endl; 
   for (int x = 0; i < x; ++x )
   {
	  cout << "Please enter a number" << endl;
	  cin >> num[x];
   }

You also are passing only the number of entries they have to greatest, you probably also need to pass a pointer to the array of numbers they entered. This is also true for smallest. In that case statement you need cin >> num; in the while loop and the call to smallest just outside it. Otherwise, again, you're only reading in one number and printing out the smallest essentially forever.

Take some of these suggestions, fix it up and re-post and we'll continue on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1