首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

用gets函数输入字符串时的程序的笑有关问题

2012-05-16 
用gets函数输入字符串时的程序的笑问题// l2_4.cpp : Defines the entry point for the console applicati

用gets函数输入字符串时的程序的笑问题
// l2_4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <string.h>
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{
/*int i;
int j=0;
char *c;
for(i=0;(c[i]=getchar())!='#';i++)
j++;
printf("%d",sizeof(c));*/
   
  char s[100];
int na;
printf("0:English\n1:汉语\n");
scanf("%d",&na);
if(0==na)
{
while(1)
{
printf("Please Input s Is:");

gets(s);
if(strcmp("int",s)==0)
printf("The Size of %s is %d\n",s,sizeof(int));
else if(strcmp("short",s)==0)
printf("The Size of %s is %d\n",s,sizeof(short));
else if (strcmp("long",s)==0)
printf("The Size of %s is %d\n",s,sizeof(long));
else if(strcmp("float",s)==0)
printf("The Size of %s is %d\n",s,sizeof(float));
else if(strcmp("double",s)==0)
printf("The Size of %s is %d\n",s,sizeof(double));
else if(strcmp("unsigned long",s)==0)
printf("The Size of %s is %d\n",s,sizeof(unsigned long));
else if(strcmp("signedlong",s)==0)
printf("The Size of %s is %d\n",s,sizeof(signed long));
else if(strcmp("unsignedshortint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(unsigned short int));
else if(strcmp("signedshortint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(signed short int));
else if(strcmp("unsignedlongint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(unsigned long int));
else if(strcmp("signedlongint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(signed long int));
else if(strcmp("signedint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(signed int));
else if(strcmp("unsignedint",s)==0)
printf("The Size of %s is %d\n",s,sizeof(unsigned int));
else if(strcmp("longdouble",s)==0)
printf("The Size of %s is %d\n",s,sizeof(long double));

else if(strcmp("quit",s)==0)
return 0;
else 
printf("illegal input\n");
}
}

else 
{
while(1)
{
printf("请输入:");
/*scanf("%s",&s);*/
gets(s);
if(strcmp("int",s)==0)
printf("int的字节长度是: %d\n",sizeof(int));
else if(strcmp("short",s)==0)
printf("short的字节长度是:%d\n",sizeof(short));
else if (strcmp("long",s)==0)
printf("long的字节长度是:%d\n",sizeof(long));
else if(strcmp("float",s)==0)
printf("float的字节长度是:%d\n",sizeof(float));
else if(strcmp("double",s)==0)
printf("double的字节长度是:%d\n",sizeof(double));



else if(strcmp("unsignedlong",s)==0)
printf("unsigned long的字节长度是:%d\n",sizeof(unsigned long));
else if(strcmp("signedlong",s)==0)
printf("signed long的字节长度是:%d\n",s,sizeof(signed long));
else if(strcmp("unsignedshortint",s)==0)
printf("unsigned short int的字节长度是:%d\n",sizeof(unsigned short int));
else if(strcmp("signedshortint",s)==0)
printf("signed short int的字节长度是:%d\n",sizeof(signed short int));
else if(strcmp("signedlongtint",s)==0)
printf("signed long int的字节长度是:%d\n",sizeof(signed long int));
else if(strcmp("signedlongint",s)==0)
printf("signed long int的字节长度是:%d\n",sizeof(signed long int));


else if(strcmp("signedint",s)==0)
printf("signed int的字节长度是:%d\n",sizeof(signed int));
else if(strcmp("unsignedint",s)==0)
printf("unsigned int的字节长度是:%d\n",sizeof(unsigned int));
else if(strcmp("longdouble",s)==0)
printf("long double的字节长度是:%d\n",s,sizeof(long double));



else if(strcmp("quit",s)==0)

return 0;
else 
printf("非法输入\n");
}


}
 



_getch();

return 0;
}

帮我看看这个程序,就开始运行的时候会首先输出一句illegal input,我不知道怎么解决,请帮我解决下,本人将不胜感激

[解决办法]

C/C++ code
#include "stdafx.h"#include <conio.h>#include <string.h>#include <stdio.h>int _tmain(int argc, _TCHAR* argv[]){    char s[100];    int na;    printf("0:English\n1:汉语\n");    scanf("%d",&na);    if(0==na)    {        while(1)        {            printf("Please Input s Is:");            getchar();            gets(s);            if(strcmp("int",s)==0)                printf("The Size of %s is %d\n",s,sizeof(int));            else if(strcmp("short",s)==0)                printf("The Size of %s is %d\n",s,sizeof(short));            else if (strcmp("long",s)==0)                printf("The Size of %s is %d\n",s,sizeof(long));            else if(strcmp("float",s)==0)                printf("The Size of %s is %d\n",s,sizeof(float));            else if(strcmp("double",s)==0)                printf("The Size of %s is %d\n",s,sizeof(double));            else if(strcmp("unsigned long",s)==0)                printf("The Size of %s is %d\n",s,sizeof(unsigned long));            else if(strcmp("signedlong",s)==0)                printf("The Size of %s is %d\n",s,sizeof(signed long));            else if(strcmp("unsignedshortint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(unsigned short int));            else if(strcmp("signedshortint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(signed short int));            else if(strcmp("unsignedlongint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(unsigned long int));            else if(strcmp("signedlongint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(signed long int));            else if(strcmp("signedint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(signed int));            else if(strcmp("unsignedint",s)==0)                printf("The Size of %s is %d\n",s,sizeof(unsigned int));            else if(strcmp("longdouble",s)==0)                printf("The Size of %s is %d\n",s,sizeof(long double));            else if(strcmp("quit",s)==0)                return 0;            else                  printf("illegal input\n");        }    }    else      {        while(1)        {            printf("请输入:");            getchar();            gets(s);            if(strcmp("int",s)==0)                printf("int的字节长度是: %d\n",sizeof(int));            else if(strcmp("short",s)==0)                printf("short的字节长度是:%d\n",sizeof(short));            else if (strcmp("long",s)==0)                printf("long的字节长度是:%d\n",sizeof(long));            else if(strcmp("float",s)==0)                printf("float的字节长度是:%d\n",sizeof(float));            else if(strcmp("double",s)==0)                printf("double的字节长度是:%d\n",sizeof(double));            else if(strcmp("unsignedlong",s)==0)                printf("unsigned long的字节长度是:%d\n",sizeof(unsigned long));            else if(strcmp("signedlong",s)==0)                printf("signed long的字节长度是:%d\n",s,sizeof(signed long));            else if(strcmp("unsignedshortint",s)==0)                printf("unsigned short int的字节长度是:%d\n",sizeof(unsigned short int));            else if(strcmp("signedshortint",s)==0)                printf("signed short int的字节长度是:%d\n",sizeof(signed short int));            else if(strcmp("signedlongtint",s)==0)                printf("signed long int的字节长度是:%d\n",sizeof(signed long int));            else if(strcmp("signedlongint",s)==0)                printf("signed long int的字节长度是:%d\n",sizeof(signed long int));            else if(strcmp("signedint",s)==0)                printf("signed int的字节长度是:%d\n",sizeof(signed int));            else if(strcmp("unsignedint",s)==0)                printf("unsigned int的字节长度是:%d\n",sizeof(unsigned int));            else if(strcmp("longdouble",s)==0)                printf("long double的字节长度是:%d\n",s,sizeof(long double));            else if(strcmp("quit",s)==0)                return 0;            else                  printf("非法输入\n");        }    }    _getch();    return 0;} 


[解决办法]
你这程序是win32的,那就在开头改成这样.

C/C++ code
scanf("%d",&na);fflush(stdin); 

热点排行