summaryrefslogtreecommitdiffstats
path: root/C++/SumOfRange
diff options
context:
space:
mode:
Diffstat (limited to 'C++/SumOfRange')
-rw-r--r--C++/SumOfRange/Sum of Range program instruction.docxbin0 -> 19551 bytes
-rw-r--r--C++/SumOfRange/SumOfRange.cpp87
2 files changed, 87 insertions, 0 deletions
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
--- /dev/null
+++ b/C++/SumOfRange/Sum of Range program instruction.docx
Binary files 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 <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/>.
+ */
+