Random C++ codes

Please ignore this. I am just posting codes to this website so that I can copy/paste into my laptop. I am having problems sending them so this is the best alternative I could find. Thank you for understanding.

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
#include <iostream>
#include <string>
using namespace std;

string f(const string & s);
string removeSpaces(const string & s);
string changeCase(const string & s);
string redactDigits(const string & s);
string removeRepetions(const string & s);


int main() {
	cout << "Spaces removed: " << endl;
	cout << removeSpaces("CS 1505 Pierce College") << endl;
	cout << endl;

	cout << "Cases changed: " << endl;
	cout << changeCase("CS 1505 Pierce College") << endl;
	cout << endl;

	cout << "Repititions removed: " << endl;
	cout << removeRepetions("aaabbbccddeefgg") << endl;
	cout << endl;

	cout << "Digits changed: " << endl;
	cout << redactDigits("4 pizzas and 87 beers") << endl;
	cout << endl;

	cout << "All combined: " << endl;
	cout << f("CS 1505 Pierce College") << endl;
	cout << endl;

}

string changeCase(const string & s) {
	string ret;
	for (unsigned pos = 0; pos < s.size(); pos++) {
		if (s[pos] >= 'a' && s[pos] <= 'z')
			ret += (s[pos] - 32);
		if (s[pos] >= 'A' && s[pos] <= 'Z')
			ret += (s[pos] + 32);
		if (s[pos] < 'A')
			ret += s[pos];

	}
	return ret;
}


string removeSpaces(const string & s) {
	string ret;
	for (unsigned pos = 0; pos < s.size(); pos++)
		if (s[pos] != ' ')
			ret += s[pos];
	return ret;
}

string removeRepetions(const string & s) {
	string ret;
	for (unsigned pos = 0; pos < s.size(); pos++)
		if (s[pos] != s[pos+1])
		ret += s[pos];
	return ret;
}

string redactDigits(const string & s) {
	string ret;
	for (unsigned pos = 0; pos < s.size(); pos++) {
		if (s[pos] >= 48 && s[pos] <= 57)
			ret += '#';
		else
			ret += s[pos];
	}
	return ret;
}

string f(const string & s) {
	return changeCase (removeRepetions (redactDigits (removeSpaces("CS 1505 Pierce College"))));
}







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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
using namespace std;

unsigned howMany(char c, const string & s);
string changeCase(const string & s);
string reverse(const string & s);
bool palindrome(const string & s);
bool allLower(const string & s);
string interleave(const string & s1, const string & s2);
void outputPieces(const string & whole);
char mostPopular(const string & s);
bool hasEach(const string & a, const string & b);

int main() {
	cout << "(How Many) Function: " << endl;
	cout << howMany('p', "Pumpkin pie") << endl;
	cout << endl;

	cout << "(Change Case) Function: " << endl;
	cout << changeCase("Blah") << endl;
	cout << endl;

	cout << "(Reverse) Function: " << endl;
	cout << reverse("C++") << endl;
	cout << endl;

	cout << "(Palindrome) Function: " << endl;
	cout << palindrome("1234 racecar 4321") << endl;
	cout << endl;

	cout << "(Lowercase) Function: " << endl;
	cout << allLower("abc def") << endl;
	cout << endl;

	cout << "(Interleave) Function: " << endl;
	cout << interleave("everyone", "HELLO") << endl;
	cout << endl;

	cout << "(Pieces) Function: " << endl;
	outputPieces("ab,cde,f,gh,,i,jk,lm,  1234 and !@#$,blah");
	cout << endl;

	cout << "(Most Popular) Function: " << endl;
	cout << mostPopular("sassafrass") << endl;
	cout << endl;

	cout << "(Has Each) Function: " << endl;
	cout << hasEach("pineapple", "linen") << endl;
	cout << endl;
}

unsigned howMany(char c, const string & s) {
	int times = 0;
	for (int pos = 0; pos < s.size(); pos++) {
		if (s[pos] <= 90 && s[pos] >= 65 || s[pos] <= 122 && s[pos] >= 97)
		{
			if (s[pos] == (c + 32) || s[pos] == (c - 32) || s[pos] == c)
				times++;
		}
		else if (s[pos] == c)
			times++;
	}
	return times;
}

string changeCase(const string & s) {
	string ret;
	for (unsigned pos = 0; pos < s.size(); pos++) {
		if (s[pos] >= 'a' && s[pos] <= 'z')
			ret += (s[pos] - 32);
		if (s[pos] >= 'A' && s[pos] <= 'Z')
			ret += (s[pos] + 32);
		if (s[pos] < 'A')
			ret += s[pos];

	}
	return ret;
}

string reverse(const string & s) {
	string ret;
	ret = string(s.rbegin(), s.rend());
	return ret;
}

bool palindrome(const string & s) {
	bool ret;
	if (s == string(s.rbegin(), s.rend()))
		ret = true;
	else
		ret = false;

	return ret;
}

bool allLower(const string & s) {
	bool ret;
	for (unsigned pos = 0; pos < s.size(); pos++) {
		if (s[pos] >= 'a' && s[pos] <= 'z')
			ret = true;
		else
			ret = false;
	}
	return ret;
}

string interleave(const string & s1, const string & s2) {
	string ret;

	if (s1.size() < s2.size()) {
		for (unsigned pos = 0; pos < s2.size(); pos++) {
			int c = s1.size();
			if (pos < c)
				ret += s1[pos];
			for (unsigned pos1 = 0; pos1 < s2.size(); pos1++) {
				if (pos1 == pos)
					ret += s2[pos1];
			}
		}
	}

	if (s2.size() < s1.size()) {
		for (unsigned pos = 0; pos < s1.size(); pos++) {
			ret += s1[pos];
			for (unsigned pos1 = 0; pos1 < s2.size(); pos1++) {
				if (pos1 == pos)
					ret += s2[pos1];
			}
		}
	}

	return ret;
}

void outputPieces(const string & whole) {
	string ret;
	for (unsigned pos = 0; pos < whole.size(); pos++) {
		if (whole[pos] != ',')
		{
			cout << whole[pos];
		}
		else
			cout << endl;
	}
	cout << endl;
}

char mostPopular(const string & s) {
	char ret;
	int max = 0;
	int count = 0;
	for (unsigned pos = 0; pos < s.size(); pos++) {
		count = 0;
		for (unsigned pos1 = 0; pos1 < s.size(); pos1++) {
			if (s[pos] == s[pos1]) {
				count++;
			}
			if (count > max) {
				max = count;
				ret = s[pos];
			}
		}
	}
	return ret;
}

bool hasEach(const string & a, const string & b) {
	bool ret;
	int count;

	for (unsigned pos = 0; pos < b.size(); pos++) {
		count = 0;
		for (unsigned pos1 = 0; pos1 < a.size(); pos1++) {
			if (b[pos] == a[pos1])
				count++;
		}
		if (count >= 1)
			ret = true;
		else {
			ret = false;
			return ret;
		}
	}

	return ret;
}
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

void a();
void b();
void c();


int main() {
	//Shapes function
	a();
	b();
	c();

	cout << endl;
	cout << "End of program!" << endl;
}

