rookie
rookie

Reputation: 3

Faulty if statement (beginner)

I'm on week 2 of cs50 edx, and keep getting the following error when trying to compile:

caesar.c:23:7: error: expected expression if (isalpha(encryptme[p]) = true){ ^

Here's my code:

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

int main(int argc, string argv[])
{
    if (argc !=2) { 
    printf("ERROR: Must pass exactly two command line arguments!\n");
    return 1;}  

   string num = argv[1];
   int i = atoi(num);

   printf("plaintext: ");

   string encryptme = get_string();

   printf("cyptertext: ");

   for (int p = 0; p <= strlen(encryptme); p++)(
      if ( isalpha(encryptme[p]) = true){
      printf("%c",encryptme[p]+i%26);
      }
   )
}

Stared at this for over an hour, have searched for over an hour, please find my undoubtedly idiotic error!

Upvotes: 0

Views: 65

Answers (2)

jdow
jdow

Reputation: 316

Your for loop for (int p = 0; p <= strlen(encryptme); p++)( has it's statements enclosed in () rather than {}

Upvotes: 0

Ray Toal
Ray Toal

Reputation: 88428

You need == instead of =.

The former is an equality test; the latter is an assignment.

It's better not to compare with true however. Best is to just say

if (isalpha(encryptme[p]))

Also, be careful of even using true in C. It is not supported in all versions of C. I'm not sure what is in cs50.h. Maybe true is in there, maybe not....

I'd do it like this:

for (int p = 0; p <= strlen(encryptme); p++) {
    if (isalpha(encryptme[p])) {
        printf("%c", encryptme[p] + i % 26);
    }
}

Cleaned up some spacing and indenting to be more conventional. (Also you should accept jdow's answer since that was where the detection of ( for { was made first.

Upvotes: 1

Related Questions