diff options
author | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
---|---|---|
committer | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
commit | 9d53d8857eaa1c9405894a88ca75bc4657e42f35 (patch) | |
tree | eb1efc1d028b949dd83bb710c68be8eff58f26e7 /C++/SumOfRange/SumOfRange.cpp | |
download | school-code-master.tar.gz school-code-master.tar.bz2 school-code-master.zip |
Diffstat (limited to 'C++/SumOfRange/SumOfRange.cpp')
-rw-r--r-- | C++/SumOfRange/SumOfRange.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/C++/SumOfRange/SumOfRange.cpp b/C++/SumOfRange/SumOfRange.cpp new file mode 100644 index 0000000..9203606 --- /dev/null +++ b/C++/SumOfRange/SumOfRange.cpp @@ -0,0 +1,87 @@ +// Name: msglm +// Date: +// Program Name: +// Description: + + +#include <iostream> +#include <string> +#include <iomanip> +using namespace std; + +// Named constants + +int main() { + + //Variable declaration + int start; + int end; + int num; + int sum; + int iterator; + char choice; + //Program title and description for the user + cout << "Title: Sum of Range" << endl << "Description: Takes two inputs, a start and an end. This program adds all numbers between the start in the end (inclusive) and prints the results." << endl; + + do { + + // User input + do + { + cout << "What is your first integer? \n"; + cin >> start; + cout << "What is your last integer? \n"; + cin >> end; + + if (!cin) { + cout << "That is not an integer. Program will terminate.\n"; + return 1; + } + + + if (start < 0 || end < 0) { + cout << "Values cannot be negative" << endl; + cin.clear(); + cin.ignore(100, '\n'); + cout << "Please try again\n"; + } + + if (start > 50 || end > 50) { + cout << "You cannot numbers greater than 50" << endl; + cin.clear(); + cin.ignore(100, '\n'); + cout << "Please try again\n"; + } + + if (start >= end) { + cout << "The starting number must be less than the ending number" << endl; + cin.clear(); + cin.ignore(100, '\n'); + cout << "Please try again\n"; + } + + } while(start < 0 || end < 0 || start > 50 || end > 50 || !cin || start >= end); + + // Calculations + sum = 0; + + for(start; start <= end; start++) { + sum = start + sum; + } + + + // Output to the screen + cout << "\nThe sum is: " << sum << endl; + + cout << "Do you want to run the program again? (Y/N)" << endl; + cin >> choice; + cout << endl << endl; + } while(choice == 'Y' || choice == 'y'); + cout << "Terminating"; +} +/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + |