-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunMethod
More file actions
110 lines (100 loc) · 2.86 KB
/
Copy pathrunMethod
File metadata and controls
110 lines (100 loc) · 2.86 KB
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
#include <iostream>
#include <fstream>
//# <string>
using namespace std;
//method that prompts user if they wish to run method inside program,
//user can enter a any of the confirmation inputs listed in the string array
//for loop runs through each element in array are checks if it matches user input
//if true runs methods else false so it doesn't run method
bool runMethod(string method){
string answer;
string confirm[6] = {"yes", "y", "sure", "sounds good", "why not", "kay"};
//string method;
cout << "would you like to run " + method + "?\n: ";
cin >> answer;
for (int i=0; i < 6; i++){
if (answer == confirm[i])
{
cout << "Great! " + method + " will be starting shortly... \n\n";
return true;
}
else if (i == 5)
{
//cout << "it made it to the end, which is: " << i << "\n\n";
return false;
}
else
{
//cout << "this the current answer that is being checked " + confirm[i] + "\n";
//cout << "the answer was " + answer << endl;
//cout << "this is the value of i = " << i << " \n";
}
}
}
//practice with concatenation in C++
//prompts user to enter first name then last name
//and concatenates both strings is with a space in between
void concatenate(){
string firstName;
string lastName;
cout << "type your first name \n: ";
cin >> firstName;
cout << "type your last name \n: ";
cin >> lastName;
string fullName = firstName + " " + lastName;
cout << fullName;
}
//gets length of string
void length(){
cout << endl;
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
}
//uses append to add Doe to end of John string
void append(){
cout << endl;
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
}
//practices try/catch usage
//trys to run divide by zero
//catches and outputs error caught
void tryCatch(){
try
{
// Code that might raise an exception
int divisor = 0;
if (divisor == 0) {
throw std::runtime_error("Divide by zero error");
}
int result = 10 / divisor;
std::cout << "Result: " << result << std::endl;
}
catch (const std::exception& e)
{
// Code to handle the exception
std::cerr << "Exception caught: " << e.what() << std::endl;
}
}
//where the methods actually get run or not
int main (){
if (runMethod("concatenate")){
concatenate();
cout << endl;
}
if (runMethod("append")){
append();
cout << endl;
}
if (runMethod("length")){
length();
cout << endl;
}
if (runMethod("tryCatch")){
tryCatch();
cout << endl;
}
return 0;
}