2

I am getting this error: expression result unused and I don't know what that means. I use the code it's pointing to (green underline), just like I do in my Caesar cipher which works:

The other posts on here about this error are not helping me, because my code seems to be correct (to me).

Here is a portion of my code:

    string key = argv[1];
    string plain_text = GetString();
    int n = strlen(plain_text);
    int m = strlen(key);

    for (int i = 0, j = 0; i < n, j < m; i++, j++)
    {
        if (islower(plain_text[i]) && isalpha(plain_text[i]))
        {   
             // Change ASCII to alphabet, apply wrap formula, then back to ASCII
             plain_text[i] = (((plain_text[i] - 'a') + (key[j] - 'a')) % 26) + 'a';
        }
        else if (isupper(plain_text[i]) && isalpha(plain_text[i]))
        {
             // Change ASCII to alphabet, apply wrap formula, then back to ASCII
             plain_text[i] = (((plain_text[i] - 'A') + (key[j] - 'A')) % 26) + 'A';
        }

    } // End of for loop
2

If you want to iterate in accordance with multiple conditions, then you should do something like this

for ( initialization ; test_condition ; re-initialization )
{
      // body of the loop
}

The test_condition should be a logical expression which should either return true or false. For eg.

test_condition = check1 *logical operator* check2 *logical operator* check3...    and so on

However , is not a logical operator. See how to do check multiple conditions simultaneously here.

The thing i < n, j < m is not a valid logical expression, you need something like i < n || j < m (if you want to execute the loop for even any one condition to be true) or i < n && j < m (if you want the loop to execute only and only iff both the conditions are true).

| improve this answer | |
  • Thanks sinister! I was not aware I could not have 2 expressions separated by a comma. I saw that you can have that in the 1st and last condition, e.g. int i = 0, j = 0; and here too i++, j++, so I assumed it was okay to have it in the middle. But thanks, I changed it an && and that fixed it. Now I'm having a new problem though, a Segmentation fault (core dumped), so I will have to figure out that one. :/ – Azurespot Aug 12 '14 at 4:34
  • 1
    That's the most horrible thing in a programmer's life. Hint-> You accessed an empty memory location. Good luck. – sinister Aug 12 '14 at 4:46
  • Ah thanks, I knew it had to do with memory, but didn't realize it was an empty memory issue. Lol, and yes... one problem leads to the next. ;) – Azurespot Aug 12 '14 at 5:15
  • 1
    Hey I fixed it!!! Turns out my error was in some argv[1] code I had. I tried to use isalpha(argv[i]) to make sure the key entered is only letters, but that would not check for the whole string (which is what our key is in this exercise), it would only check for one character in the key. So I had to create a for loop to iterate through argv[], using strlen(argv[1]) as my middle loop condition, so it would check each character separately. – Azurespot Aug 12 '14 at 5:38

You must log in to answer this question.

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