Menu and finding largest number

Hi. I'm trying to learn c++, but I'm still pretty noobish. I need to create a program that displays menu options and displays the largest number, andthe smallest number based on user input. The code I have so far is:

#include <iostream>

using namespace std;

const int SENTINEL = -99;

// Declaring variables
char letter; //letter chosen from the menu
int i; //quantity of numbers user will enter
int counter; //loop control variables
int number; // variable to store new number
int largest; // variable to store largest number
int smallest;
int number1;
int largest2;
int number2;
int smallest2;
int smallestNumber (int number2, int smallest2);
int biggestNumber (int number1, int largest2); // << declare parameters as ints

int main ()
{

// Initializing variables
largest = -999999999; // << changed these so they will fit an int
smallest = 999999999;
// 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;
for (counter = 1; counter <= i; counter++)
{
cin >> number;

if ( biggestNumber (number, largest)) // () added
largest = number;
}
cout << "The largest number you entered is" << " " << largest;
cout << " ";
cout << endl;
break;

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

cout << endl;

return 0;
}

// your function goes here not inside main()
int biggestNumber (int number1, int largest2)
{
if(number1 > largest2) return number1; else return largest2;
}

int smallestNumber (int number2, int smallest2)
{
if(number2 < smallest2) return smallest2; else return smallest2;
}
-----------------
The program loads and runs properly. The problem is, it doesn't display the correct largest or smallest number. I can't figure out where my problem is. Thanks for any help.
Jason
You are calling biggestNumber and smallestNumber in your if() statements and treating the return value of those functions as a boolean flag indicating whether or not to set biggest or smallest. But the functions return the larger of the two values.
Hey Jason, this is Dusty. Did you understand what you needed to do, because I'm still in the dark about this. I'll post mine and it looks like trash right now.....


#include <iostream>
#include <iomanip>
#include <cassert>
#include <string>

using namespace std;

const int SENTINEL = -999;

int main()
{
int limit;
int num1;
int num2;
int larger;
int smaller;
int counter;
char letter;

cout << "A: Find the largest # of known quanity of numbers.\n";
cout << "B: Find the smallest # of unknown quanity of numbers.\n";
cout << "C: Quit program.\n";
cin >> letter;
//Asking the user for the letter to guide them through the menu.

switch(letter)
{ //Made it so the user could enter capital or lowercase letters.
case 'A':
case 'a':
cout << "\nYou have chosen A. \n";
cout << "Now enter how many numbers in your list: \n";
cin >> limit;
cout << endl;

cout << "Your entered " << limit << " as your limit. \n";
cout << "Now enter in that many numbers, separated by spaces." << endl;

for (counter = 1; counter < limit; counter++)
{
cin >> num1;
if (larger)
larger = num1;
}

cout << "The largest number you entered is... " << num1;

break;

case 'B':
case 'b':
cout << "You have chosen B. \n";
cout << "Enter in the numbers you want \n";
cout << "to find the lowest number separated by spaces \n";
cout << "and end your statement with -999 \n";
cin >> num2;
counter = 0;

do
{
cin >> num2;
if (num2 != SENTINEL)
num2 = num2 + 0;
counter++;
}
while (num2 != SENTINEL);
cout << "\nThe smallest number is... ";
cout << num2;

break;

case 'C':
case 'c':
cout << "You have chosen C.\n";
cout << "Take care and have a nice day. :-)";

cin.get();
cin.get();
return 0;

break;
//Added in the "cin.get()" 2 times and the "return 0" to close on "C".

default:
cout << "The letter you entered is invalid, please exit and re-try again.";

break;
//I can't quite figure out how to loop it back to the main menu if entered wrong.
}
cout << endl;
cout << "Press any key to exit program. :-)" << endl;

cin.get();
cin.get();

return 0;
}

I don't want someone to solve it for me. I'd just like some help going in the right direction. Thanks,
Dusty
Last edited on
The two of you are both going at it the right way, but neither of you are quite right.

If you have an integer variable "largest" which is initially set to -9999999, for example, then each time through your loop, after the user enters a number "number", you should set largest to number if and only if number is greater than largest. Can you translate the above statement to code?

If not, let me rephrase it: if number is greater than largest, then set largest equal to number (otherwise largest is already largest; you don't have to assign largest to itself).

Topic archived. No new replies allowed.