while语句问题请高手赐教

lzgaofei 2005-05-02 07:17:54
#include"iostream.h"
#include"math.h"
#include"conio.h"
void main()
{int a,i,j,k; //a:总宽度,i:行数,j:每行字符个数
cout<<"输入宽度";
cin>>a;
cout<<endl;
//*************************************************************
while(a<1||fmod(a,2)==0)
{ cout<<"请输入奇数";
cin>>a;
cout<<endl;
}}

当运行时输入整数时正常,

当输入其它字符时如字母就出现死循环

不知为何

...全文
298 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
frank_dong 2005-05-13
  • 打赏
  • 举报
回复
而且,如果进入循环了,你的a又已经赋了一个可以进入循环的值,那么它当然就进入死循环了阿
frank_dong 2005-05-13
  • 打赏
  • 举报
回复
如果输入的是字母并且asc吗为偶数的话就应该进入循环吧
gangliao 2005-05-13
  • 打赏
  • 举报
回复
有一个函数叫integer,你先设一个字符变量,再用a等于这个字符变量。就可以解决.string s
cin>>s;a=integer(s)就行。
Harvey_s 2005-05-13
  • 打赏
  • 举报
回复
感觉好像是你所用的调试系统,与你所写程序要用的调试系统上出的问题!
the777 2005-05-10
  • 打赏
  • 举报
回复
使用异常处理,但不是用IF ELSE 语句,而是用 try throw catch 做出错误处理就OK了,不管你输入的是整型还是字符型都可以正确做出判断。
dzw2004 2005-05-10
  • 打赏
  • 举报
回复
up~
winwhilse 2005-05-10
  • 打赏
  • 举报
回复
cin>>a; 换成  a=int(getchar());
别忘记在头文件列表中加上
#include "stdio.h"
关键是a不是字符型。
ludf 2005-05-09
  • 打赏
  • 举报
回复
我运行了一下你的程序,发现你在上边把a定义为整形.所以你在下边当然不能输入小数了.下边是我改过的程序:#include"iostream.h"
#include"math.h"
#include"conio.h"
void main()
{
float a,i,j,k; //a:总宽度,i:行数,j:每行字符个数
cout<<"输入宽度";
cin>>a;
cout<<endl;
//*************************************************************
while(a<1||fmod(a,2)==0)
{
cout<<"请输入奇数";
cin>>a;
cout<<endl;
}
}
lingzantia 2005-05-09
  • 打赏
  • 举报
回复
3Q,能讲解以下为什么换成INT_MAX就可以了吗?
flying_dancing 2005-05-09
  • 打赏
  • 举报
回复
#include<iostream>
#include<cmath>
#include"conio.h"
using namespace std;

void main()
{
int a, i, j, k; //a:总宽度,i:行数,j:每行字符个数
cout << "输入宽度";
cin >> a;
cout << endl;
//*************************************************************
while ( a < 1 || fmod(a,2) == 0 )
{
cout << "请输入奇数";
//cin>>a;
if (cin >> a)
{
cout << endl;
}
else
{
cin.clear();
cin.ignore(INT_MAX, '\n');//这改一下就KO
}
}
cin.get();
}
lingzantia 2005-05-09
  • 打赏
  • 举报
回复
去除 //cin >> a; ?
还是一样啊!
afst37 2005-05-09
  • 打赏
  • 举报
回复
我都知道是要异常处理,^_^
凌雯 2005-05-09
  • 打赏
  • 举报
回复
楼上的方法进行判断
只是用一个注解要去除就是这个//cin>>a;
lingzantia 2005-05-09
  • 打赏
  • 举报
回复
错误提示:


--------------------Configuration: sd - Win32 Debug--------------------
Compiling...
sd.cpp
C:\Documents and Settings\flying\桌面\临时调试\sd.cpp(24) : error C2065: 'numric_limits' : undeclared identifier
C:\Documents and Settings\flying\桌面\临时调试\sd.cpp(24) : error C2275: 'streamsize' : illegal use of this type as an expression
e:\microsoft visual studio\vc98\include\iosfwd(21) : see declaration of 'streamsize'
C:\Documents and Settings\flying\桌面\临时调试\sd.cpp(24) : error C2039: 'max' : is not a member of '`global namespace''
C:\Documents and Settings\flying\桌面\临时调试\sd.cpp(24) : error C2065: 'max' : undeclared identifier
Error executing cl.exe.

sd.exe - 4 error(s), 0 warning(s)
lingzantia 2005-05-09
  • 打赏
  • 举报
回复
还是有错误啊,改后的程序如下:

#include<iostream>
#include<cmath>
#include"conio.h"
using namespace std;

void main()
{
int a, i, j, k; //a:总宽度,i:行数,j:每行字符个数
cout << "输入宽度";
cin >> a;
cout << endl;
//*************************************************************
while ( a < 1 || fmod(a,2) == 0 )
{
cout << "请输入奇数";
//cin>>a;
if (cin >> a)
{
cout << endl;
}
else
{
cin.clear();
cin.ignore(numric_limits<streamsize>::max(), '\n');
}
}
cin.get();
}
tsocpp 2005-05-03
  • 打赏
  • 举报
回复
加个简单的异常处理就可以拉,这要养成习惯
yuhjnm_20001 2005-05-03
  • 打赏
  • 举报
回复
mark
学习
jackyhubin 2005-05-03
  • 打赏
  • 举报
回复
编程要考虑异常情况的
zhousqy 2005-05-02
  • 打赏
  • 举报
回复
加判断
junnyfeng 2005-05-02
  • 打赏
  • 举报
回复
因为变量设定是整型嘛,可以用类似楼上的方法进行判断
加载更多回复(2)

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