Question
Please correct this C coding program. Their are mistakes in the code and I need them fixed. The direction to the coding program are below
Please correct this C coding program. Their are mistakes in the code and I need them fixed. The direction to the coding program are below the source code of the prgram. One of the main issues is that the code gets stuck in a never ending loop when changing the shift value.
Source code:
#include
#include
#include
int getShift( void )
{
int i;
printf ("Enter new shift value: ");
scanf ("%d", &i);
printf (" ");
return i;
}
void banner (void)
{
printf ("------------------------------- ");
printf ("| 1) Change Shift (default 3) | ");
printf ("| 2) Encrypt a message | ");
printf ("| 3) Decrypt a message | ");
printf ("| 4) Quit | ");
printf ("------------------------------- ");
fflush(stdin);
}
int getUserChoice (void)
{
int i;
scanf ("%d", &i);
return i;
}
void getString (char buf[])
{
char t;
printf ("Input: ");
scanf ("%c", &t);
scanf ("%[^ ]s", buf);
return;
}
void encrypt (char buf[], int shift)
{
int len = 0, i;
len = strlen (buf);
for (i=0; i { buf[i] = buf[i] + shift; } printf ("Output: %s ", buf); return; } void decrypt (char buf[], int shift) { int len = 0, i; len = strlen (buf); for (i=0; i { buf[i] = buf[i] - shift; } printf ("Output: %s ", buf); return; } #define MAX_BUFF 500 /*Change your input buffer as per your requirement*/ int main() { int option; char buf[MAX_BUFF] = {'\0'}; int shift = 3; /*Default Shift 3*/ while (1) { banner (); printf ("Option: "); option = getUserChoice(); memset (buf, '\0', MAX_BUFF); switch (option) { case 1: shift = getShift(); continue; case 2: printf ("In 2 .. "); getString(buf); encrypt (buf, shift); continue; case 3: printf ("In 3 .. "); getString(buf); decrypt (buf, shift); continue; case 4: return 0; default: printf ("Invalid Choice, try again "); continue; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started