Reputation: 126
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;
}
Upvotes: 0
Views: 215
Reputation: 81
as the man page of fgets
says ,
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline.
So when you enter rotate number & then hit enter
,the input buffer will contain number\n
.
While number will be stored in rotNum
, \n
will remain in stdin
.
So fgets
will read it & returns without waiting for input.
Use scanf
instead of fgets
.
If you are using linux machine, here is the man page
Upvotes: 0
Reputation: 2052
I made three changes to your code and it worked without any problem:
1. Use "int rotNum" instead of "char rotNum"
2. Use "scanf("%s", encryptString)" instead of "fgets(encryptString, sizeof(encryptString), stdin)"
3. Either use "int n = strlen(encryptString)" or "for(i = 0; i <= n; i++)"
You have to make some other changes to make it work for negative rotNum.
Upvotes: 0
Reputation: 121357
You have few problems in your code:
1)
scanf("%d", &rotNum);
Here you are passing a char *
to scanf(). Declare rotNum
as int.
2) After reading the input rotNum
, scanf() leaves a '\n' in the input buffer.fgets();
stops reading input once encounters a \n
. So fgets()
doesn't read at all. Use getchar();
after scanf() call to consume the newline char. Or better, read the rotNum
using fgets()
and parse it using sscanf()
.
3) Your second argument to fgets() is wrong.
fgets(encryptString, sizeof(encryptString), stdin);
Here, encryptString
is a pointer. So this will give you the size of pointer on your platform, not the number of bytes (256) that it points to. Change it to:
fgets(encryptString, 256, stdin); // or whatever the bytes you allocate
Additioanlly,
1) Use a proper return type for main()
such as int main(void)
or int main(int argc, char **argv)
or equivalent.
2) Check the return value of malloc()
for NULL
to see if it's failed.
3) Casting the malloc() return is unnecessary and error-prone.
Upvotes: 2
Reputation: 654
Try fflush(stdin)
before using fgets. This should clear the \n from stdin
Upvotes: -1