I have a code written to compile for a caesar shift. I need this code to be able to shift "n" spaces, determined through input by the user. I tried to put a cin in both the 1st and 2nd portions of my code to get the "n" amount, but keep getting a compiling error. Everything else works fine, and I have this set to move 13 spaces as of now. How would I go about editing it so that it will shift "n", the amount of spaces the user wants??
#include <iostream>
#include <string>
using namespace std;
char caesar( char );
int main()
{
string input;
do {
getline(cin, input);
string output = "";
for(int x = 0; x < input.length(); x++)
{
output += caesar(input[x]);
}
cout << output << endl;
} while (!input.length() == 0);
}
char caesar( char c )
{
if( isalpha(c) )
{
if (isupper(c))
{
c = (((c-65)+13) % 26) + 65;
}
if (islower(c))
{
c = (((c-97)+13) %26) + 97;
}
}
You can collect an integer from the user. Update the function to take this integer as a second parameter. Instead of a fixed 13, add the integer passed into the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
char caesar( char c, int n )
{
if( isalpha(c) )
{
if (isupper(c))
{
c = (((c-65)+n) % 26) + 65;
}
if (islower(c))
{
c = (((c-97)+n) %26) + 97;
}
}
return c;
}