| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1495 人关注过本帖
标题:[求助]怎么退出这个循环?
只看楼主 加入收藏
jiaju111
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2007-3-27
收藏
得分:0 

我也是这么想的

谢谢啦


Everything is gonna be okay!
2007-05-10 10:24
weishj
Rank: 1
等 级:新手上路
威 望:2
帖 子:141
专家分:0
注 册:2007-4-22
收藏
得分:0 

我感觉讨论如何操作才能退出这个循环应该不在编程的范畴了吧.LZ要真想要退出这个循环的操作方法,除按两次Ctrl+Z外,还有以下方法
1.Ctrl+Pause Break
2.Alt+F4
3.点击关闭按钮
4.结束进程
5.重启计算机
其效果跟按两次Ctrl+z等价
但要是这样的话该贴似乎不应该出现在编程论坛
LZ写那个程序的本意貌似一直让用户输入字符串,并显示在屏幕上,直到输入的为空字符串为止退出循环
但getline函数好象不返回0值或者bool false,所以我感觉研究一下getline比讨论如何操作来退出循环意义要大的多
连按两次Ctrl+z的效果跟七楼的程序差不多吧.
与其连按两次Ctrl+z,倒还不如这样
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int n,i=0;
cout<<"想输入几次:"<<endl;
cin>>n;
while(i<n)
{
cin>>s;
cout<<s<<endl;
i++;
}
return 0;
}


If you shed tears when you miss the sun, you also miss the stars.
2007-05-10 10:37
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

ctrl+z 回车就可以退出来了吧 (VC++2005下)
如LS所说,研究一下getline比讨论如何操作来退出循环意义要大的多,要退出程序有很多的方法。

我们可以不用全局名称空间,下个程序可以参考一下
#include <string>
#include <iostream>
int main () {
std::string s,s2;
std::getline(std::cin,s);
std::getline(std::cin,s2);
std::cout << s <<'\t'<< s2 << std::endl;
return 0;
}


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-10 12:55
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

在不同的编译下结果会有点区别:
VC++6.0:
INPUT:
Hello enter
World enter
Why? enter
OUTPUT:
Hello World //Why?被丢弃了

VC++2005:
input:
Hello enter
World enter

OUTPUT:
Hello World 不知道这是不是VC++6.0的一个BUG。

下面为MSDN的资料:
getline
Parameters
_Istr
The input stream from which a string is to be extracted.

_Str
The string into which are read the characters from the input stream.

_Delim
The line delimiter.

Return Value
The first function returns getline( _Istr, _Str, _Istr.widen( '\n' ) ).

The second function replaces the sequence controlled by _Str with a sequence of elements extracted from the stream _Istr.

Remarks
In order of testing, extraction stops:

At end of file.

After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.

After the function extracts str.max_size elements, in which case the function calls setstate(ios_base::failbit).

If the function extracts no elements, it calls setstate(failbit). In any case, it returns _Istr.




Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-10 13:10
jiaju111
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2007-3-27
收藏
得分:0 
可能是VC6.0的BUG吧

正常的话应该用文件结束符Ctrl+z 就能结束程序

Everything is gonna be okay!
2007-05-11 18:27
快速回复:[求助]怎么退出这个循环?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030666 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved