的jibberish线这是我的头文件COUT输出消息,而不是实际用户的COUT声明
#ifndef KINGDOM_H_
#define KINGDOM_H_
#include <string>
using namespace std;
namespace sict{
class Kingdom {
public:
char m_name[32];
int m_population;
};
void display(Kingdom& pKingdom);
}
#endif
,这些都是我的cpp文件
#include <iostream>
#include <string>
#include "kingdom.h"
using namespace std;
namespace sict {
void display(Kingdom& pKingdom) {
cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;
}
}
而且这是我最后的cpp文件
#include <iostream>
#include "Kingdom.h"
using namespace std;
using namespace sict;
void read(sict::Kingdom&);
int main() {
int count = 0; // the number of kingdoms in the array
Kingdom* pKingdom = nullptr;
cout << "==========\n"
<< "Input data\n"
<< "==========\n"
<< "Enter the number of Kingdoms: ";
cin >> count;
cin.ignore();
if (count < 1) return 1;
pKingdom = new Kingdom[count];
for (int i = 0; i < count; ++i) {
cout << "Kingdom #" << i + 1 << ": " << endl;
cin >> i;
cout << "Enter the name of the Kingdom: " << pKingdom[i].m_name;
cin >> pKingdom[i].m_name;
cout << "Enter the number people living in " << pKingdom[i].m_population << ": ";
cin >> pKingdom[i].m_population;
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The 1st kingdom entered is" << endl
<< "------------------------------" << endl;
sict::display(pKingdom[0]);
cout << "------------------------------" << endl << endl;
delete[]pKingdom;
pKingdom = nullptr;
return 0;
}
// read accepts data for a Kingdom from standard input
void read(sict::Kingdom& kingdom) {
cout << "Enter the name of the Kingdom: ";
cin.get(kingdom.m_name, 32, '\n');
cin.ignore(2000, '\n');
cout << "Enter the number of people living in " << kingdom.m_name << ": ";
cin >> kingdom.m_population;
cin.ignore(2000, '\n');
}
当代码到达部件输入Kingdom名称时,它会提示用户回答,但在提示之前,它只是输出jibberish这样
https://i.imgur.com/MSSHgvz.png
而且,当它到达进入人们生活的数量,这也将输出“-842150451”之前,我甚至可以输入一个有效的数字。
任何猜测来解决问题?
因子的所有指针。然后使用'std :: string'而不是char缓冲区。不要在标题中使用名称空间标准。最后,不要在变量初始化之前打印出变量。 – moooeeeep
你是指所有指针的意思是什么? – lucas
请勿在您的代码中使用它们。 – moooeeeep