制作一个程序,允许用户输入输入,以便他们可以获得简单的数学科目的帮助。我以为我已经做到了,所以如果他们进入了一个不在那里的话题,那么下面的else语句就不会看到这个话题。任何想法为什么当我运行它时,它仍然包括else语句当输入主题是正确的? http://prntscr.com/ey3qyhC++忽略其他语句
search = "triangle";
pos = sentence.find(search);
if (pos != string::npos)
// source http://www.mathopenref.com/triangle.html
cout << "A closed figure consisting of three line segments linked end-to-end. A 3 - sided polygon." << endl;
search = "square";
pos = sentence.find(search);
if (pos != string::npos)
// source http://www.mathopenref.com/square.html
cout << "A 4-sided regular polygon with all sides equal and all internal angles 90°" << endl;
else
cout << "Sorry, it seems I have no information on this topic." << endl;
case 2:
break;
default:
cout << "Invalid input" << endl;
}
将'search =“square”;'以及之后的所有内容放到'case 2:'中放入else语句中。然后,如果找不到三角形,它只会查找一个正方形。 –
有比正方形或三角形更多的选项,我只是不想包含太多的代码。我会在每个搜索如三角形和正方形之后放入else语句,并且如上所述进行操作吗? –
如果您可以更改它,以便if中的所有代码位于if语句中(例如'search =“square”; pos = sentence.find(search); if(pos!= string :: npos)'into 'if(sentence.find(“square”)!= string :: npos)'),那么你可以将第一个if语句之后的所有if语句改成else if语句。如果不是那么每个如果将不得不在其他内部。我会写一个答案 –