继续输入(Y/N)控制语句的问题

priestmoon 2008-05-21 07:30:46
不知为何,下面的代码似乎不能实现 "继续输入?(Y/N)"的功能。
哪位能分析一下错误原因,并作出适当的修改?

#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
//to display integer i,j and i+j

while(true)
{
cout<<"continue?(Y/N) :"; //press 'Y' if being to continue input.
//otherwise, jump out.
if( !(getchar()=='Y'&&getchar()=='y') )
//There seems to be something wrong with the line above.
break;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
}

return 0;
}
...全文
248 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
priestmoon 2008-05-22
  • 打赏
  • 举报
回复

哪位能解释一下错误原因?
zsxcn 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )应该改成
if( !(getchar()=='Y' || brvbargetchar()=='y') )
zsxcn 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )应该改成
if( !(getchar()=='Y'||getchar()=='y') )
MaybeDevil 2008-05-22
  • 打赏
  • 举报
回复
//Test.cpp
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int i,j,sum;
cout < <"Please input two numbers: "; // show
char ch = 0;

do {

cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
//to display integer i,j and i+j
cout << "Do you want to contimue?(y/n)" << endl; // get the result
cin >> ch; // get the entering
cout << endl;
}while(ch == 'y' || ch == 'Y') // check the result ,if equals 'y'and'Y',break ,and continue the next return 0


return 0;
}

pengzhixi 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
改成do....while 试试

C/C++ code
#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
do{
cout < <"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
char ch;
cout < <"ch?(Y/N) :";
cin>>continue;
}while(ch=='y'||ch=='Y')
}

return 0;
}
[/Quote]
犯了低级错误,把continue用做变量名了
pengzhixi 2008-05-22
  • 打赏
  • 举报
回复
改成do while 不行?
limingjiu 2008-05-22
  • 打赏
  • 举报
回复
if( !(getchar()=='Y'&&getchar()=='y') )
//There seems to be something wrong with the line above.
break;
一个字符不能同时为'Y'和'y'
priestmoon 2008-05-22
  • 打赏
  • 举报
回复
按照各位的意见改了一下,但好象还是不行...


#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" " <<sum <<endl;

while(true)
{
cout<<"continue?(Y/N) :"; //press 'Y' if being to continue input.
char ch=getchar();
if( !(ch=='Y' || ch=='y') )
break;
cout<<"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout<<i<<" "<<j<<" "<<sum<<endl;
}

//system("pause");
return 0;
}
waterfield 2008-05-22
  • 打赏
  • 举报
回复
逻辑没有分析清楚。
hyram 2008-05-22
  • 打赏
  • 举报
回复
把你的&&改成||否则就是永假式
lifelovers 2008-05-22
  • 打赏
  • 举报
回复
是输入输出的那个符号,模拟搞错了一些,C++明确要求是“<<”或是“>>”,你的有些有空格空开了
pengzhixi 2008-05-21
  • 打赏
  • 举报
回复
改成do....while 试试

#include <iostream>
using namespace std;

int main()
{
int i,j,sum;
do{
cout < <"Please input two numbers: ";
cin>>i>>j;
sum=i+j;
cout < <i < <" " < <j < <" " < <sum < <endl;
char continue;
cout < <"continue?(Y/N) :";
cin>>continue;
}while(continue=='y'||continue=='Y')
}

return 0;
}

64,637

社区成员

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

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