实验三记录

来源:互联网 发布:盘亏数据分析 编辑:程序博客网 时间:2024/04/27 00:15

为了解决输入不符合条件的字符会出现死循环问题

scanf("%s",tmpstr);

int scanf( const char *format, ... ); 格式化输入函数,它从标准输入设备(键盘) 读取输入的信息。 
  其调用格式为: scanf("<格式化字符串>",<地址表>);
  函数 scanf() 是从标准输入流 stdin 中读内容的通用子程序,可以读入全部固有类型的数据并自动转换成机内形式。
scanf("%d",&a[i])
在键盘上输入一个整数,并且把这个整数放到数组a[i]中。%d整数;&a[i]:a[i]取地址
#include
using namespace std;
main()
{int n;
char m;
char tmpstr[100];
printf("please input :");
scanf("%s",tmpstr);
int i=0;
while(tmpstr[i]!='\0')
   if(!isdigit(tmpstr[i])) break;
if(tmpstr[i]=='\0')
  n = atoi(tmpstr);
else
  printf("error input\n");
}

0 0