My professor said to find the logical error within the char shift function, but I can't figure it out. This course is accelerated and so is moving a bit more quickly than I can keep up at times!
#include <iostream>
#include <string>
usingnamespace std;
const string empty_string = "";
char shift(int shift_amount, char original);
string shift(int shift_amount, const string& original);
int main()
{
string original = "Of all things, never to have been born is best!";
for(int shift_amount = 0; shift_amount <= 26; shift_amount++)
{
string shifted = shift(shift_amount, original);
cout << shifted << endl;
}
system("pause");
return 0;
}
char shift(int shift_amount, char original)
{
char shifted;
staticconstint ALPHA_SIZE = 26;
staticconstint upper_base_code = static_cast<int>('A');
staticconstint lower_base_code = static_cast<int>('a');
shift_amount = shift_amount % ALPHA_SIZE;
if(shift_amount < 0)
shift_amount += ALPHA_SIZE;
int base_code;
if(isupper(original))
base_code = upper_base_code;
else
base_code = lower_base_code;
int original_code = static_cast<int>(original);
int original_offset = original_code - base_code;
int shifted_offset = (original_offset + shift_amount) % ALPHA_SIZE;
int shifted_code = shifted_offset + base_code;
shifted = static_cast<char>(shifted_code);
return shifted;
}
string shift(int shift_amount, const string& original)
{
string shifted = empty_string;
int length = original.length();
for(int index = 0; index < length; index++)
shifted += shift(shift_amount, original[index]);
return shifted;
}
Any help would be appreciated.
Note: I would actually prefer that tips and hints be provided, as well as clarification to understand this matter better instead of a straight out answer. Granted, if none of the above can be satisfied, helping directly with the answer wouldn't prove terrible.