Menu-Driven Program

can't get it too build, plus need help with calcRange

Page 1 of 1

2 Replies - 3071 Views - Last Post: 21 February 2009 - 07:43 AM Rate Topic: -----

#1 Darbkeshan  Icon User is offline

  • New D.I.C Head

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

Menu-Driven Program

Post icon  Posted 20 February 2009 - 11:56 PM

Ok, I'm trying to create this menu-driven program that does several different calculations. right now my main problem is that I don't know how to make my menu() function work with the driver program. the error says "undefined reference to 'Menu()'

Also I'm not sure how to make my calcRange function efficiently. I need it to take the 4 numbers from doRange and find the difference between the largest number and the smallest number.

I know I could write: if (number1 < number2 & number1 <number3.... etc. but I have a feeling there's a much more efficient way that I'm not thinking of.


#include <iostream>
#include <iomanip>

using namespace std;

/* Function Prototypes */

string Menu();

int getValue( string, int, int );

void doAverage();
double calcAverage( int, int );

void doFactorial();
int calcFactorial( int );

void doMtoNth();
int calcMtoNth( int, int );

void doRange();
int calcRange( int, int, int, int );

int main()
  {
  string userChoice;
  
  userChoice = Menu();
  
  while ( userChoice != "Q" && userChoice != "q" )
	{
	if ( userChoice == "1" )
	  {
	  doAverage();
	  }
	else if ( userChoice == "2" )
	  {
	  doFactorial();
	  }
	else if ( userChoice == "3" )
	  {
	  doMtoNth();
	  }
	else //option "4" was selected
	  {
	  doRange();
	  }

	userChoice = Menu();
	}//end of the while loop

  return 0;
  }

/**********  Functions  **********/

//function menu

  string Menu(string userChoice)
	{
	cout << "Special Purpose Calculator\n";
	  cout << "\n1) Average";
	  cout << "\n2) Factorial";
	  cout << "\n3) M to the Nth Power";
	  cout << "\n4) Range\n";
	cout << "\nQ) Quit\n";
	  cout << "Enter your choice?";
	cin  >> userChoice;
		
	while (userChoice != "1" && userChoice != "2" && userChoice != "3" && userChoice != "4" && userChoice != "Q" && userChoice != "q")
	  cout << "Error, Please enter a valid choice";
		
	return userChoice;
	}
	
//function getValue

  int getValue(string prompt, int lowerBound, int upperBound)
	{
	
	int values = 0;
	cout << prompt << "\nbetween " << lowerBound << " and " << upperBound << ":";
	cin >> values;
	while (values > upperBound, values < lowerBound)
	  {
	  cout << "\nError: Number is not in range. Enter new number.";
	  }
	return values;
	}

// function doAverage

	void doAverage()
	  {
	  
	  int sum,count;
	  getValue ("\nenter a number", -1000, 1000);
	  cin >> sum;
	  getValue ("\nenter a number", 1, 1000);
	  cin >> count;	   
	  
	  cout << fixed << showpoint;
	  cout << setprecision (3) << calcAverage (sum, count);
	  
	  }



//function calcAverage

  double calcAverage (int sum, int count)
	{
	return sum/count;
	}
	
//function doFactorial()

  void doFactorial()
	{
	int number;
	getValue ("\nenter a number", 0, 10);
	cin >> number;
	cout << calcFactorial (number);
	}

//function calcFactorial

  int calcFactorial(int number)
	{
	int n = number;
	int f = 1;
	for (int i = 2; i < n+1; i++)
	  f *= i;
	return f;
	}
	
//function doMtoNth
  
  void doMtoNth()
	{
	int M,N;
	getValue ("\nenter a number", 1, 10);
	cin >> M;
	getValue ("\nenter a number", 1, 8);
	cin >> N;
	cout << calcMtoNth(M,N);
	}
		
//function calcMtoNth

  int calcMtoNth (int M, int N)
	{
	int Mdx;
	int temp;
  
	temp = M;		  
	for (Mdx = 1; Mdx < N; Mdx++)  
	  temp *= M;
 
	return (temp);
	}

//function doRange

  void doRange()
	{
	int number1,number2,number3,number4;
	getValue ("\nenter a number", 0, 10000);
	cin >> number1;
	getValue ("\nenter a number", 0, 10000);
	cin >> number2;
	getValue ("\nenter a number", 0, 10000);
	cin >> number3;
	getValue ("\nenter a number", 0, 10000);
	cin >> number4;
	
	cout << calcRange(number1,number2,number3,number4);
	}
	
//function calcRange

  int calcRange(int number1,int number2,int number3,int number4)
	{
	
	
	}




It's very likely that there are several more problems I'll have to fix with this code, but I can't see them till I get it to build.

Is This A Good Question/Topic? 0
  • +

Replies To: Menu-Driven Program

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Menu-Driven Program

Posted 21 February 2009 - 02:35 AM

you prototype Menu()
string Menu();

and call it
 userChoice = Menu();


but when you define Menu() you pass it a parameter

  string Menu(string userChoice)
	{
	cout << "Special Purpose Calculator\n";
	  cout << "\n1) Average";
	  cout << "\n2) Factorial";
...


should it be
//function menu

  string Menu()
	{
		string userChoice;
	cout << "Special Purpose Calculator\n";
	  cout << "\n1) Average";
	  cout << "\n2) Factorial";
	  cout << "\n3) M to the Nth Power";
	  cout << "\n4) Range\n";
	cout << "\nQ) Quit\n";
	  cout << "Enter your choice?";
	cin  >> userChoice;

	while (userChoice != "1" && userChoice != "2" && userChoice != "3" && userChoice != "4" && userChoice != "Q" && userChoice != "q")
	  cout << "Error, Please enter a valid choice";

	return userChoice;
	}



Was This Post Helpful? 1
  • +
  • -

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Menu-Driven Program

Posted 21 February 2009 - 07:43 AM

To find the range you first need to have the largest and smallest numbers, as you know. Something like this would work
int calcRange(int a,int b,int c,int d)
{
	int greatest, smallest;
	greatest=a;
	
	if (b>greatest)
		greatest=b;
	if (c>greatest)
		greatest=c;
	if (d>greatest)
		greatest=d;
	
	smallest=a;
		if (b<smallest)
			smallest=b;
		if (c<smallest)
			smallest=c;
		if (d<smallest)
			smallest=d;
	
		
	return greatest-smallest;
}

int main()
{
	int a,b,c,d;
	cin >> a;
	cin >> b;
	cin >> c;
	cin >> d;
	
	cout << calcRange(a,b,c,d);
	
	return 0;
}


It would be easier with an array, so you could just cycle through values and handle larger amounts of numbers.

Hope this helps!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1