void a() {
	int size;
	cout << "Please enter size of patterns: " << endl;
	cin >> size;

	cout << "Square:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Hollow square:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (row == size || row == 1 || col == size || col == 1)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Hollow square with diaganols:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (row == size || row == 1 || col == size || col == 1 || row == col || col == (size + 1) - row)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Checkerboard:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (col % 2 == 1 && row % 2 == 1 || col % 2 == 0 && row % 2 == 0)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Diamond:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = (size - row); col >= 1; col--) {
			cout << " ";
		}
		for (int colStar = 1; colStar <= 2 * row - 1; colStar++) {
			cout << "*";
		}
		cout << endl;
	}

	for (int row = (size - 1); row >= 1; row--) {
		for (int col = 1; col <= size - row; col++) {
			cout << " ";
		}
		for (int colStar = 1; colStar <= 2 * row - 1; colStar++) {
			cout << "*";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # vertically:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << col;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # horizontally:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << row;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # diagonally from upper left:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << col + row - 1;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # diagonally from upper right:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << size + row - col;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # diagonally from lower left:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << size - row + col;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Same # diagonally from lower right:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			cout << size * 2 - row - col + 1;
		}
		cout << endl;
	}
	cout << endl;


	cout << "Triangle, upper left:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (col >= row)
				cout << col;

		}
		cout << endl;
	}
	cout << endl;


	cout << "Triangle, upper right:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (col >= row)
				cout << size + 1 - col;
			else
				cout << " ";

		}
		cout << endl;
	}
	cout << endl;

	cout << "Triangle, lower left:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= row; col++) {
			cout << size + col - row;
		}
		cout << endl;
	}
	cout << endl;

	cout << "Triangle, lower right:" << endl;
	for (int row = 1; row <= size; row++) {
		for (int col = 1; col <= size; col++) {
			if (col > size - row)
				cout << size * 2 - col + 1 - row;
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Square of letters:" << endl;
	for (int row = 1 - size; row < size; row++) {
		for (int col = 1 - size; col < size; col++) {

			cout << static_cast<char>(64 + size - max(abs(row), abs(col)));
		}
		cout << endl;
	}
	cout << endl;
}

void b() {
	unsigned number, j;
	cout << "Enter a positive number: " << endl;
	cin >> number;

	while (number) {
		if (!cin || number == 0)
			break;

		for (j = 1; j <= number; j++) {
			if (number % j == 0)
				cout << j << " ";
		}
		cout << endl;
		cout << "Enter another number (Non number or 0 to quit): " << endl;
		cin >> number;
	}
}

void c() {
	srand(time(NULL));
	int number, guess;
	char yesNo = 'y';
	guess = 1;

	number = rand() % 100 + 1;
	cout << "Guess the number between [1,100]" << endl;

	while (yesNo == 'y') {
		cin >> guess;
		while (guess) {
			if (guess < number)
				cout << "Your guess is too low." << endl;
			if (guess > number)
				cout << "Your guess is too high" << endl;
			if (guess == number) {
				cout << "You got it. The number was " << guess << "." << endl;
				cout << "Would you like to play again? (y/n)" << endl;
				cin >> yesNo;
				if (yesNo == 'n') {
					cout << "Thanks for playing. Goodbye!" << endl;
					break;
				}
				else if (yesNo == 'y') {
					cout << "Guess the new number: " << endl;
					number = rand() % 100 + 1;
				}

			}
			break;
		}
	}

}
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <iostream>
#include <string>
using namespace std;

int main() {
	const string PASSWORD = "Gandalf";
	string password;
	int counter = 0;


	cout << "Please enter secret password: " << endl;

	while (password != PASSWORD) {
		cin >> password;
		if (password == "Gandalf")
		{
			cout << "User access granted" << endl;
			break;
		}
		if (counter != 4)
		cout << "That is not the password. Please try again: " << endl;
		counter++;
		if (counter == 5)
		{
			cout << "User input failed too many times. User access denied." << endl;
			break;
		}
	}





}





int main() {
	long long int number, output;

	number = 0;
	output = 0;

	while (number < 1000000) {
		number++;
		output += number;
	}

	cout << "The sum of all numbers between 1 and 1,000,000 is: " << output << endl;

		
}





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

int main() {
	double numbers = 0;
	double average = 0;
	double divide = 0;


	do
	{
		cin >> numbers;
		if (numbers == 0)
			cout << "NO DATA" << endl;
		if (!cin && numbers != 0)
		{
			cout << "The average of these numbers is : " << (average / divide) << endl;
			break;
		}
			average += numbers;
			divide++; 
	} while (numbers);
}






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

int main() {
	unsigned numbers;
	unsigned odds = 0;

	cout << "Enter integers. Enter a non-number to quit." << endl;
	cin >> numbers;

	while (numbers)
	{
		cin >> numbers;
		if (numbers % 2 != 0)
			odds++;
		if (!cin)
			break;
	}
	if (!cin)
	cout << "You entered " << odds <<" odd numbers." << endl;
}





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

bool die(const string & msg);

int main() {
	unsigned times;
	double numbers = 1;
	double sum, product;
	sum = 0;
	product = 1;
	unsigned input = 0;

	cout << "Input a number" << endl;
	cin >> times;
	if (!cin) die("Input Failure");
	if (times != 0)
	cout << "Input " << times << " numbers." << endl;

	while (input < times) {
		cin >> numbers;
		input++;
		sum += numbers;
		product *= numbers;
	}
	if (input == times)
		cout << "Sum is: " << sum << ", Product is: " << product << "." << endl;
}




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

bool die(const string & msg);

int main() {
	unsigned k, n;
	unsigned total = 0;

	cout << "Enter 2 positive numbers: " << endl;;
	cin >> k >> n;
	if (!cin) die("Input Failure.");
	if (k < 0) die("Negative Number entered.");

	while (total < n)
	{
		if( total > 0)
			cout << ", ";
		cout << total;
		total += k;
	}

	cout << endl;

	//for (initial value, condition, step size)
	for (total = 0; total < n; total +=k)
	{
		if (total > 0)
			cout << ", ";
		cout << total;
	}

	cout << endl;

	total = 0;
	do {
		if (total > 0)
			cout << ", ";
		cout << total;
		total += k;	
	} while (total < n);

	cout << endl;
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}





namespace std;

bool die(const string & msg);

int main() {
	char input, output, extra;
	int math;

	cout << "Input any single character: " << endl;
	cin >> input;
	if (!cin) die("Invalid Input");

	if (input >= 65 && input <= 90)
		cout << "Your letter is uppercase." << endl;
	if ((input != 90) && (input >= 65) && (input <= 90))
	{
		output = static_cast<char>(input + 1);
		extra = static_cast<char>(input + 33);
		cout << "The next letter is " << output << " or " << extra << endl;
	}
	else
		if (input >= 92 && input <= 122)
			cout << "Your letter is lowercase." << endl;
	if ((input >= 92) && (input <= 122) && (input != 122))
	{
		output = static_cast<char>(input + 1);
		extra = static_cast<char>(input - 31);
		cout << "The next letter is " << output << " or " << extra << endl;
	}

	if (input >= 48 && input <= 57)
	{
		output = static_cast<int>(input - 48);
		math = (output * output * output);
		cout << input << " cubed is " << math << "." << endl;
	}

	if ((input >= 65) && (input <= 90) || (input >= 92) && (input <= 122) || (input >= 48) && (input <= 57))
		cout << " ";
	else
		cout << "The ASCII code for " << input << " is " << static_cast<int>(input) << "." << endl;

}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}





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

