如何使编译器重复,直到数0 presed [英] How to make compiler repeat until the number 0 is presed

查看:120
本文介绍了如何使编译器重复,直到数0 presed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使编译器重演,如果用户presses在最后一个随机按钮。但是,如果用户presses0的编译器退出。

I'm wondering how to make the compiler repeat itself if the user presses a random button at the end. But if the user presses "0" the compiler exits.

我的code:

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

struct mystruct
{
    float startnummer;
    float hoppnummer;
    float svarighetsgrad;
    float domarpoangs[7];
};

int main(void)
{
    struct mystruct data;
    float max = 0;
    float min = FLT_MAX;
    float sum = 0;
    float avg = 0;
    int i = 0;
    float resultat = 0;

    printf("Startnummer: \n");
    scanf_s("%f", &data.startnummer);

    printf("Hoppnummer:\n");
    scanf_s("%f", &data.hoppnummer);

    printf("Svarighetsgrad:\n");
    scanf_s("%f", &data.svarighetsgrad);


    for (i = 0; i < 7; i++)
    {
        printf("domarpoang %d\n", i + 1);
        float f;
        if (scanf_s("%f", &f) == 1)
        {
            if (f < min) min = f;
            if (f > max) max = f;

            data.domarpoangs[i] = f;
        }
        else
        {
            printf("error parsing float\n");
            exit(0);
        }

    }

    system("cls");
    printf("Startnummer: %.1f \n", data.startnummer);
    printf("Hoppnummer: %.1f\n", data.hoppnummer);
    printf("Svarighetsgrad: %.1f\n", data.svarighetsgrad);
    for (i = 0; i < 7; i++)
    {
        printf("Domarpoang %d: %.1f\n", (i + 1), data.domarpoangs[i]);
    }

    for (i = 0; i < 7; i++)
    {
        sum += data.domarpoangs[i];

    }
    sum = sum - (max + min);

    avg = sum/5;

    resultat = avg * 3 * data.svarighetsgrad;

    printf("Hoppoang:%.2f \n", resultat);


    printf("Tryck tangent for nytt hopp!"); 
    getchar();
    getchar();
    return 0;
}

*如果用户presses随机按钮,编译器何其从一开始
*如果用户presses 0,编译器将退出。

*If the user presses random button, the compiler repeat itself from the beginning *If the user presses 0, the compiler exits.

任何帮助是AP preciated,谢谢你。

Any help is appreciated, thank you.

推荐答案

这答案使一个循环在你的的main() code的身体,照顾重新初始化一些变量为下一次迭代。

This answer puts a loop around the body of your main() code, taking care to re-initialise some of the variables for the next iteration.

有有关获取键盘输入和清理杂物多那么问题。我不知道的简单的标准的测试键盘输入方式,如的kbhit(),连续服用一个键输入,如<$ C $的C>残培()或冲洗的投入。即使的getchar()是可怕的 - 它不会返回,直到你有pressed回车,它的叶子在输入缓冲区。这就造成了很多所以用坚不可摧的(对我)格式的答案为 scanf()的来刷新输入,或者测试如果(的getchar()= = EOF) - 不向Enter键响应

There are many SO questions about getting keyboard input and clearing the debris. I know of no simple standard ways of testing for keyboard input such as kbhit(), for taking a single key input such as getch() or for flushing the input. Even getchar() is horrible - it won't return until you have pressed "Enter" which it leaves in the input buffer. This has resulted in many SO answers with impenetrable (to me) formats for scanf() to flush the input, or testing if (getchar() == EOF) - which does not respond to the "Enter" key.

所以,我已经把周围的的main() code,'0'时,它终止进入其次是控制字符一个简单的包装(因为与fgets()追加换行)或终止。这消除了需要清理输入 - 除了在用户输入一些愚蠢的打字的情况下。 GIGO!

So I have put a simple wrapper around the main() code, which terminates when '0' is entered followed by a control char (because fgets() appends the newline) or terminator. This removes the need to clean up the input - except in the case where the user inputs some silly typing. GIGO!

#include <stdio.h>
#include <float.h>
#define BUFFSIZE 10

struct mystruct {
    float startnummer;
    float hoppnummer;
    float svarighetsgrad;
    float domarpoangs[7];
};

int main(void)
{
    char kbuff [BUFFSIZE+1];
    struct mystruct data;
    float max;
    float min;
    float sum;
    float avg;
    int i;
    float resultat;

    do {
        max = 0;                // initialise for each loop
        min = FLT_MAX;
        sum = 0;
        printf ("Body of your main loop\n");
        fgets(kbuff, BUFFSIZE, stdin);
    } while (kbuff[0] != '0' || kbuff[1] >= ' ');
    return 0;
}

这篇关于如何使编译器重复,直到数0 presed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