create max, min, and avg functions

Mar 25, 2010 at 8:25am
I am getting a little frustrated with this program. Im a beginner with a little c++ bg. The user has to make a choice to either find the max, min, or avg of a set of numbers the user enters. My problem is the min function does not work. It prompts me to enter numbers but they do not save at all. I have to use ctrl-x to exit the program. My other problem is that when a choice > 3 is entered, it says invalid choice but continues to the avg function

#include <iostream>
#include <limits> // for INT_MIN AND INT_MAX

using namespace std;

// Function prototypes
int max(int n); // find maximum number among n numbers
int min(int n); // find minimum number among n numbers
int avg(int n); // find average number for n numbers

int main()
{
// Declare variables
int choice, count, num;
count = 0;

// Prompt user for choice and read choice
cout << "Enter a choice (0 = Max, 1 = Min, 2 = Avg, and 3 = Exit): ";
cin >> choice;

// While loop continues until invalid choice is entered
while (choice != 3)
{
if (choice < 0 || choice > 3)
{
cout << "Invalid choice. Try again." << endl;
}

cout << "How many numbers of input? ";
cin >> num;

if (choice == 0)
{
cout << "Max is " << max(num) << endl;
}
else if (choice == 1)
{
cout << "Min is " << min(num) << endl;
}
else
{
cout << "Avg is " << avg(num) << endl;
}
cout << "Enter a choice (0 = Max, 1 = Min, 2 = Avg, and 3 = Exit): ";
cin >> choice;
}
cout << "Good Bye" << endl;

return 0;
}

// Function descriptions
// Function int max calculates the maximum number of
// n numbers entered by the user. It returns maxNum
// as the maximum number
int max(int n)
{
int count = 0, num, maxNum = 0;

cout << "Enter " << n << " numbers" << endl;

while (count < n)
{
cin >> num;

if (num > maxNum)
{
maxNum = num;
count++;
}
else
{
maxNum = maxNum;
}
}
return maxNum;
}

// Function int min
// int min calculates the minimum number of a set of
// numbers entered by the user. It returns minNum as
// the minimum number. Parameter int n is the
// number of numbers that the user must enter

int min(int n)
{
int count = 0, num, minNum = 1000;
cout << "Enter " << n << " numbers" << endl;

while (count < n)
{
if (num < minNum)
{
cin >> num;
minNum = num;
count++;
}
else
{
minNum = minNum;
}
}
return minNum;
}

// Function int avg calculates the average of n numbers
// entered by the user. It returns avg as the average
// of the entered numbers

int avg(int n)
{
int avg, sum, count, num;
sum = 0;
count = 0;

cout << "Enter " << n << " numbers" << endl;

while (count < n)
{
cin >> num;
sum = sum + num;
count++;
}

avg = sum / n;
return avg;
}


Mar 25, 2010 at 10:21am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int min(int n)
{
int count = 0, num, minNum = 1000;
cout << "Enter " << n << " numbers" << endl;

while (count < n)
{
          cin >> num;
         if (num < minNum)
         {
               
               minNum = num;
               count++;
         }
       
}
return minNum;
}


also you have to return 0 or simply return will do to break the application when you enter the value greater the 3.

1
2
3
4
5
if (choice < 0 || choice > 3)
{
           cout << "Invalid choice. Try again." << endl;
           return 0;
}

Mar 25, 2010 at 1:33pm
Thank you for fixing my min function. I can't believe it was that simple. However, the return 0; makes my program break but I have to continue looping the choice until they enter a valid one
Mar 25, 2010 at 1:39pm
Never mind. I figured it out. Thank you very much for your help.
Topic archived. No new replies allowed.