0

Help, please. The program works fine until I want to add the loops and when I do that the if statements inside it won't work and it will just ask for the key and will output the same text as the input but if instead I don't use the loops and set i = 0 (a possible value), it will work just fine but only with the first letter because I still need the loops to move onto the different characters in the string. Here is the code:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
    if (argv[1] == '\0')
    {
        printf ("Usage: ./caesar key\n");
        return 0;
    }
    int d = 0;
    while (strlen(argv[1]) > d)
    {
        if (argv[1][d] < 48 || argv[1][d] > 57 )
        {
            printf("Usage: ./caesar key\n");
            return 0;
        }
        else
        {
        d++;
        }
    }
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
    }
    else
    {
            char *p;
            //ArgvX is the same value as argv[1] but converted to an int
            int ArgvX = strtol(argv[1], &p, 10);
            string Plaintext = get_string("Plaintext: ");
            for (int i = 0;Plaintext[i] < strlen(Plaintext) ;i++)
      {     
                //These are the parameters I did using ASCCI values of uppercase letters.
                while (Plaintext[i] <= 90 && 65 <= Plaintext[i]) 
            {   
                if (Plaintext[i] + (ArgvX % 26) > 90)
                {
                // This is the function if the value exceeds the ASCCI for uppercase letters.
                Plaintext[i] = (Plaintext[i] - 26 + (ArgvX % 26));
                }
                else
                {
                Plaintext[i] = (Plaintext[i] + (ArgvX % 26));
                }
            }        
                //These are the parameters I did using ASCCI values of lowercase letters.
                while (Plaintext[i] <= 122  && 97 <= Plaintext[i])
            {             
                if (Plaintext[i] + (ArgvX % 26) > 122)
                {
                // This is the function if the value exceeds the ASCCI for lowercase letters.
                Plaintext[i] = (Plaintext[i] - 26 + (ArgvX % 26)); 
                }
                else 
                {
                Plaintext[i] = (Plaintext[i] + (ArgvX % 26));
                }
           }       
        }  
            printf("%s\n",Plaintext);       
    }
}

1 Answer 1

0

Lets try to peek into the first for loop. We assume that code in this for loop gets executed once for every character in the Plaintext string - but is it true? :)

Try placing printf function in the first row of the for loop, like next:

printf("Hello from for loop!. \ni: %d Plaintext len: %lu \n, i, strlen(Plaintext));

This way you will see how much times code inside the for loop gets executed and will have values of i and strlent(Plaintext) printed out to the console for you.

1
  • Thanks a lot, I forgot about using printf as a debugging tool and now I have just used check50 and it works perfectly!
    – DZV31
    Commented Jun 25, 2020 at 14:10

You must log in to answer this question.

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