cin,iostream问题 [英] cin, iostream problems

查看:74
本文介绍了cin,iostream问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从用户那里得到一条可能是任何东西的线,我是

期待一个字符串后跟一个空格或者只是一个字符串

本身。我尝试过使用string :: getline,stringstreams和所有

种类的cin变体而且不能正确使用它。目前我的程序

停止,等待用户在这段代码中输入一个没有参数的

命令的情况下等待更多输入。我该如何解决?


void菜单()

{

string command;

int argument;


string :: size_type index = string :: npos;


while(command!=" quit")

{

cout<<" \ n以下是有效命令:\ n" ;;

cout<<" read_all <数> - 显示整个邮件标题和正文。\ n" ;;

cout<<" read< number> - 显示消息的主体\ n" ;;

cout<<" del< number> - 删除消息\ n" ;;

cout<<" undel - 撤消所有删除\ n" ;;

cout<<" reply< ;数> - 回复消息\ n" ;;

cout<<" list< number> - 显示下一条10条消息

摘要\ n" ;;

cout<<"退出 - 退出此程序\ n \ n";


cin>>命令;


if(!(cin>>参数))

argument = 0;


cin.clear();

cin.ignore(std :: numeric_limits< std :: streamsize> :: m ax(),' '\ n'');


if(command ==" read_all")

cout<<" not implemented\\\
" ;


else if(command ==" read")

cout<<" not implemented\\\
" ;;


else if(command ==" del")

cout<<" not implemented\\\
" ;;


else if(command ==" undel")

cout<<" not implemented\\\
" ;;


else if(command = =" reply")

cout<<" not implemented\\\
" ;;


else if(command ==&q uot; list")

cout<<" not implemented\\\
" ;;

}

}


谢谢,

克里斯托弗

I am trying to get a line from the user which could be anything, I am
expecting either a string followed by a whitespace or just a string by
itself. I have tryed using string::getline, stringstreams, and all
sorts of cin variations and can''t get it right. Currently my program
stops, waiting for more input in the case the user just enters a
command with no argument, in this block of code. How can I fix it up?

void Menu()
{
string command;
int argument;

string::size_type index = string::npos;

while(command != "quit")
{
cout<<"\nThe Following are valid commands:\n";
cout<<"read_all <number> -- display entire mail header and body.\n";
cout<<"read <number> -- display body of message\n";
cout<<"del <number> -- delete message\n";
cout<<"undel -- undo all deletion\n";
cout<<"reply <number> -- reply to message\n";
cout<<"list <number> -- display the next 10 message
summaries\n";
cout<<"quit -- exit this program\n\n";

cin>>command;

if(!(cin>>argument))
argument = 0;

cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::m ax(), ''\n'');

if(command == "read_all")
cout<<"not implemented\n";

else if(command == "read")
cout<<"not implemented\n";

else if(command == "del")
cout<<"not implemented\n";

else if(command == "undel")
cout<<"not implemented\n";

else if(command == "reply")
cout<<"not implemented\n";

else if(command == "list")
cout<<"not implemented\n";
}
}

Thanks,
Christopher

推荐答案

2004年4月29日15:24:55 -0700, cp****@swt.edu (Christopher)写道:
On 29 Apr 2004 15:24:55 -0700, cp****@swt.edu (Christopher) wrote:
我是试图从用户那里得到一条可能是任何东西的一行,我期待一个字符串后面跟一个空格或者只是一个字符串本身就是
。我尝试过使用string :: getline,stringstreams以及所有类似的cin变体而无法正确使用它。目前我的程序停止,等待用户在这段代码中输入没有参数的
命令的情况下等待更多输入。我怎样才能解决这个问题?
I am trying to get a line from the user which could be anything, I am
expecting either a string followed by a whitespace or just a string by
itself. I have tryed using string::getline, stringstreams, and all
sorts of cin variations and can''t get it right. Currently my program
stops, waiting for more input in the case the user just enters a
command with no argument, in this block of code. How can I fix it up?




在任何初始命令之后它停止并等待参数,甚至是

不要争论。这可以很容易地通过

来修复你的逻辑来处理args的命令与

那些没有args的命令不同。这是朝着正确方向迈出的一步(但是还有

仍有缺陷,我会在之后提到):


#include< iostream> ;

#include< string>

使用命名空间std;


void Menu()

