1

I have been working through the caesar code and all is well until I start playing about with the isupper function. This is where the problem is as I've been compiling and running the rest of the program no problem:

int k = atoi(argv[1]);

string p = GetString();
for (int j = 0, n = strlen(p); j < n; j++)
{
    if (isupper('p[j]'))
    {
        int upper = ('p[j]' - 65);
        upper = ((upper + k)%26);
        upper = (upper + 65);
        printf("%c", upper);
    }
}

When I run the program it awaits the string from the user, I input any letter and get back Segmentation fault (core dumped). I know that this is to do with touching areas of memory I'm not supposed to, one of the likely solutions was that I was trying to call toupper on a string instead of a char, but p[j] should definitely be a character in the plaintext. To test this I replaced both instances of 'p[j]' with 'A', and it compiles and runs no problem. What is the problem with calling toupper on 'p[j]'? If p is the plaintext string, shouldn't 'p[j]', with quotes, be a character in the string?? Any help appreciated, thanks!

1

Your seg fault is probably because you are putting the variable p[j] in single quotes. Single or double quotes indicate a literal character or string, not a variable.

The seg fault is because the downstream effect is that you're feeding a string into a function that takes only one char.

That should fix that issue. I see others, but those will be separate questions. ;-)

If this answers your question, please click the check to accept. Let's keep up on forum maintenance. ;-)

1
  • OK so I'm just feeding it the literal string p[j] instead of what's actually in the box, doh...sorted that bit, thanks :) Smoothing out the rest now...
    – Ailsa
    Oct 25 '15 at 1:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .