2014-04-03 93 views
0

所以這是我目前的問題。 Scanf()實際上並沒有在我的程序中的特定點上掃描,並且沒有任何理由不能從我的編程/邏輯部分中進行掃描(嗯,據我所知)。scanf()沒有在輸入

此外,我有一個if語句,也是不工作,如果我輸入31,它應該做條件。但是,它沒有。爲了澄清,任何時候我切換scanf()與gets()一個窗口彈出說:「LFSR.c已停止工作......」。所以,出於某種原因,我不能僅僅做這個轉換。

#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; 
} 
+0

有人需要使用回車鍵,對不對? – devnull

+1

看看右邊的一些相關問題。 – devnull

+2

'scanf(「&d」,&numOfRands);'?? – Dabo

回答

4
scanf("&d", &numOfRands); 

你的意思

scanf("%d", &numOfRands); 

+0

你非常正確,它真的傷害了我。 謝謝。 – user41836

1

您使用了兩次地址運算符,一個輸入順序字符串。要在字符串中使用正確的運算符是%d,因爲它類似於int。 %d - int。 %c - char。 %s - 字符串。 依此類推... 無論如何 - scanf應該寫成scanf("%d", &numOfRands);而不是scanf("&d", &numOfRands);