{

string command;

int argument;


// string :: size_type index = string :: npos;


while(命令!="退出")

{

cout<<" \ n以下是有效的命令:\ n" ;;

cout<<" read_all< number> - 显示整个邮件标题和

body.\ n" ;;

cout<<" read< number> - 显示消息的主体\ n" ;;

cout<<" del< number> - 删除消息\ n" ;;

cout<<" undel - 撤消所有删除\ n" ;;

cout<<" reply< ;数> - 回复消息\ n" ;;

cout<<" list< number> - 显示下一条10条消息

摘要\ n" ;;

cout<<"退出 - 退出此程序\ n \ n";


cin>>命令;


if(command ==" undel")

cout<< ;未实施\ n;;

否则if(command ==" quit")

return;

else

{

if(!(cin>>参数))

argument = 0;


cin.clear();

cin.ignore(std :: numeric_limits< std :: streamsize> :: m ax(),''\ n'');


if(command ==" read_all")

cout<<" not implemented\\\
" ;;


else if(command ==" read")

cout<<" not implemented\\\
" ;;


else if(command ==" ; del")

cout<<" not implemented\\\
" ;;


else if(command ==" reply")

cou t<<" not implemented\\\
";


else if(command ==" list")

cout<<"未实施\ n" ;;


其他

cout<< 无效的命令!\ n" ;;


}

}

}

int main()

{

菜单();

返回0;

}

现在的问题是,如果用户输入了无效的命令,它仍然会在诊断之前等待那个arg。如果是我,我会为需要它的每个案例手动处理

。你会有更多的

代码,但至少它会是直截了当的。或者,你可以通过调用一个函数来处理命令名来处理命令名,这个函数将它与一个

有效字符串相匹配(可能返回一个表示命令的枚举),并且

还将命令分类为接受或不接受arg(可能通过函数集的

bool引用参数)。然后你会在通话结束后查看

bool'的价值,看看是否尝试
消费一个参数。

-leor


-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html




" Leor Zolman" <乐** @ bdsoft.com>在消息中写道

news:0b ******************************** @ 4ax.com ...

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:0b********************************@4ax.com...
2004年4月29日15:24:55 -0700, cp****@swt.edu (克里斯托弗)写道:
On 29 Apr 2004 15:24:55 -0700, cp****@swt.edu (Christopher) wrote:
我试图从用户那里得到一条可能是任何东西的线,我是
期待一个字符串后跟一个空格或只是一个字符串由
本身。我尝试过使用string :: getline,stringstreams以及所有类似的cin变体而无法正确使用它。目前我的程序停止,等待用户在这段代码中输入没有参数的
命令的情况下等待更多输入。我怎样才能解决这个问题?
在任何初始命令之后它停止并等待争论,甚至是那些不参与争论的人。这可以很容易地解决
I am trying to get a line from the user which could be anything, I am
expecting either a string followed by a whitespace or just a string by
itself. I have tryed using string::getline, stringstreams, and all
sorts of cin variations and can''t get it right. Currently my program
stops, waiting for more input in the case the user just enters a
command with no argument, in this block of code. How can I fix it up?
It is stopping and waiting for an argument after any initial command, even
the ones that don''t take an argument. This can be remedied easily enough



,只需修改逻辑来处理带有args的命令,而不是那些没有args的命令。这是朝着正确方向迈出的一步(但仍有瑕疵,我后来​​会提到):

#include< iostream>
#include< ; string>
使用命名空间std;

void菜单()
{
字符串命令;
int参数;

// string :: size_type index = string :: npos;

while(command!=" quit")
{
cout<<" \ n是有效的命令:\ n" ;;
cout<<" read_all< number> - 显示整个邮件标题和
body.\ n" ;;
cout<<" read< number> - 显示消息的主体\ n" ;;
cout<<" del< number> - 删除消息\ n" ;;
cout<<" undel - 撤消所有删除\ n" ;;
cout<<" reply< number> - 回复消息\ n" ;;
cout<<" list< number> - 显示下一条10条消息
摘要\ n";
cout<<<"退出 - 退出此程序\ n \\ nn;   

cin> ;>命令;

if(command ==" undel")
cout<<" not implemented\\\
" ;;
else if(command == 退出)
返回;

{
if(!(cin>> argument))
argument = 0;

cin.ignore(std :: numeric_limits< std :: streamsize> :: m ax(),''\ n'');

if(command ==" read_all")
cout<<"" not implemented\\\
" ;;

else if(command ==" read")
cout<<<" not implemented\\\
" ;;

if if(command ==" del")
cout<< ;未实施\ n;

否则if(command ==" reply")
cout<<" not implemented\\\
" ;;

if if(command ==" list")
cout<<"" not implemented\\\
" ;;


cout<< 无效的命令!\ n";

}
}
}
int main()
{
菜单();
返回0;
}

现在的问题是,如果用户输入无效命令,它仍然会等待那个arg在诊断之前。


是的,我尝试过这种方式,这就是我抛出它的原因

..如果是我,我会手动处理
每个需要它的案例的arg。你会有更多的代码行,但至少它会是直截了当的。


我很乐意,这就是我通常会这样做的方式,但导师

不会接受。他希望我们像1970年那样解析一系列输入。

