CSnewb
CSnewb

Reputation: 147

C crypto program with stdin, stdout, stderr and error message

I am working on what should be a simple c program that takes two args a char either e or d and a key. If e it will encrypt and if d decrypt using the key in both cases. It reads in from stdin and outputs to either stdout or stderr if there is an error. I am getting the warning message

*cypher.c:30:4: warning: passing argument 1 of ‘fputc’ makes integer from pointer without a cast [enabled by default] /usr/include/stdio.h:579:12: note: expected ‘int’ but argument is of type ‘char

The program compiles and encodes, but the decode does not appear to work also it does not throw an error if it is passed a char other then d or e as it should. Any help would be greatly appreciated.

*has been edited to address some of the issues such as, last fputc() is now fputs(), the i++ was added back into last loop, and the if(ende = e) replaced with if(ende == "e"). The error code is no longer an issue, but program functionality appear to be still.

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

main(char ende, char key[150]){

  int e; 
  int  i=0;      
  int c=fgetc(stdin);
  int n=strlen(key);

   if(ende == "e"){
       while(c != EOF){
          c= fgetc(stdin);
          e=(c - 32 + key[i % n]) % 95 + 32;
          fputc( e, stdout);
          i++;
       }
    }
   else if (ende == "d"){
       while(e != EOF){
         e= fgetc(stdin);
         c=(e - 32 - key[i % n] + 3 *95) %95 + 32;
         fputc( c, stdout);
         i++
       }
    }
 else{
   fputs("you broke it",stderr);
   exit (1);
     }
   exit (0);
}

Upvotes: 0

Views: 255

Answers (3)

Lee Duhem
Lee Duhem

Reputation: 15121

Try this:

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

int
main(int argc, char *argv[]) {

    int e, c, n, i;
    char *key, *ende;

    i = 0;
    ende = argv[1];
    key = argv[2];
    n = strlen(key);
    c = fgetc(stdin);

    if (strcmp(ende, "e") == 0) {
        while(c != EOF){
            e=(c - 32 + key[i % n]) % 95 + 32;
            fputc( e, stdout);
            i++;
            c= fgetc(stdin);
        }
    }
    else if (strcmp(ende, "d") == 0) {
        while(c != EOF){
            e=(c - 32 - key[i % n] + 3 *95) %95 + 32;
            fputc( e, stdout);
            i++;
            c= fgetc(stdin);
        }
    }
    else{
        fputs("you broke it",stderr);
        exit (1);
    }
    exit (0);
}

Upvotes: 0

jlucky
jlucky

Reputation: 144

Something wrong with the if (ende = e), maybe is if (ende == e) and else if (ende == d)

Upvotes: 2

Yu Hao
Yu Hao

Reputation: 122383

fputc("you broke it",stderr);

fputc() takes an int as first argument, it should be:

fprintf(stderr, "you broke it");

Also your main() is not standard:

main(char ende, char key[150])

The standard main should be int main(int argc, char* argv[], you may use different names other than argc and argv, but the type still mismatches.

Upvotes: 1

Related Questions