bool die(const string & msg);

int main() {
	int left, right, answer;
	char op;
	
	cout << "This program will help solve math equations!" << endl;
	cout << "Example: 1 + 2 or 6 % 5." << endl;
	cout << "Use integers only." << endl;
	cout << "What expression would you like solved? " << endl;

	cin >> left >> op >> right || die("Invalid Input");

	switch (op) {
	case '+':
		answer = left + right;
		break;
	case '-':
		answer = left - right;
		break;
	case '*':
		answer = left * right;
		break;
	case '/':
		if (right == 0) die("Cannot divide by zero.");
		answer = left / right;
		break;
	case '%':
		if (right == 0) die("Cannot mod by zero.");
		answer = left % right;
		break;
	default:
		die("This operator does not exist");
	}
	cout << left << " " << op << " " << right << " " << "=" << " " << answer << endl;
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}




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

bool die(const string & msg);

int main() {
	double hours, pay;
	int widgets;

	cout << "Please enter the number of hours worked." << endl;
	cin >> hours;
	cout << "Now enter the number of widgets sold." << endl;
	cin >> widgets;

	if (!cin) die("Invalid Input");
	if (cin < 0) die("Invalid Input");

	if (widgets <= 50)
	{
		pay = hours * 10;
		cout << "Your pay is: $" << pay << endl;
	}
	else
		if (widgets > 50 && widgets <= 100)
		{
			pay = (hours * 10) + (widgets - 50);
				cout << "Your pay is: $" << pay << endl;
		}
		else
			if (widgets > 100 && widgets <= 300)
			{
				pay = (hours * 10) + ((widgets - 100) * 2) + 50;
				cout << "Your pay is: $" << pay << endl;
			}
			else
				if (widgets > 300)
				{
					pay = (hours * 10) + 450 + ((widgets - 300) * 5);
					cout << "Your pay is: $" << pay << endl;
				}
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}




#include <iostream>
#include <string>
#include <cmath>                              //abcX - acbX - cabC - cba - bacX - bcaX
using namespace std;

bool die(const string & msg)

