diff options
author | Michael Abed <michaelabed@gmail.com> | 2012-02-12 15:42:43 -0500 |
---|---|---|
committer | Michael Abed <michaelabed@gmail.com> | 2012-02-12 15:42:43 -0500 |
commit | 042acb10595161ac36b47768f7c1e58c553e6b33 (patch) | |
tree | 469bfa7f6ce25a4580169bc5c18a9de70d217970 | |
parent | 986e3aa56c96097bb6993e48859ea6540e9ad7cc (diff) | |
download | ec327-lab1-042acb10595161ac36b47768f7c1e58c553e6b33.tar.gz ec327-lab1-042acb10595161ac36b47768f7c1e58c553e6b33.tar.bz2 ec327-lab1-042acb10595161ac36b47768f7c1e58c553e6b33.zip |
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | Q4.cpp | 49 |
2 files changed, 53 insertions, 1 deletions
@@ -1,5 +1,5 @@ -all: Q1 Q2 Q3 +all: Q1 Q2 Q3 Q4 Q1: Q1.cpp g++ -o Q1 Q1.cpp @@ -10,5 +10,8 @@ Q2: Q2.cpp Q3: Q3.cpp g++ -o Q3 Q3.cpp +Q4: Q4.cpp + g++ -o Q4 Q4.cpp + clean: rm Q1 Q2 Q3 @@ -0,0 +1,49 @@ +/* + * Michael Abed <mgabed@bu.edu> + * + * Lab 1 Problem 4 + * + * Convert a letter based on an offset. + */ + +#include <iostream> + +using namespace std; + +int main() +{ + + char input; + int offset; + int caseswap = 'a' - 'A'; + + cout << "Enter character: "; + cin >> input; // any character will do + + do { // validate input + cout << "Offset (enter 0 to convert case): "; + cin >> offset; + if (offset < 0) + cout << "Offset must be positive" << endl; + if (offset >= 128) + cout << "Offset must be less than 128" << endl; + } while (offset < 0 || offset >= 128); + + char knew; + // calculate new character + if (offset == 0 && input >= 'A' && input <= 'Z') + knew = input + caseswap; + else if (offset == 0 && input >= 'a' && input <= 'z') + knew = input - caseswap; + else if (offset == 0) + knew = input; + else + knew = input + offset; + + if (input + offset > '~' ) // larger is non printing + cout << "Result outside of ASCII range" << endl; + else + cout << "New character: " << knew << endl; + + return 0; +} |