为什么我的C代码跳过下一个用户输入?

时间:2014-10-14 06:24:00

标签: c

我正在尝试将我的Caesar Cipher代码转换为使用用户给定的参数来使用用户给定的输入,但它并没有按照我的预期方式进行。我有这个代码,它询问ROT编号的第一个输入,但随后它会跳过其余代码的输入。现在,如果我想旋转2并使用字符串bB,则输出应该是dD,并且它是,但只有在为输入选择时,才会输入“2 bB”。我不知道为什么会这样,而且我看了其他线程说只是把scanf(“%c”,& blah);但我不知道如何在我的情况下这样做。任何帮助都很感激。

编辑:将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;
}

4 个答案:

答案 0 :(得分:2)

您的代码中几乎没有问题:

1)

scanf("%d", &rotNum);

您要将char *传递给scanf()。将rotNum声明为int。

2)读取输入rotNum后,scanf()在输入缓冲区中留下'\ n'。fgets();一旦遇到\n就停止读取输入。所以fgets()完全不读。在scanf()调用之后使用getchar();来使用换行符char。或者更好,请使用rotNum阅读fgets()并使用sscanf()解析。

3)你对fgets()的第二个参数是错误的。

fgets(encryptString, sizeof(encryptString), stdin);

这里,encryptString是一个指针。因此,这将为您提供平台上指针的大小,而不是它指向的字节数(256)。将其更改为:

fgets(encryptString, 256, stdin); // or whatever the bytes you allocate

Additioanlly,

1)为main()使用正确的退货类型,例如int main(void)int main(int argc, char **argv)或同等的。 2)检查malloc()的{​​{1}}的返回值,看它是否失败 3)Casting the malloc() return is unnecessary and error-prone.

答案 1 :(得分:0)

正如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.

所以当你输入旋转数字&amp;然后点击enter,输入缓冲区将包含number\n。 号码将存储在rotNum中,\n将保留在stdin中。 所以fgets会读它&amp;返回时无需等待输入。

使用scanf代替fgets。 如果您使用的是linux机器,here is the man page

答案 2 :(得分:0)

我对您的代码进行了三次更改,并且没有任何问题:

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++)"  

您必须进行一些其他更改才能使其适用于负rotNum。

答案 3 :(得分:-1)

在使用fgets之前尝试fflush(stdin)。这应该从stdin中清除\ n