Please correct this C coding program. Their are mistakes in the code and I need
ID: 3876770 • Letter: P
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 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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<len; 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<len; 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] = {''};
int shift = 3; /*Default Shift 3*/
while (1)
{
banner ();
printf ("Option: ");
option = getUserChoice();
memset (buf, '', 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;
}
Requirements: Use the following: - functions - function returns - function parameters - strings -loops - if statements Include the following functions - int getUserChoice() - int getshift() - void getString(char buf[1) - void encrypt (char buf, int shift) - void decrypt (char bufl, int shift) The parameters of these functions are given, do not change them. However, the body of these functions need to be filled in as does the body of main) getUserChoice() will be used to print the menu and get the users choice of 1, 2, 3, or 4 * Your program should either quit or handle invalid menu options. getstring) will be used to get the string the user wants encrypted * These are the only 3 functions scanf.) or fgets() are allowed to appear in. This means input functions cannot appear in main() Your default shift value should be set to 3 * DO NOT encrypt/decrypt the newline ( ) character or NULL terminator (1) character in your stringsExplanation / Answer
//every thing is fine with you code....
//i have changed shift i many ways ,....i went to loop
//and logic is also fine
//not a problem with shift..
//because loop value depends on length of the buffer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getShift( void )//fine
{
int i;
printf ("Enter new shift value: ");
scanf ("%d", &i);
printf (" ");
return i;
}
void banner (void)//fine
{
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)//fine
{
int i;
scanf ("%d", &i);
return i;
}
void getString (char buf[])//fine
{
char t;
printf ("Input: ");
scanf ("%c", &t);
scanf ("%[^ ]s", buf);
return;
}
//infinite loop can be possible
void encrypt (char buf[], int shift)
{
int len = 0, i;
len = strlen (buf);
for (i=0; i<len; i++)//fine
{
buf[i] = buf[i] + shift;
}
printf ("Output: %s ", buf);
return;
}
//infinite loop can be possible
void decrypt (char buf[], int shift)
{
int len = 0, i;
len = strlen (buf);
for (i=0; i<len; i++)//fine
{
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] = {''};
int shift = 3; /*Default Shift 3*/
while (1)//this loop also fine
{
banner ();
printf ("Option: ");
option = getUserChoice();
memset (buf, '', 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;
}
//everything is fine..
//if you have any specific case/input
//comment....it
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: hello
Output: khoor
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 1
Enter new shift value: 4
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: hello
Output: lipps
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 3
In 3 ..
Input: lipps
Output: hello
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: sd
Output: wh
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 1
Enter new shift value: 1
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: abc
Output: bcd
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: z
Output: {
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: hello wsu
Output: ifmmp!xtv
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 3
In 3 ..
Input: ifmmp!xtv
Output: hello wsu
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 1
Enter new shift value: 0
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: abc
Output: abc
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 1
Enter new shift value: -1
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: bcd
Output: abc
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 3
In 3 ..
Input: abc
Output: bcd
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input:
Output:
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 6
Invalid Choice, try again
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 10
Invalid Choice, try again
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: -10
Invalid Choice, try again
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 1
Enter new shift value: 2
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: asdf
Output: cufh
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 2
In 2 ..
Input: efhjadslfi ahdfkjh kjdshafk
Output: ghjlcfunhk"cjfhmlj"""""""""mlfujchm
-------------------------------
| 1) Change Shift (default 3) |
| 2) Encrypt a message |
| 3) Decrypt a message |
| 4) Quit |
-------------------------------
Option: 4
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.