I made a Caesar cipher that successfully worked with command line arguments with the help of looking at other code examples. However, I want to make a version where you can scan in the key rather than having it be as a command line argument. The problem is, for some reason I can’t figure out, the fgets part of the code doesn’t let the user enter in anything and the program simply runs through and returns 0. The top part of the code is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int k; //key
char e[100]; //plain text to encrypt
int length; //string length for plain text
int c; //encrypted int (displays as char)
//get key
printf("Key? ");
scanf("%d", &k);
//display the key
printf("The key is: %i\n", k);
//prompt for plain text to encrypt
printf("Please enter the words to encrypt:\n");
fgets(e, 100, stdin);
printf("%s", e); //print words to encrypt
//get length of plain text string
length = strlen(e);
scanf()leaves the newline in the buffer, so when you get tofgets(), the first character read is a newline, which terminates thefgets()call, effectively returning an empty line. You’ll need to do something to skip over the newline before you callfgets().