C++ integer cheaking
来源:百度知道 编辑:UC知道 时间:2024/06/09 16:51:56
如果我什麼都不输入,只系一直按Enter就会一直空行,
有没有方法可以做到每按一次Enter
如果没有输入任东西都会输出"Enter a number:"?
#include <iostream>
#include <limits>
int main(){
using namespace std;
int num;
bool bad;
do {
bad=0;
cout <<"\nEnter a number :";
cin >> num;
if(!cin.good()){
bad=1;
cin.clear();
cin.ignore(numeric_limits<int>::max(),'\n');
cout << "This is not valid value" << endl;
}
}while(bad);
system("pause");
return 0;
}
fuxuwei:
如果用getline联合数字检查要如何写?可以给个例子吗?
非数字[0-9]->重输入,空白或零输入->重输入
有没有方法可以做到每按一次Enter
如果没有输入任东西都会输出"Enter a number:"?
#include <iostream>
#include <limits>
int main(){
using namespace std;
int num;
bool bad;
do {
bad=0;
cout <<"\nEnter a number :";
cin >> num;
if(!cin.good()){
bad=1;
cin.clear();
cin.ignore(numeric_limits<int>::max(),'\n');
cout << "This is not valid value" << endl;
}
}while(bad);
system("pause");
return 0;
}
fuxuwei:
如果用getline联合数字检查要如何写?可以给个例子吗?
非数字[0-9]->重输入,空白或零输入->重输入
你可以先接收字符串
用cin.getline(char * strnum,int)
然后 判断strlen(strnum)==0
再将strnum转成num
给你个示例,逻辑你自己写啊
char strnum[10];
cin.getline(strnum,10) ;
if(strlen(strnum)==0)
cout<<"输入为空";
int num=0;
for(int i=0;i<strlen(strnum);i++)
{
if((int)strnum[i]<48||(int)strnum[i]>57)
{
cout<<"输入的不为数字";
return 0;
}
num =num*10+(int)strnum[i]-48;
}
cout<<num;
return 0;
Dim a As Integer, b As Integer, c As Integer, aa As Integer, bb As Integer
c语言若有以下定义: tyepdef int *INTEGER; INTEGER p, *q
Integer.toString()
integer.parseInt()
integer问题
我下载了一个C程序中用integer,doublecomplex之类的数据类型,
C编程问题:reverse the digits in a user-input positive integer
Dim i As Integer,j As Integer
Integer.valueof() Integer.parseInt() 有什么区别?
CHE文件如何打开