关于模板函数 返回值有点问题,费解

ftkghost 2004-10-24 12:06:15
请各位看看,如果你全都输入负数就会发现问题了。
是因为模板中为Type分配的大小有关?

#include<iostream>
template<class Type>
bool input(Type& inp);

int main()
{
int a1;
double a2;
float a3;

if(input(a1))
std::cout<<a1<<std::endl;
else
std::cout<<"负数:"<<a1<<std::endl;

if(input(a2))
std::cout<<a2<<std::endl;
else
std::cout<<"负数:"<<a2<<std::endl;

if(input(a3))
std::cout<<a3<<std::endl;
else
std::cout<<"负数:"<<a3<<std::endl;

system("Pause");
return 0;
}

template<class Type>
bool input(Type& inp)
{
Type in;
int i=3;
do{
std::cout<<"请输入一个非负数:";
std::cin>>in;
if(in<0){
std::cout<<"输入错误,您还有"<<(i-1)<<"次机会!"<<std::endl;
i--;
continue;
}
else{
inp=in;
return true;
}
}while(i>0);
return false;
}
...全文
124 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ftkghost 2004-10-24
  • 打赏
  • 举报
回复
顶一下
ftkghost 2004-10-24
  • 打赏
  • 举报
回复
Leaveye(~枝)(完全了解系统,也要实际量测性能)

另外还有一处BUG,如果输入的不是数字或小数点,那么到程序结束也不会再等待输入了。

请问这个BUG怎么改?以前也遇到过
难道要加个if语句限制输入只能是数字和小数点?
ftkghost 2004-10-24
  • 打赏
  • 举报
回复
hehe 不好意思 低级错误

a1,a2,a3没有初值

谢谢啊
ftkghost 2004-10-24
  • 打赏
  • 举报
回复
谢谢
zxl_llx 2004-10-24
  • 打赏
  • 举报
回复
我知道了:应当:
template<class Type>
bool input(Type& inp)
{
Type in;
int i=3;
do{
std::cout<<"请输入一个非负数:";
std::cin>>in;
if(in<0){
std::cout<<"输入错误,您还有"<<(i-1)<<"次机会!"<<std::endl;
i--;
inp=in;//<-------------加上这一句,就可以了。
continue;
}
else{
inp=in;
return true;
}
}while(i>0);
return false;
}
Leaveye 2004-10-24
  • 打赏
  • 举报
回复
重置 cin ,这里有段代码,你可以参考一下:
It would be better to prompt again for a correct value. There are two complications to consider in order in doing this. First, once cin gets into an error state, it needs to be reset before reuse. This is done with the istream member function clear. Second, when an input operation fails the bad data is still left in the input stream. The next read will get the value of "A" again. The istream member function ignore can be used to remove this bad data from the input stream.

#include <iostream>
using namespace std;

int main()
{
int age;

cout << "Enter you age: ";
cin >> age;

while (!cin)
{
cin.clear();
//reset cin, so more input can be done
cin.ignore(100,'\n');
//remove bad data, up to 100 characters
//or until the end of line is reached
cout << "Please enter a number for you age: ";
cin >> age;
}
if (age > 21)
{
cout << "You're legal at " << age < }
else
{
cout << "Sorry kid, you're only " << age << endl;
}

return 0;

}

Here's the results from this version.

Enter you age: A
Please enter a number for you age: s
Please enter a number for you age: t
Please enter a number for you age: u
Please enter a number for you age: p
Please enter a number for you age: i
Please enter a number for you age: d
Please enter a number for you age: u
Please enter a number for you age: s
Please enter a number for you age: e
Please enter a number for you age: r
Please enter a number for you age: 23
You're legal at 23

Generally, it is better to anticipate and check for user error on input than to risk program failure or errors.

摘自:http://cplus.about.com/library/weekly/aa030702b.htm
Leaveye 2004-10-24
  • 打赏
  • 举报
回复
在模板函数内,返回假之前,加一句赋值
inp = in;

另外还有一处BUG,如果输入的不是数字或小数点,那么到程序结束也不会再等待输入了。

64,678

社区成员

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

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