I'm trying to convert my Caesar Cipher code from taking a user given argument to using a user given input, but it's not going the way I've intended at all. I have this code, and it asks the first input for the ROT number, but then it skips the input for the rest of the code. Now, if I wanted to rotate by 2 and use the string bB, the output should be dD, and it is, but only if, when aksed for the input, you put "2 bB". I don't know why this is, and I've looked at other threads saying to just put scanf("%c", &blah);, but I don't know how to do this in my situation. Any help is thankful.
Edit: Changed char to int, as I did in my code just before I posted this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(){
/**********************************************************************************************************************************/
int bytesRead;
int nbytes = 255;
char *encryptString;
encryptString = (char *) malloc (nbytes + 1);
//char encryptString[256];
char finalChar;
char finalString[256];
int rotNum;
/**********************************************************************************************************************************/
puts("Please enter the ROT (rotate) number you wish to encrypt by: ");
scanf("%d", &rotNum);
printf("Please enter the phrase you'd like to encrypt: \n");
fgets(encryptString, sizeof(encryptString), stdin);
printf("The string entered is: %s\n", encryptString);
printf("The encrypted version is: ");
int n = strlen(encryptString) - 1;
int i;
for(i = 0; i < n; i++){ //For loop to go through the entire string entered
if(isupper(encryptString[i])){
finalChar = (((encryptString[i] - 65) + rotNum) % 26) + 65;
finalString[i] = toupper(finalChar);
//printf("%c\n", finalChar);
}
else if(islower(encryptString[i])){
finalChar = (((encryptString[i] - 97) + rotNum) % 26) + 97;
finalString[i] = tolower(finalChar);
//printf("%c\n", finalChar);
}
else{
finalChar = ' ';
finalString[i] = finalChar;
}
printf("%c", finalString[i]);
}
printf("\n");
return 0;
}