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);       
    }
}

2 Answers 2

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
    Jun 25, 2020 at 14:10
0

Brother, don't mind, I would like to suggest you that your program may works fine after your bug fixes but your code isn't well designed.

I think that good design doesn't mean that you have written comments but in fact it is the matter that how others can understand your code in how much amount of time because this thing matters a lot when you actually move to professional field of CS.

Do consider my advice as a positive and don't take it as a criticism :)

3
  • Why would you demand or expect the code to be 'good design' and why would you compare it to the professional field of CS when someone is doing problem set 2 on Week 3 from introductory CS course? At this stage code either works or doesn't. Everything else comes with the experience... Jun 25, 2020 at 13:19
  • Okay brother, np well I just thought that I should share some guidance with you which maybe very beneficial for you later on in this field :) Jun 25, 2020 at 13:27
  • Don't worry I will take it as good advice I even asked a question about it on a CS50 forum and they told me to don't worry a lot about clean code the first weeks and I said that I would come back to make more efficient code as a review when I advance on the course but if I have some time I will try to make it a little cleaner, Thanks!
    – DZV31
    Jun 25, 2020 at 14:09

You must log in to answer this question.

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