Hello,
I'm currently learning C++ and have decided to write a Caesar implementation. I have the code working but lack error checking (dealing with spaces, going back to start of alphabet) and am not sure how best to do it. Here's my method so far:
I don't really want a bunch of if statements inside the while loop, but cannot think of any other way. Can somebody suggest something please?Code:#include <iostream> #include <string> using namespace std; string sEncrypt(string& sInput, int iShift); int main(int argc, char *argv[]) { // Declare variables string sInput; //text to encrypt, user inputted int iShift = 0; // shift value, user inputted cout << "Please enter the string to encrypt: "; getline(cin, sInput); //getline used to preserve spaces cout << "\n Please input the shift value: "; cin >> iShift; // pass values to Encryption functon sEncrypt(sInput, iShift); cout << "Encrypted String = " << sInput << endl; system("PAUSE"); return EXIT_SUCCESS; } string sEncrypt(string& sInput, int iShift) //pass variable by reference { int iArrayValue = 0; //simple integer variable used to traverse the string array while(iArrayValue < sInput.length()) { sInput[iArrayValue] = int(sInput[iArrayValue]) + iShift; //use ASCII values to change letter iArrayValue++; } return sInput; }



LinkBack URL
About LinkBacks




