我有一个我创建的菜单。我有一些用户也可以去看某些东西的选项,所以为了让他们继续使用程序,他们必须输入end才能继续下一段代码。但我的问题是,当他们输入结束时,它会把它变成无限循环。这里的任何人都可以代码。
int var = 1;
int main=1;
char dd[] = "done";
do
{
cout << "\n\n\nEnter the number of one of the following and I will explain!\n";
cout << "1.integer 2.boolian 3.floats 4.doubles 5.character";
cout << "\n\n[when you are done type 'done' to continue]\n\n";
cin >> option || dd;
if (option == 1)
{ blah blah blah blah
}
else if (option ==2)
{
blah blah blah
}
else if (dd=="done") //when user types 'done' it goes into infinite loop.
{ break;}
while (var==1);
答案 0 :(得分:1)
cin >> option || dd;
此代码不会提取option
和dd
的输入。运算符||
不像“管道”那样被使用。相反,您可以再次使用>>
来链接对提取器的调用:
cin >> option >> dd;
此外,您应该将std::string
用于dd
,因为比较C字符串只会比较其地址,而不是实际内容。
答案 1 :(得分:0)
您需要检查失败位 - 修复行cin >> option || dd;
后查看http://www.cplusplus.com/reference/istream/istream/
答案 2 :(得分:0)
您需要将原始C字符串(char*
)与strcmp()
进行比较。要比较dd
==“完成”是否使用strcmp(dd,“done”)== 0。
编辑:
要使用strcmp
,您应#include<cstring>
EDIT2:正如其他答案和评论中所指出的,看到整个代码,最好更改为std::string
。但是,如果您真的想继续使用char*
,请使用strcmp
进行比较,如上所述。