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 | |
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; : }
|