scanf()没有输入

时间:2014-04-03 19:52:31

标签: c if-statement scanf gets

所以这是我目前的问题。 Scanf()实际上并没有在我的程序中的特定点进行扫描,并且没有任何理由说明为什么它不会出现在编程/逻辑部分(嗯,据我所知)。

另外,我有一个if语句也没有用,如果我输入31,它应该执行条件。但是,它只是没有。

并且澄清一下,无论何时我用scan()切换scanf(),弹出一个窗口说" LFSR.c已停止工作。 。 &#34 ;.所以,出于某种原因,我无法进行转换。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int result = 0;
    int sigBit = 0;

    int numOfRands;

    unsigned feedBack;

    // Will hold the input
    unsigned int input;
    // Will be the number of taps the user can give.
    int taps[32];

    // Prompts user for an unsigned int
    puts("Please write an unsigned integer.");

    // Places the input into the correct variable.
    // This works perfectly fine. 
    scanf("%u", &input);

    // Prompt user for amount of random numbers they'd like.
    puts("How many random numbers would you like to be generated.");

    // Places the input into the corresponding variable.
    // This scanf never actually happens. It skips over it.
    scanf("&d", &numOfRands);

    // Prompts the use for the taps they wish to use.
    puts("Please give the taps you'd like to use, between the numbers 0 - 32");

    // Scan the taps into the array,
    while(fscanf(stdin, "%d", &taps[i]) == 1)
    {
        // I also tried to just use if(i == 31), but eitherway, it never triggered this
        // conditional statement.
        if(taps[i] == 31)
            {
                sigBit = 31;
            }

        i++;
        puts("Tap stored.");

    }
    puts("Currently, the sig bit is,");
    printf("%d", sigBit);

    if(sigBit == 0)
    {
       taps[i] = 31;
       printf("We placed the sig bit as a Tap.\n");
       printf("%d, %d\n", taps[i], i);
    }

    for(j = 0, k = 1; j < i+1; j = k++)
    {
        printf("The %d tap was, %d\n", j, taps[j]);

    }
    printf("Please input how many random numbers you want to be generated now\n");

    // This is to test if scanf is working or not, and it always triggers so,
    // That means there's an error, right?
    if(scanf("%d", &numOfRands) != 1)
        exit(1);

    printf("We will create %d random numbers for you.", numOfRands);






    return 0;
}

2 个答案:

答案 0 :(得分:4)

scanf("&d", &numOfRands);

你的意思是

scanf("%d", &numOfRands);

答案 1 :(得分:1)

您使用了地址运算符两次,其中一个在输入顺序字符串中。要在字符串中使用的正确运算符是%d,因为它类似于int。 %d - int。 %c - char。 %s - 字符串。 等等... 无论如何 - scanf应该写成scanf("%d", &numOfRands);而不是scanf("&d", &numOfRands);