0

My code works for non-alphabetical chars in that it doesn't change them, and it compiles, but it doesn't print out the encrypted letters. What is wrong? I've been staring at it so long I'm seeing stars. Thanks!!!

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
int checkchar(int);

int ifchar;
int main(int argc, string argv[])
{
    if(argc != 2)
    {
        printf("please only enter one interger in the command line\n");
        return 1;
    }

    else
    {
        printf("\n");
        int k = atoi(argv[1]); //casts command line input to int k

        printf("enter messaged to be encrypted: \n");
        string message = GetString();
        int n = strlen(message);

        for(int i = 0; i < n; i++) //iterate through the message
        {
            int charnum = atoi(&message[i]);
            ifchar = checkchar(charnum); //check to see if char is letter

            switch(ifchar)
            {
                case(-1):
                    printf("%c", message[i]);
                    break;
                case(0):
                    charnum -= 39; //if upper then - 39 to get alphabetical numbering
                    int cyph = (charnum + k) % 26;
                    printf("%c", cyph);
                    break;
                case(1):
                    charnum -= 71; //if lower then - 71
                    cyph = (charnum + k) % 26;
                    printf("%c", cyph);
                    break;
            }
        }

    }

    printf("\n");
    return 0;
}


int checkchar(int charnum)
{
    if(charnum > 64 && charnum < 91)
        return 0;
    else if(charnum > 96 && charnum < 122)
        return 1;
    else
        return -1;
}
2
  • Tried to test, but the code posted doesn't compile. Are you sure that this is your current version? – Cliff B Jan 2 '17 at 3:32
  • sorry I edited it now it compiles in my IDE but does nothing to the letters. – Tiangang Huang Jan 2 '17 at 3:35
1

If you had run it through debug you might have noticed that the checkchar() function always returns -1 for a letter. The reason goes back to the following line:

int charnum = atoi(&message[i]);

It appears that you may not understand the way that atoi() works. It will look at the argument and, if it is a string representation of a number, i.e., a string of numeric ascii characters, it will convert them from a string to an actual integer number, and store that integer in charnum. Since an alpha character is not a string representation of a number, it will always return 0.

A char can always be treated as a single byte signed integer. It doesn't need to be converted. You can do math on it directly.

You should also become familiar with isalpha, islower, isupper, tolower, toupper, and all of their cousins. They will be your friends in this exercise.

Finally, an efficiency tip. Wrapping the bulk of the program in the else clause is not necessary. In the event that the first if statement is true, the return will be executed, ending the program. If false, the rest of the code will be executed automatically, so there's no need for an else clause there. Also, when not necessary, adding an else clause increases the execution time and complexity of a program, as well as increasing the chances of accidentally adding a bug to the code.

Some more issues are lying in wait, but they haven't been executed yet, so I'll give you a chance to discover and debug them. There is a bit of rethinking to be done here, so happy coding! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thanks! I already found some other bugs. – Tiangang Huang Jan 2 '17 at 4:54
  • EVERYTHING WORKS NOW!!! – Tiangang Huang Jan 2 '17 at 4:58

You must log in to answer this question.

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