boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++之极弱问
相关主题
a simple question for C++ class
请问一个exception题目
两个继承问题
为什么我看不懂下面的code,是不是水平还不够?
C++疑问
two c++ interview questions! (转载)
请教一个作用域的问题
compare double to float
[合集] C++问题(copy constructor)
问一个简单的C++问题
相关话题的讨论汇总
话题: v2话题: cin话题: sum话题: v1话题: endl
进入Programming版参与讨论
1 (共1页)
r**e
发帖数: 339
1
#include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
}
cout<< "sum is "<< sum< //part 2 below
cout<< "imput v2 "< int v2=0;
cin>>v2;
cout< return 0;
}
第一部分读入多个数,然可求和.用字母或 ctrl+z 做结束符时,第二部分没有执行读
入v2,直接输出v2结束,返回零.为啥啊?
b********r
发帖数: 1080
2
错在while()那句.
r**e
发帖数: 339
3
how a

【在 b********r 的大作中提到】
: 错在while()那句.
b********r
发帖数: 1080
4
我猜你用字母或者ctrl-z终断while的时候,不是正常返回0的终断.
不过你还是等大牛们回答吧.

【在 r**e 的大作中提到】
: how a
j*****a
发帖数: 436
5
#include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
cin.get(); //get the space or the return character
}
cin.clear(); // clear out the non-numeric character
cin.get(); // get the return character
cout<< "sum is "<< sum< //part 2 below
cout<< "imput v2 "< int v2=0;
cin>>v2;
cout< return 0;
}
r**e
发帖数: 339
6
重新启动流,got it. thanks.

【在 j*****a 的大作中提到】
: #include
: using namespace std;
: int main()
: {
: //part 1 below
: int sum=0,v1;
: while(cin>>v1)
: {
: sum+=v1;
: cin.get(); //get the space or the return character

H***a
发帖数: 735
7
The error happened at
while(cin>>v1)
Check this out: http://www.cplusplus.com/reference/iostream/istream/operator>>/
"Errors are signaled by modifying the internal state flags:
[flag]
failbit
[error]
The input obtained could not be interpreted as an element of the appropriate
type."
cin is an object of class istream, once error occurs, "an exception of type
ios_base::failure is thrown", cin therefore won't work anymore. That's why
v2 is still 0.

【在 r**e 的大作中提到】
: #include
: using namespace std;
: int main()
: {
: //part 1 below
: int sum=0,v1;
: while(cin>>v1)
: {
: sum+=v1;
: }

1 (共1页)
进入Programming版参与讨论
相关主题
问一个简单的C++问题
一个指向指针的指针的引用?
问个char*的问题
数组弱问
[合集] 关于构造函数
C++菜问: 怎么这样也可以?
C++ 初学者请教一个 iostream 的问题
请教一个c++ reference问题
请教一道入门小题
大家来做题C++。
相关话题的讨论汇总
话题: v2话题: cin话题: sum话题: v1话题: endl