summaryrefslogtreecommitdiffstats
path: root/C++/SumOfIntegers/SumOfIntegers.cpp
blob: b91724343307c490e9ee28c2523107b364a08fe9 (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
// Name: msglm
// Date: 
// Program Name:
// Description: 


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Named constants

int main() {

	//Variable declaration
	int numOfNums;
	int num;
	int sum;
	int iterator;
	char choice;
	//Program title and description for the user
	cout << "Title: Sum of Integers" << endl << "Description: Takes user input and provides of sum of said input. only 2 to 10 numbers and the number must be greater than 0 but less than 1000" << endl;

	do {

		// User input
		do
		{
			cout << "How many integers do you wish to add?: \n";
			cin >> numOfNums;

			if (!cin) {
				cout << "That is not an integer. Program will terminate.\n";
				return 1;
			}

			if (numOfNums < 2) {
				cout << "Addition requires at least two values" << endl;
			}

			if (numOfNums > 10) {
				cout << "This program can only add up to 10 values" << endl;
			}
		} while(numOfNums < 2 || numOfNums > 10 || !cin);

		// Calculations
		for(iterator=1; iterator <= numOfNums; iterator++) {
			do {
				cout << "Enter an integer (in a range from 0 to 1000): \n";
				cin >> num;
				if (!cin) {
					cout << "That is not an integer. Program will terminate.\n";
					return 1;
				}
				if(num<0 || num>1000){
					cout << "Invalid number. Only numbers in the range of 0 to 1000 are accepted\n\n";
				}

			}while(num < 0 || num > 1000);
			sum = sum + num;


			// Output to the screen
			cout << "\nThe sumnation of " << numOfNums <<  " integer values you inputted 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/>.
 */