int main() {
	string first, second, third;
	cout << "Please enter 3 words to be alphabetize." << endl;
	cout << "In order for this to work, type words in all capital letters." << endl;
	cin >> first >> second >> third;

	if (!cin) die("Invalid Input");

	if (first[0] < second[0] && second[0] < third[0])                     //if (abc) then outputs (ABC)
		cout << first << " " << second << " " << third << "." << endl;
	else
		if (first[0] < third[0] && third[0] < second[0])              //if (acb) then (ABC)
			cout << first << " " << third << " " << second << "." << endl;
		else
			if (second[0] < first[0] && first[0] < third[0])          //if (bac) then ABC
				cout << second << " " << first << " " << third << "." << endl;
			else
				if (third[0] < first[0] && first[0] < second[0])          //if (bca) then ABC
					cout << third << " " << first << " " << second << "." << endl;
				else
					if (second[0] < third[0] && third[0] < first[0])           // if (cab) then ABC
						cout << second << " " << third << " " << first << "." << endl;
					else                                                       //if (cba) then ABC
						if (third[0] < second[0] && second[0] < first[0])
							cout << third << " " << second << " " << first << "." << endl;
}           

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}






Last edited on
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
int main() {
	double grade;
	string name, letter_grade;

	cout << "What is your name? (Capitalize the first letter only) " << endl;
	cin >> name;
	cout << "Hello, " << name << "!" << endl;
	cout << "Please enter your numerical grade to see your letter grade. " << endl;
	cin >> grade;

	if (!cin) die("Invalid Input");
	if (cin < 0) die("Invalid Input");

	if (name == "Stefan" || name == "Bob")
		if (grade <= 50)
		{
			letter_grade = 'D';
			cout << letter_grade << endl;
		}
		else
			if (grade < 65 && grade > 50)
			{
				letter_grade = 'C';
				cout << letter_grade << endl;
			}
			else
				if (grade >= 65 && grade < 75)
				{
					letter_grade = 'B';
					cout << letter_grade << endl;
				}
				else
					if (grade >= 75 && grade < 85)
					{
						letter_grade = 'A';
						cout << letter_grade << endl;
					}
					else
						if (grade >= 85)
						{
							letter_grade = 'A';
							cout << letter_grade << endl;
						}
						
	if (name != "Stefan" && name != "Bob")
		if (grade <= 50)
		{
			letter_grade = 'F';
			cout << letter_grade << endl;
		}
		else
			if (grade < 65 && grade > 50)
			{
				letter_grade = 'D';
				cout << letter_grade << endl;
			}
			else
				if (grade >= 65 && grade < 75)
				{
					letter_grade = 'C';
					cout << letter_grade << endl;
				}
				else
					if (grade >= 75 && grade < 85)
					{
						letter_grade = 'B';
						cout << letter_grade << endl;
					}
					else
						if (grade >= 85)
						{
							letter_grade = 'A';
							cout << letter_grade << endl;
						}

}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}




#include <iostream>
#include <math.h>
#include <string>
using namespace std;

bool die(const string & msg);

int main() {
	unsigned x, ax, xa, bx, xb, cx, xc, dx, z;
	cout << "Please enter a 4-digit integer: " << endl;
	cin >> x;

	if (!cin) die("Invalid Input.");
	if (x > 9999) die("Number too large.");
	if (x < 1000) die("Number too small");

	ax = x / 1000; //generates first digit
	xa = x % 1000; //generates remainder 
	bx = xa / 100; //generates second digit
	xb = xa % 100; //generates remainder
	cx = xb / 10;  //generates third digit
	xc = xb % 10;  //generates remainder
	dx = xc / 1;   //generates fourth digit
	z = (ax) + (bx * 10) + (cx * 100) + (dx * 1000);

	cout << x << " squared is " << x*x << endl;
	cout << z << " squared is " << z*z << endl;

}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}





#include <iostream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;

bool die(const string & msg);

int main() {
	unsigned seconds, S, M, MM, H, HH;
	cout << "Please enter the total number of seconds: " << endl;
	cin >> seconds;

	if (!cin) die("Invalid Input");
	if (seconds <= 0) die("Invalid Input");

	H = seconds / 3600;
	HH = seconds % 3600;
	M = HH / 60;
	MM = HH % 60;
	S = MM / 1;
	cout << H << ":" << M << ":" << S << endl;
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}



