-6

I've been staring at this problem for weeks and I have nothing! It doesn't work, I know that much, but I don't know why or what's wrong. I do know that the developer spits out "error: expected expression" regarding the line I highlighted, but really that's just the tip of the iceberg. If anyone at all knows how to fix any small piece of this I would greatly appreciate it!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cs50.h>
#include <ctype.h>

int main(int argc, char *argv[]) 
{

    //Get the key
    if (argc != 2 || atoi(argv[1]) < 0)
    {
        printf("Usage: ./caesar k");
        return 1;
    }

    int key = atoi(argv[1]);
    string plaintex;
    string plaintext = GetString();

    for (int i = 0, n = strlen(plaintext); n < i; i++)
    {
        if (plaintext[i] > 'A' && plaintext[i] <= 'Z')
        {
            plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
        }
    }  
    for (int i = 0, n = strlen(plaintext); n < i; i++)
    {           
        if (plaintext[i] >= 'A' && plaintext[i] >= 'Z')  // Highlighted line
        {
            plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
        }
        else if (plaintext[i] >= 'a' && plaintext[i] < 'z')
        {
            plaintext[i] = (plaintext[i] - 'a' + key) % 26 + 'a';
        }
        else
        {
            printf("%c\n", plaintext[i]);
        }
    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2
if (plaintext[i] >= 'A' && plaintext[i] >= 'Z')

should be

if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
Michael Aquilina
  • 5,352
  • 4
  • 33
  • 38
0

A has a less significant symbol table index than Z. Most likely A == 65 and Z == 90.

if (plaintext[i] >= 'A' && plaintext[i] >= 'Z')

You are saying "if something is larger than 65 and also larger than 90". The logic doesn't make any sense, it is the very same thing as saying "if something is larger than 90".

You probably meant

if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Would that cause a compile error though? It sounded like the code wasn't even compiling. – Dennis Meng Feb 25 '14 at 08:00
  • @DennisMeng It shouldn't, unless you have set the compiler to turn all warnings into errors. The OP doesn't mention which compiler that is used. – Lundin Feb 25 '14 at 08:29
  • I ask because the OP claims that his code is generating a "error: expected expression..." – Dennis Meng Feb 25 '14 at 08:30
  • For clarity, I'd write `'A' <= x && x <= 'Z'`. Or just use the character clasification macros, in this case `isupper()`. – vonbrand Feb 25 '14 at 17:00
  • @vonbrand Please don't edit posts to change the contents or meaning of them, that's quite rude and also completely against SO policies. You really should know better with 3k rep. – Lundin Feb 26 '14 at 07:13