我的程序没有正确读取整数。为什么?

时间:2018-04-06 18:49:45

标签: c input

我想知道是否有人可以伸出援助之手指出我在这里做错了什么。我的任务是:

  

编写一个读取整数的程序,直到输入0。输入终止后,   程序应报告输入的偶数整数(不包括0)的总数   偶数整数的平均值,输入的奇数整数的总数,以及   奇数整数的平均值

我设法做了类似的任务,但是有了字符(停在'#'而不是0),我没有遇到问题。我在这里看不到或遗失的是什么?

#include <stdio.h>

int evenQ = 0, oddQ = 0, evenSum = 0, oddSum = 0; /* We initate variables */
int ch;

int main(void)
{
    while ((ch = getchar()) != 0 ) /* As long as the input is not 0 keep taking input*/
    {
        if (ch % 2 == 0) /* If the input is an even integer */
        {
            evenQ++; /* Add 1 to evenQ */
            evenSum += ch; /*Add the inputted int to evenSum */
        }
        else
        {
            oddQ++; /* Add 1 to oddQ */
            oddSum += ch; /* Add the inputted int to oddSum */
        }
    }

    printf("Total even integers:\t%d\n"
            "Total odd integers:\t%d\n"
            "Average even ints:\t%d\n"
            "Average odd ints:\t%d\n", evenQ, oddQ, (evenSum/evenQ), (oddSum/oddQ)); /* Print back
            the evenQ, oddQ and the averages of even integers and odd integers
            */

    return 0;
}

1 个答案:

答案 0 :(得分:2)

你的问题归结为getchar只能获取字符,而不是整数(如果输入1,则getchar将获取'1'-char,而不是int-)。使用scanf代替getchar,例如:

#include <stdio.h>

int evenQ = 0, oddQ = 0, evenSum = 0, oddSum = 0; /* We initate variables */
int ch;

int main(void)
{
    while ((scanf("%i", &ch) == 1) && (ch != 0 )) /* As long as the input is not 0 keep taking input*/
    {
        if (ch % 2 == 0) /* If the input is an even integer */
        {
            evenQ++; /* Add 1 to evenQ */
            evenSum += ch; /*Add the inputted int to evenSum */
        }
        else
        {
            oddQ++; /* Add 1 to oddQ */
            oddSum += ch; /* Add the inputted int to oddSum */
        }
    }

    printf("Total even integers:\t%d\n"
            "Total odd integers:\t%d\n"
            "Average even ints:\t%d\n"
            "Average odd ints:\t%d\n", evenQ, oddQ, (evenSum/evenQ), (oddSum/oddQ)); /* Print back
            the evenQ, oddQ and the averages of even integers and odd integers
            */

    return 0;
}

其他例子(最糟糕的方式,但更直观):

#include <stdio.h>

int evenQ = 0, oddQ = 0, evenSum = 0, oddSum = 0; /* We initate variables */
int ch = -1;

int main(void)
{
    while (ch != 0 ) /* As long as the input is not 0 keep taking input*/
    {
        scanf("%i", &ch);

        if (ch != 0) // Do not take in care int 0
        {
            if (ch % 2 == 0) /* If the input is an even integer */
            {
                evenQ++; /* Add 1 to evenQ */
                evenSum += ch; /*Add the inputted int to evenSum */
            }
            else
            {
                oddQ++; /* Add 1 to oddQ */
                oddSum += ch; /* Add the inputted int to oddSum */
            }
        }
    }

    printf("Total even integers:\t%d\n"
            "Total odd integers:\t%d\n"
            "Average even ints:\t%d\n"
            "Average odd ints:\t%d\n", evenQ, oddQ, (evenSum/evenQ), (oddSum/oddQ)); /* Print back
            the evenQ, oddQ and the averages of even integers and odd integers
            */

    return 0;
}