输出错误

Error in Output

我的输出与预期的不一样

第一行应该说明计时器应该计时多少时间 运行 第二个将显示 运行ning 计时器 第三个将显示分数

我不知道为什么计时器会变成 2147344384 之类的东西

#include <stdio.h>
#include <conio.h>
#include <time.h>

//main
int main(){
    //function declaration

int score(int c , int d);
int timer(int a , int b);

    //function declaration

    // variable declaration;
    time_t t1;
    int e ;
    //variable declaration;

printf("how many seconds\n");
scanf("%d",e);
(void) time(&t1);// time starts
while(1){

    printf("timer will run for %d seconds\n",e);
    printf("Time remaining = %d\n",timer(t1,e));
    printf("Current Score = %d\n",score(timer(t1,e), e));
    if (timer(t1,e) == 0){break;}
    system("cls");

}

}
//main


//timer
int timer(int starttime, int fortime){
    int tori = 0;
    // (void) time(&t1); // paste this where u have to start time
time_t t2;

   while(1){
    (void) time(&t2);
     tori=fortime-(t2-starttime);
     return (tori);
   }

}
//timer

// score
int score(int timerem, int time){
   int k , ka ;
    ka = ((timerem/time)*100);


if (ka >90 && ka <= 100){
    k = 10;
}
else if (ka >80 && ka <= 90){
    k = 9;
}
else if (ka >70 && ka <= 80){
    k = 8;
}
else if (ka >60 && ka <= 70){
    k = 7;
}
else if (ka >50 && ka <= 60){
    k = 6 ;
}
else if (ka >40 && ka <= 50){
    k = 5;
}
else if (ka >30 && ka <= 40){
    k = 4;
}
else if (ka >20 && ka <= 30){
    k = 3;
}
else if (ka >10 && ka <= 20){
    k = 2;
}
else if (ka >50 && ka <= 10){
    k = 1;
}
else if (ka >0 && ka <= 5){
    k = 0;
}
return (k);
}
//score
  1. 这行是错误的

    scanf("%d",e);
    

    你必须传递 e

    的地址
    scanf("%d", &e);
    

    因为你没有传递 e 的地址,e 永远不会被初始化并且包含一个垃圾值,这就是你的输出的意思。

  2. score() 函数中,k 可以 returned 而无需初始化。

注意: main() 应该 return 一个反映程序退出代码的 int,所以你缺少一个 return EXIT_SUCCESS; 在你的 main().

末尾

我修改了你的代码,测试一下

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

int score(int c , int d);
int timer(int a , int b);

//main
int main()
{
    //function declaration

    //function declaration

    // variable declaration;
    time_t t1;
    int    e ;
    //variable declaration;

    printf("how many seconds\n");
    scanf("%d", &e); // pass the address of e

    (void)time(&t1);// time starts

    while (1)
    {
        printf("timer will run for %d seconds\n",e);
        printf("Time remaining = %d\n",timer(t1,e));
        printf("Current Score = %d\n",score(timer(t1,e), e));

        if (timer(t1,e) == 0)
        {
            break;

        }
        system("cls");
    }
    return 0;
}

int timer(int starttime, int fortime)
{
    time_t t2;
    int    tori = 0;

    while (1)
    {
        (void) time(&t2);
        tori = fortime - (t2 - starttime);
        return (tori);
    }
}
//timer

// score
int score(int timerem, int time)
{
    int k , ka ;

    ka = ((timerem / time) * 100);
    k  = 0; // initialize k
    if (ka > 90 && ka <= 100)
        k = 10;
    else if (ka > 80 && ka <= 90)
        k = 9;
    else if (ka >70 && ka <= 80)
        k = 8;
    else if (ka > 60 && ka <= 70)
        k = 7;
    else if (ka > 50 && ka <= 60)
        k = 6 ;
    else if (ka > 40 && ka <= 50)
        k = 5;
    else if (ka > 30 && ka <= 40)
        k = 4;
    else if (ka > 20 && ka <= 30)
        k = 3;
    else if (ka > 10 && ka <= 20)
        k = 2;
    else if (ka > 50 && ka <= 10)
        k = 1;
    else if (ka > 0 && ka <= 5)
        k = 0;

    return (k);
}

