2014-10-19 15 views
-4

我一直在盯着這段代碼幾個小時。 int main運行時,它顯示一個菜單,接受一個數字輸入,但在輸入數字後,程序不執行任何操作。當我按下「暫停」時,它顯示程序停留在函數調用中。任何幫助表示讚賞。當我運行它並按暫停休息時,C程序顯示它卡在函數調用中

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

void simdice(int a); 
void fiboseries(int b); 
void revdigits(int c); 
void fourpatterns(int d); 
void exitchoice(int e); 

int main() 
{ 

    void (*f[ 6 ])(int) = { simdice, fiboseries, revdigits, fourpatterns, exitchoice }; 
    int userchoice; 

    printf ("Please enter a number to select one of the options below: \n"); 
    printf ("1- Simulate the roll of two dice \n"); 
    printf ("2- The Fibonacci Series \n"); 
    printf ("3- Reverse digits \n"); 
    printf ("4- Print four patterns \n"); 
    printf ("5- Exit \n"); 

    scanf ("%d", &userchoice); 

    while (userchoice>=0 && userchoice<5); 
    { 
     (*f[userchoice])(userchoice); 

     printf ("Please enter a number to select one of the options below: \n"); 
     printf ("1- Simulate the roll of two dice \n"); 
     printf ("2- The Fibonacci Series \n"); 
     printf ("3- Reverse digits \n"); 
     printf ("4- Print four pattersn \n"); 
     printf ("5- Exit \n"); 

     scanf ("%d", &userchoice); 
    } /* end while */ 

    printf("Program Now Closing \n"); 
               /* indicates successful termination */ 
} /* end main */ 

void simdice(int a) 
{ 
    srand(time(NULL));       /* random number generator seeded */ 
    int a1;          /* second loop counter */ 
    int die1;          /* first of the dice to roll */ 
    int die2;          /* second of the dice to roll */ 
    long b1;          /* first loop counter */ 
    int total[13]={0};       /* count number appearances in main */ 
    int runshow[13]={0,0,1,2,3,4,5,6,5,4,3,2,1}; /* compares expected appearances vs actual */ 



    /* counter makes dice roll 36,000 times */ 
    for (b1 = 1; b1 <= 36000; b1++) 
    { 

     /* each dice is calculated as a six sided die and the sums are added together */ 
     die1 = rand() % 6 + 1; 
     die2 = rand() % 6 + 1; 
     ++total[ die1 + die2 ]; 
    } 

    /* print table titles */ 
    printf ("%10s%10s%10s%10s\n", "Sum", "Total", "Expected", "Actual"); 

    /* data from above rolls are totaled and formated into columns */ 
    for (a1 = 2; a1 <= 12; a1++) 
    { 

     printf ("%10d%10d%9.3f%%%9.3f%%\n", a1, total[a1], 100.0 * runshow[a1]/36, 100.0 * total[a1]/36000 ); 
    } 
    /* indicate successful termination */ 
} 


void fiboseries(int b) 
{ 
    /* declaring and initializing primary variables for user input calculation */ 
    int i; 
    int iterate; 
    int start = 0; 
    int pass = 1; 
    int next; 
    /* declaring and initializing secondary variables for system max calculation */ 
    int i2; 
    int iterate2; 
    int start2 = 0; 
    int pass2 = 1; 
    int next2 = 1; 
    { 
     /* prompt user for integer and assign to variable */ 
     printf("Which position in the Finbonacci sequence do you want to display: "); 
     scanf("%d", &i); 

     /* display message to introduce position number */ 
     printf("The Fibonacci number for that postion is: "); 


     /* calculate Fibonacci number based on user entered position number */ 
     for (iterate = 0; iterate < i; iterate++) 
     { 
      if (iterate <= 1) 
       next = iterate; 
      else 
      { 
       next = start + pass; 
       start = pass; 
       pass = next; 
      } 

     } 
     /* display position number based on user input integer */ 
     printf("%d \n", next); 


     /* calculate largest Fibonacci number the system can accurately display for this */ 
     for (iterate2 = 0; next2 >= 0; iterate2++) 
     { 
      next2 = start2 + pass2; 
      start2 = pass2; 
      pass2 = next2; 
     } 

     /* print largest Fibonacci number the system can accurately display for this data */ 
     printf("The largest Fibonacci number that can be calculated on this system is: %d"); 
     printf("\n\n"); 
    } 
} 
+0

你是否已經在調試器中加入了代碼? – OldProgrammer 2014-10-20 00:00:22

+0

這裏有一個嚴重缺乏信息。你輸入了什麼號碼?當你停止你的程序時,哪個函數調用被卡住了? *什麼*工作? – 2014-10-20 00:01:23

+0

'main()'應該總是在代碼的底部,畢竟是'functions'。這可能無法解決您的問題,但我認爲需要指出。祝你好運! – Carlton 2014-10-20 00:02:06

回答

1

這條線的問題是:

while (userchoice>=0 && userchoice<5); 

您在此聲明,這意味着你的while循環的內容是空的末尾有一個分號,它有效地是一個無限循環。刪除分號。

相關問題