我会使用char数组并以旧时尚方式进行,但我讨厌c风格

字符串,而且更愿意找出是否有办法判断

是什么

坐在cin流中然后将其粘贴到参数变量中如果它

以c ++方式存在。

或者,您可以通过调用将其与有效字符串匹配的函数来处理命令名称(可能返回表示命令的枚举)和
,它还将命令分类为采用或不采用arg(可能通过函数集的
bool引用参数)。然后你会在通话后检查
bool'的价值,看看是否试图消费一个论点。
-leor


by simply modifying your logic to treat commands with args differently from
those without args. Here''s a step in the right direction (but there are
still flaws, which I''ll mention afterwards):

#include <iostream>
#include <string>
using namespace std;

void Menu()
{
string command;
int argument;

// string::size_type index = string::npos;

while(command != "quit")
{
cout<<"\nThe Following are valid commands:\n";
cout<<"read_all <number> -- display entire mail header and
body.\n";
cout<<"read <number> -- display body of message\n";
cout<<"del <number> -- delete message\n";
cout<<"undel -- undo all deletion\n";
cout<<"reply <number> -- reply to message\n";
cout<<"list <number> -- display the next 10 message
summaries\n";
cout<<"quit -- exit this program\n\n";

cin>>command;

if(command == "undel")
cout<<"not implemented\n";
else if(command == "quit")
return;
else
{
if(!(cin>>argument))
argument = 0;

cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::m ax(), ''\n'');

if(command == "read_all")
cout<<"not implemented\n";

else if(command == "read")
cout<<"not implemented\n";

else if(command == "del")
cout<<"not implemented\n";

else if(command == "reply")
cout<<"not implemented\n";

else if(command == "list")
cout<<"not implemented\n";

else
cout << "Invalid command!\n";

}
}
}
int main()
{
Menu();
return 0;
}
The problem now is that if the user enters an invalid command, it''ll still
wait for that arg before diagnosing it.
Yea I tryed that way and that''s why I tossed it out
.. If it were me, I''d manually deal
with the arg for each case that requires it. You''ll have more lines of
code, but at least it''ll be straight-forward.
I''d love to and that''s the way I would normally do it, but the instructor
won''t accept that. He wants us to parse a line of input like it was 1970.
I''d use a char array and do it the old fashion way but I hate c style
strings and would much rather find out if there is a way to tell if
something
is sitting in the cin stream then stick it into the argument variable if it
exists in a c++ fashion.
Alternatively, you can
process just the command name by calling a function that matches it to a
valid string (perhaps returning an enum representing the command), and that also categorizes the command as taking or not taking an arg (perhaps via a
bool reference argument the function sets). Then you''d just check that
bool''s value after the call to find out whether or not to attempt to
consume an argument.
-leor



如果用户愚蠢并且没有参与辩论,它仍然会挂起。我是

旨在获得整条线

,然后看看是否存在争论,如果它不是或它

不是数字然后给出错误信息。


虽然赞赏输入,

Chris



It would still hang if the user was dumb and didn''t enter an argument. I am
aiming for getting the whole line
and then seeing if an argument exists when it should and if it isn''t or it
isn''t a number then giving an error message.

Appreciate the input though,
Chris


2004年4月30日星期五00:03:15格林威治标准时间,克里斯托弗 <一个** @ nospam.net>写道:

On Fri, 30 Apr 2004 00:03:15 GMT, "Christopher" <an**@nospam.net> wrote:

如果是我,我会手动处理每个需要它的案件的arg 。你会有更多的代码,但至少它会是直截了当的。
If it were me, I''d manually deal
with the arg for each case that requires it. You''ll have more lines of
code, but at least it''ll be straight-forward.



我很乐意,那就是我通常会这样做,但教练
不会接受。他希望我们像1970年那样解析一系列输入。
我会使用char数组并以旧时尚的方式进行,但我讨厌c风格的字符串,而且更愿意找出是否有一种方法可以判断
是否存在于cin流中,然后将其粘贴到参数变量中,如果它以c ++方式存在。



I''d love to and that''s the way I would normally do it, but the instructor
won''t accept that. He wants us to parse a line of input like it was 1970.
I''d use a char array and do it the old fashion way but I hate c style
strings and would much rather find out if there is a way to tell if
something
is sitting in the cin stream then stick it into the argument variable if it
exists in a c++ fashion.




如果你想像1970那样做,(好吧,1980年更有可能,至少如果你不是让他们不是丹尼斯·里奇......)那么 ;想想fgets和sscanf,但用

iostreams做吧!只需使用getline将整行读入std :: string,然后

在输入后开始分析。您可以使用< sstream>设施

从输入字符串创建一个istringstream,然后从

中提取,并在每次提取后测试流状态。

祝你好运,

-leor

-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


这篇关于cin,iostream问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