佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1166|回复: 9

C++ help帮忙下

[复制链接]
发表于 13-6-2009 06:11 PM | 显示全部楼层 |阅读模式
如何避免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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
原帖由 天使乐园院长 于 13-6-2009 09:02 PM 发表
第一:你使用int,那万一user输入55.6,你要怎么办?
第二:我建议使用standard format
#include
using namespace std;

int main()
{
   
system("pause";
return 0;
}

因为新的MS V C++的main一 ...



还是不明白!能写出来吗?
回复

使用道具 举报

发表于 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 | 显示全部楼层

回复 4# yeenfei 的帖子

谢谢您的指导。。。
我会好好去了解。
回复

使用道具 举报

发表于 16-6-2009 12:29 AM | 显示全部楼层

回复 4# yeenfei 的帖子

scanf 避免输入非 int ? 如何?

我一般是input to string, isdigit 检查, 在 stringstream 转换回int
回复

使用道具 举报

Follow Us
发表于 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;
        }
    }
回复

使用道具 举报


ADVERTISEMENT

发表于 19-6-2009 10:17 PM | 显示全部楼层
原帖由 onlylonly 于 16-6-2009 12:29 AM 发表
scanf 避免输入非 int ? 如何?

我一般是input to string, isdigit 检查, 在 stringstream 转换回int

  1. if(scanf("%d", buffer))
  2. {
  3. //integer read..
  4. }
  5. else
  6. {
  7. //non integer read
  8. }
复制代码
回复

使用道具 举报

发表于 1-7-2009 11:37 AM | 显示全部楼层

回复 2# 天使乐园院长 的帖子

转成int,做检查就好了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2026 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-4-2026 03:13 PM , Processed in 0.077799 second(s), 13 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表