tori 应该是计时器用完之前的秒数,对吧?如果我正确理解您的代码在 timer(a,b) 中的意图,该时间点的值为 starttime+fortime。 如果那是对的,tori = starttime+fortime-t2 就是正确的。

但话又说回来,您没有正确读取变量(提供 scanf 变量地址,而不是变量本身!); timer 中的 while(1) 循环根本没有任何意义,如果发生某些事情并且您错过了计时器()的 return 值的第二个,您可能永远不会结束程序0.

此外,虽然 C 绝对合法,但您在 main() 的范围内声明您的函数,老实说,这很奇怪。我不清楚你为什么要 (void) time(&t1);,因为忽略 return 值在这里是完全合法的。综上所述,我认为最公平的建议是多练习一下你的 C 技能,阅读你使用的函数的手册页(如果你有 Unix 系统,很可能 man 2 time),然后使用IDE 允许您在调试器中逐行执行代码;这将帮助您自己解决此类问题,让您迅速成为一名优秀的程序员!

there were two significant bugs in the posted code:
-- the timer function, calculation of percent remaining time always yielded 0
-- the score function, 'k' was not initialized when percent remaining time was 0

there is a the 'problem' that the outputs to the screen are at a very fast rate.
suggest adding a nanosleep() call of about 1/2 second in the main while loop

// <-- corrected included files for portability 
// <-- (and so will compile on my linux system)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>   // time()


// prototypes  // <-- moved function prototypes to file globally visible location
               // <-- otherwise compiler (except inside main()) will use default 
               // <-- return and parameter typing
int score(int c , int d);
int timer(int a , int b);

//main
int main()
{
    // variable declaration;
    time_t t1;
    int e ;

    printf("how many seconds\n");
    if( 1 != scanf(" %d", &e) ) // <-- corrected problems with statement
    { // then scanf failed      // <-- added error checking
        perror( "scanf for seconds failed" );
        return(-1);
    }

    // implied else, scanf successful

    t1 = time(NULL);// time starts // <-- modified for clarity

    while(1)
    {
        // <-- modified loop so all functions use same time value
        // <-- modified loop to eliminate sudden loss of initial/subsequent screen display
        time_t t2 = timer(t1,e);

        //system( "cls" );   // uncomment for windows
        //system( "clear" ); // uncomment for linux
        printf("timer will run for %d seconds\n",e);
        printf("Time remaining = %d\n",(int)t2);
        printf("Current Score = %d\n",score(t2, e));
        if (!t2) { break; }
    } // end while

    return(0);
} // end function: main


//timer
int timer(int starttime, int fortime)
{
    int tori = 0;
    // (void) time(&t1); // paste this where u have to start time
    time_t t2 = time(NULL);      // <-- modified for clarity
                                 // <-- removed unneeded/do nothing while loop
    tori=fortime-(t2-starttime);
    return (tori);
} // end function: timer


// score
int score(int timerem, int time)
{
    int k;   // returned value
    int ka;  // calculated percentage not expired

    ka = (timerem*100)/time; // <-- corrected this calculation


    if (ka >90 && ka <= 100)
    {
        k = 10;
    }

    else if (ka >80 && ka <= 90)
    {
        k = 9;
    }

    else if (ka >70 && ka <= 80)
    {
        k = 8;
    }

    else if (ka >60 && ka <= 70)
    {
        k = 7;
    }

    else if (ka >50 && ka <= 60)
    {
        k = 6 ;
    }

    else if (ka >40 && ka <= 50)
    {
        k = 5;
    }

    else if (ka >30 && ka <= 40)
    {
        k = 4;
    }

    else if (ka >20 && ka <= 30)
    {
        k = 3;
    }

    else if (ka >10 && ka <= 20)
    {
        k = 2;
    }

    else if (ka >50 && ka <= 10)
    {
        k = 1;
    }

    else if (ka > 0 && ka <= 5)
    {
        k = 0;
    }

    else // <-- added this code block, which eliminates display of random value at end
    {
        k = 0;
    }
    return (k);
} // end function: score