C程序显示当我运行它并按暂停中断时它停留在函数调用上

时间:2014-10-19 23:58:38

标签: c function pointers call

我几个小时都在盯着这段代码。 int main运行,它显示一个菜单,接受一个数字输入,但在输入数字后,程序什么都不做。当我按下"暂停休息"它表明程序停留在函数调用上。任何帮助表示赞赏。

#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");
    }
}

1 个答案:

答案 0 :(得分:1)

这一行是问题所在:

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

在此语句的末尾有一个分号,表示while循环的内容为空,它实际上是一个无限循环。删除分号。