c++ - I cannot solve the else part in my if-else ladder?
问题描述
I am trying to write a simple program that iterates through the numbers in Vector to find the number the user asks for. I ask the user to add random numbers to a list (the vector). Then I ask them to enter the number they wish to find. I used a if else ladder to check if the Vector is empty before looping through it. Else I displayed :"The list is empty" I used a Range based for loop to loop through the numbers of the vector, then I tried comparing every number in the loop to the WantedNum. if a number in the loop==wantedNum, I assign it to wantedNum, then display WantedNum back to the user. My code displays the wanted number but it does not stop there like intended.
The case I can't solve is when the user asks for a number that's not in the list.
Well here's my code (I initialized the vector explicitly with bunch of random numbers until I can solve the "Find a number"part, then I'll move on to the "add a number" logic)
I am a beginner so I'd appreciate explanations, especially what have I done wrong.. Of course advice is most welcomed. Thank you in advice.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
char selection {};
vector<int> numbers {10, 20, 30, 46, 78, 100, -2};
int wantedNum {};
cout << "Welcome to my menu: \n";
cout << "Enter F to find a number.\nEnter P to display the numbers in the "
"list.\nEnter Q to quit\n";
do
{
cout << "Enter your choice: ";
cin >> selection;
switch (selection)
{
case 'f':
case 'F':
cout << "Enter the number you wish to find: ";
cin >> wantedNum;
if (numbers.size() != 0)
{
for (auto i : numbers)
{
if (i == wantedNum)
wantedNum = i;
}
cout << "Here's your number: " << wantedNum << endl;
for (auto i : numbers)
{
if (i != wantedNum)
{
cout << "Searching..." << endl;
}
}
cout << "Your number is not in the list.\n";
}
else
{
cout << "List is empty - []\n";
}
break;
case 'p':
case 'P':
cout << "The numbers in the list are: ";
for (auto i : numbers)
{
cout << i << " ";
}
break;
case 'q':
case 'Q': cout << "GoodBye !\n"; break;
default: cout << "I dont recognize this input, please try again.\n";
}
} while (selection != 'Q' && selection != 'q');
}
解决方案
下面的代码块
for (auto i : numbers) {
if (i == wantedNum)
wantedNum = i;
}
cout << "Here's your number: " << wantedNum << endl;
for (auto i : numbers) {
if (i != wantedNum) {
cout << "Searching..." << endl;
}
}
cout << "Your number is not in the list.\n";
向我表明您不清楚逻辑。
你必须:
- 寻找项目。
- 如果找到该项目,则设置一些标志以指示该项目已找到。找到该项目时打破外观。找到项目后继续循环遍历数字没有任何作用。
- 在循环结束后适当地打印一条消息。
这是一个更新的版本
bool found = false;
for (auto i : numbers) {
if (i == wantedNum)
{
found = true;
break;
}
}
if ( found )
{
cout << "Found " << wantedNum << " in the list.\n"
}
else
{
cout << "Did not find " << wantedNum << " in the list.\n"
}
您可以使用它std::find
来稍微减少代码量。
bool found = (std::find(numbers.begin(), numbers.end(), wantedNum) != numbers.end());
if ( found )
{
cout << "Found " << wantedNum << " in the list.\n"
}
else
{
cout << "Did not find " << wantedNum << " in the list.\n"
}
推荐阅读
- apache-nifi - NIFI || 仅为 True 布尔表达式发送邮件
- frama-c - 如何在 ACSL 中编写“是 2 的幂”谓词?
- scala - Neo4j社区版安装问题HostnamePortTest失败
- filenames - 文件名的 Base64 编码字符串
- terraform - 在 Terraform 0.12 中创建地图时保留列表顺序?
- git - git-lfs 是否从 git 历史记录中删除文件?
- node.js - 如何为 SQL Server/Sequelize/NodeJS 中的多列关联生成单个 bcrypt 哈希?
- ios - 用动画改变collectionView Cell的高度
- c# - 如何远程调试 .NET Core 应用程序?
- docker - 你能看到在 Heroku 注册表中推送/存储的图像吗?