fgets是從文件讀取,getchar是從輸入讀取,兩個還是有區(qū)別的
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的浠水網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
懷疑是gets
如果一定要用fgets的話,那么可以先用freopen重定向輸入,來使fgets和getchar的定向相同
以下用gets實現(xiàn)或者被實現(xiàn)
含測試函數(shù)main
#include stdio.h
int str_to_int(char *str)
{
int sign = 1;
int ret = 0;
char *p;
p = str;
if(*p == '+') p++;
else if(*p == '-') sign = -1, p ++;
while(*p)
{
if(*p = '0' *p = '9')
ret = ret * 10 + (*p - '0');
else break;
p ++;
}
return sign * ret;
}
int get_int_with_getchar()
{
char a[20] = {0};
int i = 0;
while(i19)
{
a[i] = getchar();
if(a[i] == '\n')
break;
i ++;
}
a[i] = 0;
return str_to_int(a);
}
int get_int_with_gets()
{
char a[20] = {0};
gets(a);
return str_to_int(a);
}
void gets_with_getchar(char *str)
{
int i = 0;
if(str == NULL) return;
do
{
str[i] = getchar();
}while(str[i++] != '\n' );
}
char *do_strchr(char *src, const char c)
{
char *p = src;
while(*p *p!= c) p ++;
if(*p != c) return NULL;
return p;
}
int do_isdigit(char c)
{
return c = '9' c = '0';
}
int do_isalpha(char c)
{
return (c = 'Z' c = 'A') || (c = 'z' c = 'a');
}
int main()
{
int a;
char test[100] ;
a = get_int_with_getchar();
printf("get_int_with_getchar = %d\n", a);
a = get_int_with_gets();
printf("get_int_with_gets = %d\n", a);
gets_with_getchar(test);
printf("gets_with_getchar = %s\n", test);
printf("do_strchr %s %s\n", do_strchr(test, 'a'), do_strchr(test, 'b'));
printf("do_isdigit = (%d,%d) \n", do_isdigit('1'), do_isdigit('a'));
printf("do_isalpha = (%d,%d) \n", do_isalpha('1'), do_isalpha('a'));
}
cinsth;
while(!check(sth))
{
//check
函數(shù)檢查輸入值,如果滿足就不需要在輸入,這是就返回1或者true
cout"enter
again,
because
it
is
invalidate"
cinsth;
}
應(yīng)該把提示信息修改為“請輸入英文字母字符串”,并且在頭上增加一句包含語句#includectype.h
然后把循環(huán)判斷語句修改為
while(!isalpha(m))