1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Assignment 3.cpp
//Write a program with a loop that lets the user enter a series of integers, followed by 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.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Declaring variables used
int number, minNumber, maxNumber, i;
//Declaring the sentinel value that terminates the program
const int SENTINEL = -99;
//User inputs a series of integers
cout << "Enter a series of integers (then enter -99 to quit):\n";
cin >> number;
//Checks the input from the user
if ( number != SENTINEL )
{
//if only one number is put into the series, the max and min will be equal
maxNumber = number;
minNumber = number;
//user gets the chance to put in another number into the series,
//or the user can type -99 (SENTINEL) to terminate program
cin >> number;
//if the user decides to terminate the program with -99 (SENTINEL) then
// the program will display the largest and smallest numbers
if (number == SENTINEL )
{
cout << "The largest number was: " << maxNumber << endl;
cout << "The smallest number was: " << minNumber << endl;
cout << endl <<endl;
system("pause");
return 0;
}
//otherwise, if the user decides to enter another number in the series,
//the following executes
else
{
do{
//The program determines whether the number is a new maximum and
//stores the new max into the maxNumber variable, while not equal to -99
if ( number > maxNumber && number != SENTINEL )
{
maxNumber = number;
}
//The program determines whether the number is a new minimum and
//stores the new min into the minNumber variable, while not equal to -99
if ( number < minNumber && number != SENTINEL )
{
minNumber = number;
}
cin >> number;
} while (number != SENTINEL );
//After the series of numbers are input by the user, the user will terminate the
//program with -99 (SENTINEL) and the program will display the largest and
//smallest numbers of the series
if ( number == SENTINEL )
{
cout << "The largest number was: " << maxNumber << endl;
cout << "The smallest number was: " << minNumber << endl;
cout << endl <<endl;
system("pause");
return 0;
}
}
}
//If the user inputs -99 (SENTINEL) without inputting other numbers first,
//the program will display an error message and will ask the user whether
// or not they wish to try the program again .
if ( number == SENTINEL )
{
string answer;
cout << "\n\nError. No data entered." << endl;
cout << "Try again? ";
cin >> answer;
//Basically if the user wishes to try again, the main part of the
//program is repeated within this if statement
if ( answer == "Yes" || answer == "yes" || answer == "Y" || answer == "y")
{
//User inputs a series of integers
cout << "Enter a series of integers (then enter -99 to quit):\n";
cin >> number;
//Checks the input from the user
if ( number != SENTINEL )
{
//if only one number is put into the series, the max and min will be equal
maxNumber = number;
minNumber = number;
//user gets the chance to put in another number into the series,
//or the user can type -99 (SENTINEL) to terminate program
cin >> number;
//if the user decides to terminate the program with -99 (SENTINEL) then
// the program will display the largest and smallest numbers
if (number == SENTINEL )
{
cout << "The largest number was: " << maxNumber << endl;
cout << "The smallest number was: " << minNumber << endl;
cout << endl <<endl;
system("pause");
return 0;
}
//otherwise, if the user decides to enter another number in the series,
//the following executes
else
{
do{
//The program determines whether the number is a new maximum and
//stores the new max into the maxNumber variable, while not equal to -99
if ( number > maxNumber && number != SENTINEL )
{
maxNumber = number;
}
//The program determines whether the number is a new minimum and
//stores the new min into the minNumber variable, while not equal to -99
if ( number < minNumber && number != SENTINEL )
{
minNumber = number;
}
cin >> number;
} while (number != SENTINEL );
//After the series of numbers are input by the user, the user will terminate the
//program with -99 (SENTINEL) and the program will display the largest and
//smallest numbers of the series
if ( number == SENTINEL )
{
cout << "The largest number was: " << maxNumber << endl;
cout << "The smallest number was: " << minNumber << endl;
cout << endl <<endl;
system("pause");
return 0;
}
}
}
}
//If the user does not wish to try the program again, then the preogram terminates
else if ( answer == "No" || answer == "no" || answer == "N" || answer == "n")
{
cout << "Okay cool. Thanks for trying."<< endl << endl;
cout << "PROGRAM TERMINATED.";
cout << endl <<endl;
system("pause");
return 0;
}
//If the user does not enter Yes/No or Y/N, without first letter case mattering,
//then the program terminates
else
{
cout << "Not a good enough answer. Guess you don't want to run the program again.\n Goodbye." << endl << endl;
cout << "PROGRAM TERMINATED.";
cout << endl <<endl;
system("pause");
return 0;
}
}
cout << endl <<endl;
system("pause");
return 0;
}
|