Largest numb, smallest num with sentinal

K I am getting one error in my code that just wont let me finish the program. So I am supposed to write a program with 3 choices, A,B,C. A print the largest number out of a defined set of numbers entered by the user, B print the smallest number using a sentinal loop to exit the program, C Exit the program. Here is the code I have. Also how would I convert this to switch statements

//Write a program that displays a menu with the following choices to the user.
// A - Find the largest # with a known quantity of numbers
// B - Find the smallest # with an unknown quantity of numbers
// C - Quit
//Please enter your choice ___

//This menu needs to be repeatedly displayed until the user chooses to quit.

//If A is chosen, you should ask the user how many numbers he wants to enter. If he enters 5, you should read 5 numbers from him.
//You should then display the largest number he entered. Use a for loop to read these and display the largest.

//If B is chosen, you should keep reading numbers no matter what until the user enters -99.
//Then you should display the smallest number he entered not counting the -99.

//Hint - you only need to store the largest or smallest number, not every number that is input.


#include <iostream>

using namespace std;

const int SENTINEL = -99; //Sentinel to exit loop

int main()
{
int numbers; //variables
int large;
int small;
int smallest;
int counter;
int largest;
int next;
char letter;

cout << "A - Find the largest # with a set quantity of numbers. \n"; //program initiation
cout << "B - Find the smallest # with an unknown quantity of numbers. \n";
cout << "C - Quit Program. \n";
cout << "Please enter your choice: ";
cin >> letter; //input

while (letter != 'C'); //start of sentinel controlled while loop
cout << "The letter you entered is " << letter << endl;

if (letter == 'A') //First if loop for A
{
cout << "Enter the amount of positive numbers to be compared. \n";
cin >> numbers;

for (counter = 1; counter < numbers; counter++) //This counts +1 on the counter
{
cout << "Enter a number." << endl;
cin >> large; //stored first number
next = large; //stored number in case of largest
largest = large; //stored for largest overall
}

for (counter = 1; counter < numbers; counter++) //This counts +1 on the counter

cout << "Enter another number." << endl;
cin >> large;

if (large >= next) //loop to determine largest
largest = large;
else
largest = next;

cout << "The largest number entered was. \n";
cout << largest << endl; //output of largest number entered
}

if (letter == 'B') //Second if loop for B
//tried adding another open bracket here for this part of the loop and still got a syntax error for bottom else
cout << "Enter a number, the smallest number entered overall will be displayed. \n To exit enter -99" << endl;
cin >> small;
next = small; //spot to store input number

do
{
cout << " Please enter another number" << endl;
cin >> small; //input of next number entered

if (small > next) //looop for output of smallest number entered
smallest = small;
else
smallest = next;

while (small != -99); //control loop exit
}
else (letter == 'C') //Keep getting a syntax error here don't know why
//I have tried removing this line completly also still get syntax error on else
// tried brackets after the else if
return 0;
}
Last edited on
Here is one implement:
#include <iostream>

using namespace std;

const int SENTINEL = -99; //Sentinel to exit loop

int main()
{
int numbers; //variables
int large;
int small;
int smallest = 0;
int counter;
int largest = 0;
int next;
char letter;
bool quit = false;

while(!quit)
{
cout << "A - Find the largest # with a set quantity of numbers. \n"; //program initiation
cout << "B - Find the smallest # with an unknown quantity of numbers. \n";
cout << "C - Quit Program. \n";
cout << "Please enter your choice: ";
cin >> letter; //input

switch (letter)
{
case 'A':
cout << "Enter the amount of positive numbers to be compared. \n";
cin >> numbers;
cout << "Enter numbers:";
cin >> large;
largest = large;
for (counter = 0; counter < numbers-1; counter++)
{
cin >> next;
if (largest < next)
{
largest = next;
}
}
cout << "The largest number entered was. \n";
cout << largest << endl; //output of largest number entered
break;
case 'B':
cout << "Enter numbers, the smallest number entered overall will be displayed. \n To exit enter -99" << endl;
cin >> small;
smallest = small;
while (next != -99)
{
cin >> next;
if (smallest > next && next != -99)
{
smallest = next;
}
}
cout << "The smallest number entered was. \n";
cout << smallest << endl; //output of smallest number entered
break;
case 'C':
quit = true;
break;
}
cout << endl;
}

return 0;
}
I don't know if it can satisfy your requirement.
you my friend are wonderful I have been working on a solution for this for ummmmm 3 weeks now I couldn't figure out the complete system of it till I got it working and now I get it, much appreciated.
with pleasure, and if you have any question about it, I wish we can think it over together.
If I might add the following comments:

The variables 'large' and 'small' appear to form no useful function.

1
2
cin >> large;
largest = large;


why not just have cin >> largest

Also you have declared a sentinel const int SENTINEL = -99; //Sentinel to exit loop
but in you while loop you hard code the value -99.


Last edited on
Topic archived. No new replies allowed.