From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- .../Sum of Range program instruction.docx | Bin 0 -> 19551 bytes C++/SumOfRange/SumOfRange.cpp | 87 +++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 C++/SumOfRange/Sum of Range program instruction.docx create mode 100644 C++/SumOfRange/SumOfRange.cpp (limited to 'C++/SumOfRange') diff --git a/C++/SumOfRange/Sum of Range program instruction.docx b/C++/SumOfRange/Sum of Range program instruction.docx new file mode 100644 index 0000000..abc6187 Binary files /dev/null and b/C++/SumOfRange/Sum of Range program instruction.docx differ 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 +#include +#include +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 . + */ + -- cgit v1.2.3