2015-10-17 89 views
-1

如何在while(1)循环中放置isalpha和isdigit?c isalpha和isdigit while循环

#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"); 
    } 
} 

我尝试添加而(1)在代码的开始,并在代码的最后关闭它,但只要我输入数字或字母的控制台崩溃......可能有人请帮助我这个?

+2

更换

int i; 

你是什么意思与 “控制台崩溃”? '我'在哪里申报,你如何初始化它? – Downvoter

+1

请告诉我们完整的代码。 – guntbert

+0

.exe文件崩溃。我在学C,所以这可能不是做事的最佳方式。 – mypoint

回答

2

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

int i=0;