如何避免多次提示用户输入?

时间:2015-11-14 01:57:53

标签: c function

我正在创建一个简单的程序,在一个函数mark中请求用户输入,然后将输入值传递给第二个函数characters进行验证。

#include <stdio.h>
void marks(float *mark1, float *mark2, float *mark3);
void characters(float mark1, mark2, float mark3);

void marks(float *mark1, float *mark2, float *mark3) {

    float firstMark, secMark, thirdMark;

    printf("Enter mark1, mark2, and mark3: ");
    scanf("%f,%f,%f", mark1, mark2, mark3);

    while (mark1 < 0 || mark2 < 0 || mark3 < 0) {

        printf("Error, marks can't be less than zero \n");
        printf("Enter mark1, mark2, and mark3: ");
        scanf("%f,%f,%f", mark1, mark2, mark3);

        firstMark = *mark1;
        secMark   = *mark2;
        thirdMark = *mark3;
    }
}

void characters(float mark1, float mark2, float mark3) {

    marks(&mark1, &mark2, &mark3);

    if (mark1 < 50 || mark2 < 50 || mark3 < 50) {
        printf("Really Bad! You have failed. \n");
    }
    else if (mark1 > 50 && mark2 > 50 && mark3 > 50) {

        printf("You have passed. \n");
    }
    else {
        printf("ERROR! \n");
    }
}

int main(void) {

    int i;
    float mark1, mark2, mark3;

    printf("Grade list \n");

    marks(&mark1, &mark2, &mark3);
    characters(mark1, mark2, mark3);
}

问题出在characters,当我呼叫mark时,它要求用户输入,但我只希望程序要求用户在main中输入标记。

如果没有程序要求输入数据,如何在marks中调用characters,以便我可以使用它的参数来计算值?

1 个答案:

答案 0 :(得分:1)

如果您不想要求用户输入两次,请删除对marks的第二次通话。已经characters通过调用mainmarks收集了输入,并通过了三个参数;没有必要第二次打电话。

这将解决您的问题:

void characters(float mark1, float mark2, float mark3) {
    // No second call to marks
    if (mark1 < 50 || mark2 < 50 || mark3 < 50) {
        ...
    }
    ...
}