Hello, I have a school project to make and as I'm a complete beginner in
this, I'd appreciate some help. I have some sort of skeleton for my
program, it can write out help and error codes, but I'm a bit stuck
after that. The project should be simple in execution (ie something that
a beginner can understand and explain to others), there are certain
restrictions (using only getchar and putchar; printf for help and error
messages) and standard libraries. No strtol() etc. allowed.
We're supposed to write a program that encodes/decodes a word using a
chosen key, so that the program can be launched in the command line
like: program -encode key (the user choses his own key). Each character
will be taken as a number in a number system of 36 base and the encoding
will execute so that the characters from the key and the input text will
add up like they were digits from 36 number system. Only characters 0-9
and A-Z are allowed in the input.
Decoding will work in similar way, only the characters will be
substracted.
I'd be really grateful if someone could help me. Thanks!
(Sorry if some things are unclear, English is not my first language)
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
void printHelp()
{
printf( "Help...");
}
const int CBASE = 36;
void doParams(int argc, char *argv[])
{
if (argc == 2 && strcmp("-h", argv[1]) == 0)
{
printHelp();
exit(EXIT_SUCCESS);
}
}
enum tcodes
{
EOK = 0, /
ECLWRONG,
EUNKNOWN,
ENDCODE,
CHELP,
};
const char *CODEMSG[] =
{
/* EOK */
"OK.\n",
/* ECLWRONG */
"Wrong command line parameters! \n",
/* EUNKNOWN */
"Uknown error! \n",
};
typedef struct params
{
bool encode; /* < true - encode, false - decode */
char *key; /* < Encode key */
int state; /* < Status of the program */
} TParams;
void printCode(int code)
{
if (code < EOK || code >= ENDCODE)
{ code = EUNKNOWN; }
FILE *outstream = stdout;
if (code <= EUNKNOWN)
{
outstream = stderr;
}
fprintf(outstream, "%s", CODEMSG[code]);
}
/* Reads characters */
int read_char()
{
int c = getchar();
if ((c >= 'A' && c <= 'Z') || isdigit(c)); /* Checks if '0-9' and
'A-Z' are being used */
else
printf("Your text contains characters that are not allowed!"); /*
Otherwise prints error message. */
}
/* Encodes text */
int encode()
{
int c = getchar();
return (c < 10 ? c + '0' : c + 'A' - 10);
}
/* Decodes text */
int decode()
{
}
/* Prints result */
int print_result()
{
}
/*
*
* MAIN PROGRAM
*
*/
int main(int argc, char *argv[])
{
doParams(argc, argv);
int i = read_char();
int j = encode();
int k = decode();
int l = print_result();
int c;
return EXIT_SUCCESS;
}
/* end of project */
[Non-text portions of this message have been removed]