The Greatest and the Least of These:
Write a program with a loop that lets the user enter a series of integers, followed by the end of a sentinel -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered. ATTACHED IS MY CODE
1. I must use a constant in my line of code, WHERE CAN I PUT A CONSTANT?
2. How can i fool proof this code, so that when I enter a letter value it won't crash my program.
ATTACHED IS MY CODE
#include<iostream>
using namespace std;
int main()
{
double number, max, min;
cout << "Enter -99 to end the series.";
cout << "\nEnter a number in a series: ";
cin >> number;
min = max = number;
while (number != -99)
{
if (number > max)
max = number;
if (number < min)
min = number;
cout << "Enter a number in the series: ";
cin >> number;
}
cout << "\nThe Largest number is: " << max <<
endl;
cout << "\nThe Smallest number is " << min <<
endl;
return 0;
}
#include<iostream> using namespace std; int main() { const int SENTINEL = -99; double number, max, min; cout << "Enter "<<SENTINEL<<" to end the series."; cout << "\nEnter a number in a series: "; cin >> number; min = max = number; while (number != SENTINEL) { if (number > max) max = number; if (number < min) min = number; cout << "Enter a number in the series: "; cin >> number; } cout << "\nThe Largest number is: " << max << endl; cout << "\nThe Smallest number is " << min << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.