0

私は、ユーザーに文字のストリームを入力して、大文字と小文字の数を出力するように求めるプログラムを実行しています。私は関数でそれをやろうとしていますが、それを印刷するのに問題があります..すべての文字入力に対してim入力im取得im取得0, 0 私が間違っていることを理解するためにあなたの助けをいただければ幸いです:

#include <stdio.h>
#include <ctype.h>

int case_letters(int ch);

int main(void)

{
    int x;
    printf("please enter a some characters, and ctrl + d to see result\n");

    case_letters(x);

    return 0;
}

int case_letters(int ch)

{
    int numOfUpper = 0;
    int numOfLower = 0;

    while ((ch = getchar()) != EOF)
    {
        if ((ch = isdigit(ch)) || ch == '\n')
        {
            printf("please enter a valid character\n");
            continue;
        }


        else if ((ch = isupper(ch)))
        {
            numOfUpper++;
        }

        else if ((ch = islower(ch)))
        {
            numOfLower++;
        }

    }

    return  printf("%d, %d", numOfUpper, numOfLower);
}
4

2 に答える 2

3

すべてのifステートメントは異なる値を割り当て、の値をchチェックしませんch

たとえば、正しいを入力するcharと、

if ((ch = isdigit(ch)) || ch == '\n')

を返す0ためch、に割り当てます。私はあなたが必要だと思いますisdigit(ch)0

if ( isdigit(ch) || ch == '\n')

islowerとについても同じですisupper

于 2013-02-01T15:28:25.160 に答える
1
    if ((ch = isdigit(ch)) || ch == '\n')
            ^-- assignment, not equality test.

ch値をisdigit()、isupper()、およびislower()の戻り値で破棄しているため、isdigitテストを実行するとすぐに、ユーザーが入力した元の値が破棄されます。

試す

    if (isdigit(ch) || ch == '\n')
    else if (isupper(ch))
    else if (islower(ch))

代わりは。iswhatever値を保持する必要はありません。

于 2013-02-01T15:29:49.347 に答える