Drew Mortenson
Drew Mortenson

Reputation: 11

Trouble getting a switch function to loop properly

I'm writing a program to 'encrypt' an inputted string of text by using a switch statement to correlate the given character with a symbol, and output that symbol in the place of the character. I put it in a while loop, the idea being that it would loop the full switch function each time until the received character is EOF. On a guess, I believe it is looping through just the first character, because I don't advance the getchar() statement, but I'm not sure how to do that so any help would be greatly appreciated. I say this because if I use return instead of break, it closes the while loop and only takes that first letter, if I use a break then it spams the first 'encrypted' char.

#include <stdlib.h>
#include <stdio.h>

/* C program to encrypt a given text message, assuming all lowercase */

int main() {

    int Input, Encrypted;
    printf("Please type your message\n");

    Input = getchar();

    while (Input != EOF) {
        switch (Input) {
        case 'a':printf("!"); break;
        case 'b':printf("@"); break;
        case 'c':printf("#"); break;
        case 'd':printf("$"); break;
        case 'e':printf("%"); break;
        case 'f':printf("^"); break;
        case 'g':printf("&"); break;
        case 'h':printf("*"); break;
        case 'i':printf("`"); break;
        case 'j':printf("~"); break;
        case 'k':printf("-"); break;
        case 'l':printf("_"); break;
        case 'm':printf("="); break;
        case 'n':printf("+"); break;
        case 'o':printf("["); break;
        case 'p':printf("{"); break;
        case 'q':printf("]"); break;
        case 'r':printf("}"); break;
        case 's':printf(";"); break;
        case 't':printf(":"); break;
        case 'u':printf("|"); break;
        case 'v':printf(","); break;
        case 'w':printf("<"); break;
        case 'x':printf("."); break;
        case 'y':printf(">"); break;
        case 'z':printf("'");break;
        return 0;
        }
    }
    return 0;

}

Upvotes: 1

Views: 55

Answers (1)

Andreas Wenzel
Andreas Wenzel

Reputation: 24726

The simplest solution would be to remove the line

Input = getchar();

and to replace the line

while (Input != EOF) {

with:

while ( (Input=getchar()) != EOF && Input != '\n' ) {

Alternatively, if you find this while condition too confusing, you could also use an infinite loop, instead, like this:

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    printf("Please type your message\n");

    for (;;) //infinite loop, equivalent to while(true)
    {
        int c;

        c = getchar();

        if ( c == EOF || c == '\n' )
            break;

        switch ( c )
        {
           case 'a':printf("!"); break;
           case 'b':printf("@"); break;
           case 'c':printf("#"); break;
           case 'd':printf("$"); break;
           case 'e':printf("%%"); break;
           case 'f':printf("^"); break;
           case 'g':printf("&"); break;
           case 'h':printf("*"); break;
           case 'i':printf("`"); break;
           case 'j':printf("~"); break;
           case 'k':printf("-"); break;
           case 'l':printf("_"); break;
           case 'm':printf("="); break;
           case 'n':printf("+"); break;
           case 'o':printf("["); break;
           case 'p':printf("{"); break;
           case 'q':printf("]"); break;
           case 'r':printf("}"); break;
           case 's':printf(";"); break;
           case 't':printf(":"); break;
           case 'u':printf("|"); break;
           case 'v':printf(","); break;
           case 'w':printf("<"); break;
           case 'x':printf("."); break;
           case 'y':printf(">"); break;
           case 'z':printf("'"); break;
        }
    }

    return 0;
}

Note that most character sets (such as ASCII) store the characters a to z consecutively. With these character sets, you don't need the long switch statement. Instead, you can simplify it to the following:

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    printf("Please type your message\n");

    for (;;) //infinite loop, equivalent to while(true)
    {
        const char map[] = "!@#$%^&*`~-_=+[{]};:|,<.>'";
        int c;

        c = getchar();

        if ( c == EOF || c == '\n' )
            break;

        if ( 'a' <= c && c <= 'z' )
            putchar( map[c-'a'] );
    }

    return 0;
}

Upvotes: 1

Related Questions