Skip to main content
2 of 4
improved formatting -please use style50 to assess code formatting
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

CS50 pset2 Caesar's cipher - Compile but doesn't work?!? GetString() above and beyond

I'm working on pset2, Caesar's cipher part. I understand how to proceed to do the encryption and my program compile well. The first line of command allow the user to correctly enter his key. After the first return, user can write some plaintext to encrypt, and then, there appear the problem. My program get block to the "GetString()" command line and ask continually to enter strings...

May someone can help me to solve this problem?!

Thanks for your answer and your time.

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

int main(int argc, string argv[])
{
    if (argc != 2) 
    {
        printf("Usage : ./asciimath key\n");
        return 1;
    }
    
    int key = atoi(argv[1]);

    string plaintext = GetString();
    int length = strlen(plaintext), i = 0;
    char cipher[length];

    while (i < length) 
    {
        if (isalpha(plaintext[i])) 
        {
            if (isupper(plaintext[i])) 
            {
                cipher[i] = ((int)plaintext[i] + key) % 26 + 65;
                i++;
            }
                               
            if (islower(plaintext[i])) 
            {
                cipher[i] = ((int)plaintext[i] + key) % 26 + 97;
                i++;
            }          
            else return 1;
            }
            
            if (isalpha(plaintext[i])) 
            {
                cipher[i] = plaintext[i];
                i++;
            }
        }
    printf("%s\n", cipher);
    return 1;
}