|
查看: 1167|回复: 9
|
C++ help帮忙下
[复制链接]
|
|
|
如何避免user 键入英文字在INT 里面?
这是我的code,我想使它更完美
#include <iostream.h>
void main()
{
double x,y,z;
cout<<"Please Enter Your Assignment 1 mark ! (100%)"<<endl;
cin>>x;
while(x>100 || x<0)
{
cout<<"Please Enter Your x marks (100%)"<<endl;
cin>>x;
}
cout<<"Please Enter Your y mark ! (100%)"<<endl;
cin>>y;
while(y>100 || y<0)
{
cout<<"Please Enter Your y mark ! (100%)"<<endl;
cin>>y;
}
z=x+y
cout<<"z<<endl;
}
[ 本帖最后由 风之女神 于 14-6-2009 01:16 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 13-6-2009 09:02 PM
|
显示全部楼层
回复 1# 风之女神 的帖子
第一:你使用int,那万一user输入55.6,你要怎么办?
第二:我建议使用standard format
#include <iostream>
using namespace std;
int main()
{
system("pause");
return 0;
}
因为新的MS V C++的main一定要set type。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 14-6-2009 01:26 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 14-6-2009 12:46 PM
|
显示全部楼层
回复 2# 天使乐园院长 的帖子
一个"standard"的main (console entry) 不止要有return type, 还需要两个parameter, 一个记录多少个parameter passed in, 另一个是vector of parameter
回楼主:
去看看scanf 吧。。我不喜欢喂饭 |
|
|
|
|
|
|
|
|
|
|
发表于 14-6-2009 01:32 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 16-6-2009 12:29 AM
|
显示全部楼层
回复 4# yeenfei 的帖子
scanf 避免输入非 int ? 如何?
我一般是input to string, isdigit 检查, 在 stringstream 转换回int |
|
|
|
|
|
|
|
|
|
|
发表于 16-6-2009 12:40 AM
|
显示全部楼层
|
scanf(" %[0-9]", whatever ); ?? |
|
|
|
|
|
|
|
|
|
|
发表于 19-6-2009 06:45 PM
|
显示全部楼层
string s;
int number = 0;
cin >> s;
for(int i=0; i<s.length(); i++)
{
if(s.at(i) >= '0' && s.at(i) <= '9')
number = 10*number + s.at(i)-48;
else
{
cout << "non-integer input" << endl;
break;
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 19-6-2009 10:17 PM
|
显示全部楼层
原帖由 onlylonly 于 16-6-2009 12:29 AM 发表 
scanf 避免输入非 int ? 如何?
我一般是input to string, isdigit 检查, 在 stringstream 转换回int
- if(scanf("%d", buffer))
- {
- //integer read..
- }
- else
- {
- //non integer read
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 1-7-2009 11:37 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|