if 语句不起作用并被跳到 else 部分
if statement is not working and is skipped to else part
//this is my source file, .cpp
#include <iostream>
#include <string>
#include "kingdom.h"
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, string KingdomName){
cout << " ---------------- " << endl;
cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;
for(int i=0; i<kingdomElement; i++){
if (pKingdom[i].m_name == KingdomName){
cout << " --------------------- " << endl;
cout << KingdomName << ", population " << pKingdom[i].m_population << endl;
cout << " --------------------- " << endl;
}
else{
cout << " --------------------- " << endl;
cout << KingdomName << " is not part of Westeros. " << endl;
cout << " --------------------- " << endl;
}
}
}
}
//this is my main file
#include <iostream>
#include "kingdom.h"
#include <string>
using namespace std;
using namespace westeros;
int main(void){
int count = 0;
Kingdom* pKingdoms = nullptr;
pKingdoms = new Kingdom[count];
display(pKingdoms, count, "Mordor");
display(pKingdoms, count, "The_Vale");
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
//this is my header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
using namespace std;
namespace westeros{
class Kingdom{
public:
char m_name[32];
int m_population;
};
void display(Kingdom pKingdom[], int kingdomElement, string KingdomName);
}
#endif
现在它打印
魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分
The_Vale不是维斯特洛的一部分The_Vale,人口234567The_Vale不是维斯特洛的一部分The_Vale不是维斯特洛的一部分The_Vale不是维斯特洛的一部分
int count = 0;
Kingdom* pKingdoms = nullptr;
pKingdoms = new Kingdom[count];
这将创建一个包含count = 0
元素的数组。任何进一步的访问都将超出限制。
我建议您使用std::vector
:
std::vector<Kingdom> kingdoms(/*(good) count*/);
以及std::string
作为您的char m_name[32];
类型.
当我看到这行时:
cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;
我希望像循环跟随这样的东西,而不是if
.还有一些代码首先用一些名称填充数组。
if
=> for
版后:您应该打印
王国名不是维斯特洛的一部分。
循环后不在里面,只有当KingdomName
没有找到时。
也许您只是忘记将The_Vale
添加到pKingdoms
数组中。
所以,这样的事情会让你明白你做错了什么:
在主文件中,您可以像下面这样改进它,所有其他文件都可以;)
//this is my main file
#include <iostream>
#include "kingdom.h"
#include <string.h>
using namespace std;
using namespace westeros;
int main(void){
int count = 0;
// Kingdom* pKingdoms = nullptr;
// pKingdoms = new Kingdom[count];
Kingdom* pKingdoms = new Kingdom[count]; // Might be a better choice,
// as it reduces code.
display(pKingdoms, count, "Mordor"); // pKingdoms doesn't have Mordor
display(pKingdoms, count, "The_Vale"); // pKingdoms doesn't have The_vale
// Here I add the 'The_Vale to the array
strcpy(pKingdoms[0].m_name, "The_Vale");
pKingdoms[0].m_population = 1000;
display(pKingdoms, count, "The_Vale"); // pKingdoms have The_vale
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
同样在编辑后,也许源代码中的这样的东西.cpp会有所帮助。
//this is my source file, .cpp
#include <iostream>
#include <string>
#include "kingdom.h"
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, string KingdomName){
int flag = -1;
cout << " ---------------- " << endl;
cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;
for(int i=0; i<kingdomElement; i++)
if (pKingdom[i].m_name == KingdomName)
flag = i;
if (flag != -1)
{
cout << " --------------------- " << endl;
cout << KingdomName << ", population " << pKingdom[flag].m_population << endl;
cout << " --------------------- " << endl;
}
else{
cout << " --------------------- " << endl;
cout << KingdomName << " is not part of Westeros. " << endl;
cout << " --------------------- " << endl;
}
}
}
相关文章:
- 部分定义/别名模板模板参数
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- 我的简单if-else语句是如何无法访问的代码
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 程序在执行程序的其余部分之前退出
- 如何删除peer if else分支中的冗长句子
- 在更改for循环的第三部分后,未使用for循环结果
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 部分专业化和嵌套模板
- 为C++03编译器编写部分unique_ptr,该编译器与较新的编译器在公共代码库上运行
- 如何将数组部分初始化为某个默认值?
- c++ std::enable_if .... else?
- 我无法在我的If-Else梯子中解决其他部分
- 如果和 else 部分都在执行
- if 语句不起作用并被跳到 else 部分
- C++ 三元if语句 - 希望没有"else"部分
- 找不到if-else循环的第二部分