ShaneBird
ShaneBird

Reputation: 187

Why is nothing being printed? C Programming

I'm making a program that takes the characters from congress.txt makes them all uppercase, then "shifts them two characters", (A goes to C) (Z goes to B). But nothing is being printed, my main concern is if my arrays are being stored and passed to the different functions properly.

This is what is in congress.txt:

Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.

#include<stdio.h>

int processFile(int *store);
int cipher(int *store, int *code);
int outputCode(int *code);

int main(void){
    int store[300], code[300], i;

    processFile(store);
    cipher(store, code);
    outputCode(code);
    getchar();
    return 0;
}

void processFile(int *store){
    int i, a = 0;
    FILE *f = fopen("congress.txt", "r");

    for (i = 0; a != EOF;){
        fscanf(f, "%c", &a);        //store character in a
        if (a <= 'Z' && a >= 'A'){  //store uppercase letters
            store[i] = a;
            i++;
        }
        if (a <= 'z' && a >= 'a'){  //store lowercase letters as uppercase
            store[i] = a - 32;
            i++;
        }
    }
    i++;
    store[i] = '\0';
}

void cipher(int *store, int *code){
    int i;

    for (i = 0; store[i] != 0; ++i){
        if (store[i] <= 'X' && store[i] >= 'A'){    //tests to see if the letter is between A and X
            code[i] = (char)(store[i] + 2);         //shifts letter by two characters
        }
        if (store[i] >= 'Y' && store[i] <= 'Z'){
            code[i] = (char)(store[i] - 24);        //shifts Y and Z to A or B respectively
        }
    }
}

void outputCode(int *code){
    int i, a, b;
    for (a = 0; code[a] != 0; ++a){
        if (!(a % 50)){                             //makes a newline every 50 characters
            printf("\n");
        }
        for (b = 0; code[a] != 0 && b <= 5; ++b){   //prints chunks of 5 characters then makes a space
            printf("%c", code[a]);
        }
        printf(" ");
    }

}

Upvotes: 0

Views: 903

Answers (1)

Floris
Floris

Reputation: 46375

There are several things wrong with your code - many of which your compiler will complain about.

To start - you don't have return values for functions that are declared int. Just make them void, or return something.

Second - you declare int a; but proceed to use it like a char. Declare it how you use it.

Third - testing for end of file is done with feof(f) not a != EOF.

Fourth - when you output your code you need to increment a, otherwise you get the same value five times:

VVVVVV JJJJJJ KKKKKK UUUUUU KKKKKK UUUUUU UUUUUU

etc.

Fifth - your printing routine doesn't guarantee it will stop - if you have a single '\0' followed by other garbage, you will print more garbage (unless it happens on a multiple of 5). You need to pad your cipher with zeros.

So - working code:

#include<stdio.h>

int processFile(int *store);
int cipher(int *store, int *code);
int outputCode(int *code);

int main(void){
    int store[300], code[300], i;

    processFile(store);
    cipher(store, code);
    outputCode(code);
    printf("\n=====\n\n");
    return 0;
}

int processFile(int *store){
    int i;
    char a = 0;
    FILE *f = fopen("congress.txt", "r");

    for (i = 0; !feof(f) && i<299;){
        fscanf(f, "%c", &a);        //store character in a
        if (a <= 'Z' && a >= 'A'){  //store uppercase letters
            store[i] = a;
            i++;
        }
        if (a <= 'z' && a >= 'a'){  //store lowercase letters as uppercase
            store[i] = a - 32;
            i++;
        }
    }
    store[i]='\0';
    return 0;
}

int cipher(int *store, int *code){
    int i;

    for (i = 0; store[i] != 0; ++i){
        if (store[i] <= 'X' && store[i] >= 'A'){    //tests to see if the letter is between A and X
            code[i] = (char)(store[i] + 2);         //shifts letter by two characters
        }
        if (store[i] >= 'Y' && store[i] <= 'Z'){
            code[i] = (char)(store[i] - 24);        //shifts Y and Z to A or B respectively
        }
    }
    for(; i<300; i++) code[i]=0; // pad with zeros
    return 0;
}

int outputCode(int *code){
    int i, a, b;
    for (a = 0; code[a] != 0; ++a){
        if (!(a % 50)){                             //makes a newline every 50 characters
            printf("\n");
        }

        for (b = 0; code[a] != 0 && b <= 5; ++b){   //prints chunks of 5 characters then makes a space
            printf("%c", code[a++]);
        }
        printf(" ");
    }
return 0;
}

Upvotes: 1

Related Questions