使用断言捕捉不应该发生的非法情况

使用断言捕捉不应该发生的非法情况。

不要混淆非法情况与错误情况 之间的区别,后者是必然存在的并且是一定要作出处理的。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6         //输入字符串
 7     char str[80];
 8     printf("str:");      //显示提示
 9     scanf("%s",str);
10     printf("The string:%s",str); 
11 
12     //输入各进制整数
13     int a,b,c,sum;
14     printf("\na\tb\tc\n");            //显示提示
15     scanf("%d %o %x",&a,&b,&c);     //以十进制、八进制、十六进制形式输入数据
16     sum=a+b+c;
17     printf("a=%d  b=%d  c=%d   sum=%d",a,b,c,sum);
18 
19     //输入浮点数并计算显示
20     float x,y;              //声明变量
21     printf("\nx\ty\n");            //显示提示
22     scanf("%f %f",&x,&y);         //对非空白字符"x= y="读入,不保存
23     printf("sum=%f  product=%f\n",x+y, x*y);   //显示表达式的值
24     return 0;
25 }

 

posted @ 2018-08-03 13:27  borter  阅读(196)  评论(0编辑  收藏  举报