#include <iostream>
#include <math.h>
#include <string>
using namespace std;

bool die(const string & msg);

int main() {
	double A, B, C, D, E, X;
	cout << "Please enter 5 test scores you'd like to find the average of." << endl;
	cout << "Decimals are an acceptable input" << endl;
	cout << "Test score 1: " << endl;
	cin >> A;
	cout << "Test score 2: " << endl;
	cin >> B;
	cout << "Test score 3: " << endl;
	cin >> C;
	cout << "Test score 4: " << endl;
	cin >> D;
	cout << "Test score 5: " << endl;
	cin >> E;

	if (!cin) die("Input Failure");
	if (A <= 0) die("One or more inputs are negative.");
	if (B <= 0) die("One or more inputs are negative.");
	if (C <= 0) die("One or more inputs are negative.");
	if (D <= 0) die("One or more inputs are negative.");
	if (E <= 0) die("One or more inputs are negative.");

	double AA, BB, CC, DD, EE;
	AA = static_cast<int> (A + .5);
	BB = static_cast<int> (B + .5);
	CC = static_cast<int> (C + .5);
	DD = static_cast<int> (D + .5);
	EE = static_cast<int> (E + .5);
	X = ((AA + BB + CC + DD + EE) / 5);
	cout << "The average (after each score is rounded to the nearest integer) is: " << X << "." << endl;
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}



#include <iostream>
#include <string>
#include <math.h>
using namespace std;

bool die(const string & msg);

int main() {
	int $;
	cout << "Hello! Let me sort out your money for you." << endl;
	cout << "Please enter the amount of cents that you would like to turn into coins." << endl;
	cin >> $;
	int Q = $ % 25;
	int QQ = $ / 25;
	int D = Q % 10;
	int DD = Q / 10;
	int N = D % 5;
	int NN = D / 5;
	int PP = N / 1;

	if (!cin) die("Invalid Input.");
	if ($ <= 0) die("Non existant amount.");

	cout << "The following is minimum number of coins you'd have to equal " << $ << " cents:" << endl;
	cout << "Quarters: " << QQ << endl;
	cout << "Dimes: " << DD << endl;
	cout << "Nickles: " << NN << endl;
	cout << "Pennies " << PP << endl;
}

bool die(const string & msg) {
	cout << "Self destruct initiated: " << msg << endl;
exit(EXIT_FAILURE);
}



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

bool die(const string & msg);

int main() {
	double Q, D, N, P;
	double QQ, DD, NN, PP, Total;
	cout << "Welcome to your personal piggy bank counter!" << endl;
	cout << "" << endl;
	cout << "Let me help you count how much money you have based on the coins you have." << endl;
	cout << "" << endl;
	cout << "How many quarters do you have? ";
	cin >> Q;
	cout << "How many dimes do you have? ";
	cin >> D;
	cout << "How many nickles do you have? ";
	cin >> N;
	cout << "How many pennies do you have? ";
	cin >> P;


	if (Q + D + N + P > 1000) die("Too many coins.");
	if (!cin) die("Bad Input.");

	QQ = Q * .25;
	DD = D * .10;
	NN = N * .05;
	PP = P * .01;
	Total = QQ + DD + NN + PP;
	cout << "Quarters: " << Q << endl;
	cout << "Dimes: " << D << endl;
	cout << "Nickles: " << N << endl;
	cout << "Pennies: " << P << endl;
	cout << "You have $" << Total << " in your piggy bank!" << endl;

}

bool die(const string & msg) {
	cout << "Self destruct initiated: " << msg << endl;
	exit(EXIT_FAILURE);
}


Please leave this here for a day or so! I am copying over into my laptop when I arrive back home. Thank you admins!
Just so you know there are in fact easier methods for next time:
pastebin.com
github.com
http://paste.ofcode.org/ - expires in 1 week
emailing the file to yourself
I tried emailing but it wasn't copying correctly. Thanks though! I will definitely give that a try next time! I already took the time to copy/paste all of this though so I will just use this haha. Thanks though :)
If you have a usb stick, you could always just copy paste the files and physically move them...
Topic archived. No new replies allowed.