从输出中排除用户的输入值

时间:2017-02-11 05:26:25

标签: c loops

编写交流程序,目的是让用户输入多次,当用户输入特定年龄时,循环停止并输出所有值的平均值,最小值和最大值用户只需输入;但是,在确定年龄的最大/最小/平均值时,程序必须忽略停止while循环的年龄。我只能使用一个循环。我遇到的唯一问题是输出。它并没有忽略具体的年龄,而是给了我不可思议的数字。这是我编码的一部分:

 #include <stdio.h>
 int age, avAge, minAge, maxAge, sum;
 sum = 0;
//here is where I get the user input
    while (age != -1){
              printf("Please enter age: \n");
              scanf("%d", &age);
//here is where I try to calculate the average
              sum += age;
              avAge = sum / age;
//here is where I placed restrictions on the ages the user can input
              if (age >= 0 && age <= 100)
                   printf("Nice try!\n");
//here is where I try to get the largest/smallest in order
              if (age < minAge)
                   minAge = age;
              if (age > maxAge)
                   maxAge = age;

//here is where I output if the user inputs a 0
              else ( age == -1){
              printf("Smallest: %d\n", minAge);  
              printf("Largest: %d\n", maxAge);
              printf("Average: %d\n", avAge)

  return 0;
  }

请原谅我的编码格式,因为我在计算机上运行了一个未安装的ubuntu,所以我在手机上。我只是想知道我应该使用什么来阻止程序使用0作为minAge。由于我只能使用一个循环,我认为我的选择有限。请记住,我是非常新的,所以我为我的任何无知道歉。谢谢。

3 个答案:

答案 0 :(得分:1)

我认为无限循环在这里有一定程度的意义,我已经将你的打印结果语句移出循环,所以它们仍然只会在用户输入年龄后才执行。我还将if (age >= 0 && age <= 100)更正为if (age < -1 && age > 100),并在使用前正确初始化所有变量并添加了一些评论,如果您还有任何其他问题,请发表评论:)

#include <stdio.h>
#include <limits.h> // for INT_MAX 
int main(void) 
{
    int age, avAge = 0, minAge = INT_MAX, maxAge = 0, sum = 0, avgCounter = 1; // avgCounter is the amount of times the loop has ran

    while( 1 ) //infinite loop broken if age == -1
    {
        printf("Please enter age: \n");
        scanf("%d", &age);

        if( age == -1 )
            break;   // exit loop
        else if (age < -1 && age > 100)
           printf("Nice try!\n"); // could add continue here in order to not skew the min/max and average age variables.
        else if (age < minAge)
           minAge = age;
        else if (age > maxAge)
           maxAge = age;
        sum += age;
        avAge = sum / avgCounter;
        avgCounter++;
    } 

    printf("Smallest: %d\n", minAge == INT_MAX? 0 : minAge); // incase the user enters -1 straight away  
    printf("Largest: %d\n", maxAge);
    printf("Average: %d\n", avAge);
}

注意:看到“有问题的数字”的实际原因可能是undefined behaviour,这要归功于未初始化的变量,在尝试阅读/使用它们之前,必须始终确保它们已初始化。

答案 1 :(得分:0)

编辑: 当前代码中存在太多与逻辑和语法相关的小错误。 以下代码按您的要求正常工作。

 #include <stdio.h>

 int main(){
 int age, avAge, minAge, maxAge, sum;
 minAge = 100000 ; // some very large number
 maxAge = -1;   // initailize with min possible age
 sum = 0;
 int iteration = 0 ; // no. of times the loops run with valid input from user
    do{
              printf("Please enter age: , -1 to quit \n");
              scanf("%d", &age);

              if (age >= 0 && age <= 100) {
                   printf("Nice try!\n");
                   iteration ++; 
                   sum += age;
                   avAge = sum / age;   

                 if (age < minAge)
                   minAge = age;

                 if (age > maxAge)
                   maxAge = age;
              }
              else {
                printf("Smallest: %d\n", minAge);  
                printf("Largest: %d\n", maxAge);
                printf("Average: %d\n", avAge);
                break ;
              }     
    }
    while( age > 0 && age <= 100) ;

    return 0;
  }

在ideone上工作example

答案 2 :(得分:0)

假设有效年龄在(0, 100]范围内,这是一个实现:

#include <stdio.h>

int main(void) {
    int age, minAge, maxAge, sum, avAge;
    sum = 0;
    int i = 0;     // counts the number of ages read

    printf("Please enter age: ");
    while (scanf("%d", &age)) {
        // if age is in range (0, 100], consider valid
        //   and invalid otherwise
        if (age > 0 && age <= 100) {
            // calculate sum
            sum += age;

            // if this is the first loop round (i == 0), initialize
            //   both minAge & maxAge to 'age', since they
            //   don't yet have a valid value
            if (i == 0) {
                minAge = age;
                maxAge = age;
                ++i;
            } else {
                if (age < minAge) {
                    minAge = age;
                }
                if (age > maxAge) {
                    maxAge = age;
                }
                ++i;
            }
        } else if (age == 0) {
            // if age is zero, break/exit loop
            // zero can be changed to any invalid value (eg. -1)
            break;
        } else {
            printf("Nice try!\n");
        }
        printf("Please enter age: ");
    }

    // calculate average (outside loop)
    // average = sum / number of ages read
    avAge = sum / i;

    // print summary
    printf("Smallest: %d\n", minAge);
    printf("Largest: %d\n", maxAge);
    printf("Average: %d\n", avAge);

    return 0;
}