c isalpha 和 isdigit while 循环

标签 c while-loop fgets

如何将 isalpha 和 isdigit 放入 while(1) 循环中?

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

int main()
{
    int i;
    char type[256];
    printf("You can type a number or a word. Type exit to exit! \n");
    printf("Type: ");

    fgets (type, 256, stdin);

    if (isalpha(type[i]))
    {
            printf("Typed text: %s\n", type);
        if((strcmp(type,"exit\n") == 0))
        {
            printf("Exiting...\n");
            exit(1);
        }
    }
    else if (isdigit(type[i]))
    {
            printf("Typed number: %s\n", type);
    }
    else
    {
        printf("Typed: %s\n", type);
        printf("Its not a letter or number...?!\n");
    }
}

我尝试在代码开头添加 while(1) 并在代码末尾将其关闭,但是一旦我输入数字或字母,控制台就会崩溃...有人可以帮我解决这个问题吗?

最佳答案

你的问题不是循环问题,你需要给 i 一个值,因为它是未定义的,你会得到一个很好的崩溃。请更换

int i;

int i=0;

关于c isalpha 和 isdigit while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33190385/

相关文章:

c - fgets 不接受最后一个值?

c - 在我的语法中得到转变/减少错误

c - 我想要一个读取文件并返回 ascii 值之和的函数

c - While 循环在到达 getchar() 之前复制 printf 两次?

python - 如何在Python中运行多线程进行多客户端套接字编程?

c - 使用 ANSI 函数获取文本文件的特定行

c - 如何分割一个字符?

用于小型 Web 服务的 PHP 与 C CGI

php - 如何在 PHP 的 while 循环中求和每个用户的总时间

c - 我如何知道输入是否是字母?我只能接受姓名中的字母和中间首字母的点