[英]Why is my C code skipping the next user input?
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. 我正在尝试将我的Caesar Cipher代码从使用用户给定的参数转换为使用用户给定的输入,但是这根本不符合我的预期。 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.
我有此代码,它要求ROT号的第一个输入,但随后将跳过其余代码的输入。 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".
现在,如果我想旋转2并使用字符串bB,则输出应为dD,并且是dD,但前提是在输入时加上“ 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.
我不知道为什么会这样,我已经看过其他线程说只将scanf(“%c”,&blah);放进去,但是我不知道该怎么做。 Any help is thankful.
任何帮助都是感激的。
Edit: Changed char to int, as I did in my code just before I posted this. 编辑:将char更改为int,就像我在发布此代码之前在代码中所做的那样。
#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;
}
You have few problems in your code: 您的代码中有几个问题:
1) 1)
scanf("%d", &rotNum);
Here you are passing a char *
to scanf(). 在这里,您将一个
char *
传递给scanf()。 Declare rotNum
as int. 将
rotNum
声明为int。
2) After reading the input rotNum
, scanf() leaves a '\\n' in the input buffer. 2)读取输入
rotNum
,scanf()在输入缓冲区中留下一个'\\ n'。 fgets();
stops reading input once encounters a \\n
. 遇到
\\n
停止读取输入。 So fgets()
doesn't read at all. 因此
fgets()
根本不会读取。 Use getchar();
使用
getchar();
after scanf() call to consume the newline char. 在scanf()调用之后消耗掉换行符。 Or better, read the
rotNum
using fgets()
and parse it using sscanf()
. 或者更好的方法是,使用
fgets()
读取rotNum
并使用sscanf()
对其进行解析。
3) Your second argument to fgets() is wrong. 3)您对fgets()的第二个参数是错误的。
fgets(encryptString, sizeof(encryptString), stdin);
Here, encryptString
is a pointer. 在这里,
encryptString
是一个指针。 So this will give you the size of pointer on your platform, not the number of bytes (256) that it points to. 因此,这将为您提供平台上指针的大小,而不是指针所指向的字节数(256)。 Change it to:
更改为:
fgets(encryptString, 256, stdin); // or whatever the bytes you allocate
Additioanlly, Additioanlly,
1) Use a proper return type for main()
such as int main(void)
or int main(int argc, char **argv)
or equivalent. 1)对
main()
)使用正确的返回类型,例如int main(void)
或int main(int argc, char **argv)
或等效的类型。
2) Check the return value of malloc()
for NULL
to see if it's failed. 2)检查
malloc()
的返回值是否为NULL
,看是否失败。
3) Casting the malloc() return is unnecessary and error-prone. 3) 强制转换malloc()返回是不必要的,并且容易出错。
as the man page of fgets
says , 正如
fgets
的手册页所述,
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
. 因此,当您输入旋转数字然后按
enter
,输入缓冲区将包含number\\n
。 While number will be stored in rotNum
, \\n
will remain in stdin
. 虽然数字将存储在
rotNum
,但\\n
将保留在stdin
。 So fgets
will read it & returns without waiting for input. 因此,
fgets
将读取它并返回而无需等待输入。
Use scanf
instead of fgets
. 使用
scanf
而不是fgets
。 If you are using linux machine, here is the man page 如果您使用的是Linux机器,则这是手册页
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. 您必须进行一些其他更改才能使其适用于负数rotNum。
Try fflush(stdin)
before using fgets. 在使用fgets之前尝试
fflush(stdin)
。 This should clear the \\n from stdin 这应该从标准输入中清除\\ n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.