summaryrefslogtreecommitdiffstats
path: root/C++/SumOfRange/SumOfRange.cpp
blob: 9203606a127ec6726107bc02a4226210c3a666ad (plain) (blame)
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
// 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/>.
 */