c – 如果语句不起作用,则跳过其他部分
发布时间:2020-12-16 07:20:29 所属栏目:百科 来源:网络整理
导读://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 " Kin
//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,"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[],string KingdomName); } #endif 现在它打印 魔多不属于维斯特洛 The_Vale不是维斯特洛的一部分 解决方法
也许您只是忘了将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,"Mordor"); // pKingdoms doesn't have Mordor display(pKingdoms,"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,"The_Vale"); // pKingdoms have The_vale delete[]pKingdoms; pKingdoms = nullptr; return 0; } 在编辑之后,也许source.cpp中的这样的东西会有所帮助. //this is my source file,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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |