I simply can't find this logic error.
Oct 13, 2011 at 1:42am Oct 13, 2011 at 1:42am UTC
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!
Here is the code:
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
#include <iostream>
#include <string>
using namespace 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;
static const int ALPHA_SIZE = 26;
static const int upper_base_code = static_cast <int >('A' );
static const int 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.
Topic archived. No new replies allowed.