0

Please I would like some help to understand what I am doing wrong to have those errors with check50?

:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "barfoo" as "caqgon" using "baz" as keyword
   \ expected output, but not "ciphertext: caqgv\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "ciphertext: CaQgv\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
   \ expected output, but not "ciphertext: CAQGV\n"
:( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword
   \ expected output, but not "ciphertext: xoqv!$?\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "ciphertext: xoqv, dl imyp!\n"
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword



Here is my code : 

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

int main(int argc, string argv[])
{
     //only 1 string is allowed next to the command
    if (argc != 2)
    {
        printf("error\n");
        return 1;
    }

     //keyword is only alphabetic
    for (int i = 0; i < strlen(argv[1]); i++)  
    {
        if (!isalpha(argv[1][i]))
        {
            printf("error\n");
            return 1;
        }

    }
    {
           printf("plaintext: ");
    }

    string plntxt = get_string();      
    if (plntxt != NULL)
    {
        printf("ciphertext: ");
    }

    int k = 0;
    int c = 0;
    k = k % strlen(argv[1]);

    for (int j = 0,  m = strlen(plntxt); j < m; j++, k++)
    {

        if (isalpha(plntxt[j]))
        {
            if (islower(plntxt[j]) && islower(argv[1][k]))
            {
                c = ((plntxt[j] - 97) + (argv[1][k] - 97))% 26;
                int aletter = c + 97;
                printf("%c", (char)aletter);
            }

            else if (islower(plntxt[j]) && isupper(argv[1][k]))
            {
                c = ((plntxt[j] - 97) + (argv[1][k]) - 65)% 26;
                int bletter = c + 97;
                printf("%c", (char)bletter);
            }

            else if (isupper(plntxt[j]) && islower(argv[1][k]))
            {
                c = ((plntxt[j] - 65) + (argv[1][k] - 97))% 26;
                int cletter = c + 65;
                printf("%c", (char)cletter);
            }

            else if (isupper(plntxt[j]) && isupper(argv[1][k]))
            {
                c = ((plntxt[j] - 65) + (argv[1][k] - 65 ))% 26;
                int dletter = c + 65;
                printf("%c", (char)dletter);
            }
        }  
        else 
        {
            printf("%c", (char)plntxt[j]);
        }
    }
    printf("\n");
    return 0;
}
3
  • It s works, I have to change all the int letter to be different :) But when I run check50 I have this error message : :( encrypts "barfoo" as "caqgon" using "baz" as keyword \ expected output, but not "ciphertext: cbsgpp\n" :( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "ciphertext: CbSGpp\n" :( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "ciphertext: CBSGPP\n" :( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword \ expected output, but not "ciph
    – Genevie
    Apr 29, 2017 at 20:54
  • if I put the for loop, I have a plaintext printed as many time as I have a char in my string :( please inlight me :)
    – Genevie
    Apr 29, 2017 at 20:58
  • here what I get : ~/workspace/pset2/ $ ./vigenere abc0 plaintext: plaintext: plaintext: error
    – Genevie
    Apr 29, 2017 at 21:02

2 Answers 2

0

it is likely due to the fact that you made a typo error. Remove the ) before the && as this is causing the if condition to terminate at that point, if you know what i mean.

3
  • thanks, now I have an error with the unidentified [i] but I declared it in the keyword section
    – Genevie
    Apr 28, 2017 at 12:37
  • 1
    You did but i only exists inside that for loop i.e., for (int i = 0; i < 10; i++) { printf("%d\n", i); } printf("%d\n", i); will not compile because i is only in-scope during the loop.
    – chad
    Apr 28, 2017 at 16:27
  • Thanks. But iif it's the same variable I want to use, I mean I want to use the variable from the keyword which is the same used in the loop. How can i do that than ?
    – Genevie
    Apr 29, 2017 at 17:08
0

Thansk everybody. So i had to move the cycle into the for loop and add up the ++.

You must log in to answer this question.

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