Hey everyone, I'm writing a program for a class. It's a relatively simple C program that I'm compiling in Dev C++. I'm trying to make it as complex as possible, but I'm just a college freshman and don't know much beyond the basics of programming.
I'd like to enable option to have the user select an input file and output file for the text to be encoded and decoded, as well as an option of which substitution cypher to use (26 in total). I'm a total noob when it comes to file I/O, but here's my code so far. Any tips/recommendations are very much appriciated!

#include "stdio.h"
#include "stdlib.h"

#define ESC 27  		/* standard ascii value for the escape key */

int main()
   {
	   int i=0;			                            // declare a variable named i of type int
      char ch=0;		                            // declare a variable named ch of type char
	   char *alpha="abcdefghijklmnopqrstuvwxyz";  // declare a char pointer to the beginning of the alphabet
      char *key="zyxwvutsrqponmlkjihgfedcba";    // declare a char pointer to the beginning of the key

//      FILE    *infile, *outfile;
      
//      infile=fopen("quad.dat", "r+");
      
      system("cls");
      printf("Cryptology Tool Prototype\n");

      while(ch!=ESC) // do the following while the escape character is not present
         {
            printf("Type the message you would like to encode\n");
            ch=fgetc(stdin); // store a character entered from standard input into ch
         
         	if(isalpha(ch)) // do the following if ch is a letter
               { 
               for(i=0;i<26;i++) // for(INIT ; LOOP WHILE THIS IS TRUE ; MANIPULATE)
                  { 
                  if(alpha[i]==tolower(ch)) // if the character residing at alpha + offset i = ch
                     {  
                     ch=(isupper(ch)) ? (toupper(key[i])):key[i];
                     printf("%i\n");
                     break;
                     } // end of if
               } // end of for
            } // end of if
         fputc(ch,stdout); 
         } // end of while
   return(0);
} // end of main

Recommended Answers

All 10 Replies

It looks like you're trying to use some nonstandard I/O. Compiler/OS?

It looks like you're trying to use some nonstandard I/O. Compiler/OS?

I haven't included the I/O library yet.

Compiler: Dev-C++ 4.9.9.2
OS: Win XP (SP2)

I haven't included the I/O library yet.

Given this statement, I'd say back up a bit and work on learning the language first.

This is outside of standard code:

while(ch!=ESC) // do the following while the escape character is not present

Why do you need to go there?

I know quite enough about C to use file I/O, I meant that I haven't included the #include "stdio.h" library yet.

I'm doing that so if the user presses ESC, it will clear the line he is typing on.

I know quite enough about C to use file I/O, I meant that I haven't included the #include "stdio.h" library yet.

Did you run the program? Then yes, you have. Case in point.

[edit]And generally, it should be

#include <stdio.h>

I'm doing that so if the user presses ESC, it will clear the line he is typing on.

And as I said, attempting to do so requires nonstandard functions that you are not using.

Did you run the program? Then yes, you have. Case in point.

[edit]And generally, it should be

#include <stdio.h>

And as I said, attempting to do so requires nonstandard functions that you are not using.

The ESC key works fine, can we discuss something else?

I guess not if you don't want to learn.

I guess not if you don't want to learn.

What is your problem? Can you offer any real advice or are you just here to be picky?

Short note, since you seem to have io well in hand just do man fopen and you will have what you need... (You can use the same functions for files as for standard input/output if you have a FILE* pointer)...

So I take it you understand how a while loop works, right?

while(ch!=ESC)

So when you press the ESC key, the loop breaks and your program ends, right? [Not for me.]

Or might it be that the ESC key is never seen at all, and that your OS is intercepting the keypress and doing other things? So you don't actually know how to control your main loop, and don't know what you are really getting for character input? [Actually having data would seem to be a prerequisite for encrypting it, wouldn't you think?]

Let's see here...

char ch=0;
/* ... */
ch=fgetc(stdin);

The function fgetc returns an int. It does so for a reason. It's so you can check for EOF . This is a good thing to do for input from stdin, but you can imagine that it might be of interest if you wanted to take input from a file, right?

This code is broken:

printf("%i\n");

What exactly are you printing? Air?

So you've got broken code that appears to "work" correctly. (For you by some act of God?) But it has several bugs in it and you want to make it "more complex"? Why? To confuse yourself more? Or to make it so you look like yet another dumbass masquerading as 1337 coder?

And if you were actually intending "more complex" algorithmically, google for such things. Otherwise, you might find it good to know the language you are using for encryption first. Knowing the language and the platform will make your life much easier when you try to implement more complex algorithms. If you're still stuck at haphazard string input and iffy loop control, forget about it.

This thread seems to be going badly: I'll close it. PM me or another mod if you'd like it reopened and can be more open to advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.