注:文章内容来源于网络,真实性有待确认,请自行甄别。
如何让这个程序在输入非整形数据之后不进入无穷循环状态?#incl?
发表于:2024-10-24 00:00:00浏览:5次
问题描述:如何让这个在输入非整形数据之后不进入无穷循环状态?
#include "stdio.h"
main()
{
int a, d;
srand(time());
d=rand()%100;
scanf("%d", &a);
if(a==d){printf("^_^");}
else
{
do
{
printf("*_*");
scanf("%d", &a);
if(a==d){printf("^_^");}
else{
if(a<d) {printf("%d<x", a);}
如何让这个在输入非整形数据之后不进入无穷循环状态?
#include "stdio.h"
main()
{
int a, d;
srand(time());
d=rand()%100;
scanf("%d", &a);
if(a==d){printf("^_^");}
else
{
do
{
printf("*_*");
scanf("%d", &a);
if(a==d){printf("^_^");}
else{
if(a<d) {printf("%d<x", a);}
else{printf("x<%d", a);}}
}
while(a!=d);
}
getch();
}
这个问题其实不难,只要对scanf的返回值做一个判断并进行处理即可。
下面的代码,请参考:
#include "stdio.h"
main()
{
int a, d,ret;
srand(time());
d=rand()%100;
ret = scanf("%d", &a);
if(ret == 0)
{ printf("Input Error!\n"); getch(); exit(0);}
if(a==d){printf("^_^");}
else
{
do
{
printf("*_*");
ret = scanf("%d", &a);
if(ret == 0)
{ printf("Input Error!\n"); getch(); exit(0);}
if(a==d){printf("^_^");}
else{
if(a
猜你喜欢
- 提问c++的问题#include"iostream.h"void
- #include"tream.h" void main() {int k=0; char c='A'; do{switch(c++) {case'A':k++;break; case'B':k--; case'C':k+=2;break; case'D':k=k%2;continue; case'E':k=k*10;break; /3;} k++;} while(c<'G');cout<<"k=...
栏目分类全部>