ATHR
ATHR

Reputation: 1

CS50 PSET2 : Simply can't figure out how to implement isalpha so that each command line argument is checked

I simply cannot detect why I can't make isalpha working for the assignment of Vigenere. I've tried so many different inputs. I'm a complete beginner with programming - as in - absolute beginner, so I am wondering whether you have any thoughts. I've also tried to put in argv and argc into isalpha, but it still did not have the output I wanted. The user has to see an error if the keyword that is used is not a letter. This is my code:

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

int main(int argc, string argv[])

{
    
    if (argc < 2)    
    
    {
        printf("ERROR \n");
        return 1;
    }
    

     if (! isalpha (k[i]))
            
    
    else 
    {
        string s = get_string("plaintext: ");
        int k = atoi(argv[1]);
        printf("ciphertext: ");
        for (int i = 0, n = strlen(s); i < n;  i++)
       
           
        if (isalpha(s[i]))
            { printf("ERROR");
              
        if isupper(s[i])
            { 
                printf("%c", (((s[i] + k) - 65) % 26));
            }
         
        else  if islower(s[i])
            
            {
                printf("%c", (((s[i] + k) - 97) % 26)); 
            }
            
        else 
            {
                printf("%c", (s[i]));
            }
          
        }
    }
    
    { 
        printf("\n"); 
    }
}
        

Upvotes: 0

Views: 76

Answers (1)

Jim Rogers
Jim Rogers

Reputation: 5051

Your source code has a number of errors.

 if (! isalpha (k[i]))
        

else 

Two problems with this code fragment:

  • k appears to be intended to be a string, but k is never declared before this usage.
  • Your if statement does nothing if it evaluates to TRUE. It appears that you want to evaluate a command line argument to determine if the first character is or is not an alpha character. Your program does not evaluate the command line argument. It evaluates a string entered by the user which is assigned to s.

You attempt to convert the first command line argument to an integer when its first character may be an alpha character:

int k = atoi(argv[1]);

I am not sure what you think k contains at this point, but whatever you think it is, you are wrong. You probably want to convert all but the first character into an int.

What kind of data is argv[1][0], assuming a command line argument was properly used when starting the program? Are you looking for two particular characters, or will any alpha characters suffice?

Upvotes: 1

Related Questions