2017-09-20 249 views
0

的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」之前,我甚至可以輸入一個有效的數字。

任何猜測來解決問題?

+0

因子的所有指針。然後使用'std :: string'而不是char緩衝區。不要在標題中使用名稱空間標準。最後,不要在變量初始化之前打印出變量。 – moooeeeep

+0

你是指所有指針的意思是什麼? – lucas

+0

請勿在您的代碼中使用它們。 – moooeeeep

回答

2

由於變量(char[]int)未初始化,因此您的程序將打印垃圾。實際的行爲是未定義的。爲了解決這個問題,你應該爲你的類添加一個構造函數並初始化變量。

對於進一步閱讀:

此外,當您使用std::cin讓用戶輸入一個王國的名稱爲固定大小的字符數組,他們可以很容易產生緩衝區溢出。這通常是不可取的。請改用std::string

不鼓勵使用using namespace std;。特別是在頭文件中。

對於進一步閱讀:

除非你有你一般不應使用指針來動態分配對象或數組可以很好的理由。如果您需要在運行時分配數組,請改爲使用std::vector

對於進一步閱讀:

你也許應該補充重載的< <和>>操作符類。您當時不需要公開聲明這些成員。

對於進一步閱讀:

0

你可以不喜歡這樣。我很快就做到了,所以它不完美。

的main.cpp

#include "Kingdom.h" 

int main() { 
    int count = 0; // the number of kingdoms in the array 
    sict::Kingdom* pKingdom = nullptr; 


    std::cout << "==========\n" 
     << "Input data\n" 
     << "==========\n" 
     << "Enter the number of Kingdoms: "; 
    std::cin >> count; 
    std::cin.ignore(); 

    if (count < 1) return 1; 

    //without read() 
    //pKingdom = new sict::Kingdom[count]; 
    //for (int i = 0; i < count; ++i) { 
    // std::cout << "Kingdom #" << i + 1 << ": \n"; 
    // //cin >> i; 
    // std::cout << "Enter the name of the Kingdom: "; 
    // pKingdom[i].setName(); 
    // std::cout << "Enter the number people living in " << pKingdom[i].m_name << ": "; 
    // std::cin >> pKingdom[i].m_population; 

    //} 
    //std::cout << "==========\n\n"; 

    pKingdom = new sict::Kingdom[count](); 

    for (int i = 1; i <= count; ++i) 
    { 
     pKingdom[i-1].read(); 
    } 


    // testing that "display(...)" works 
    std::cout << "------------------------------\n" 
     << "The 1st kingdom entered is:\n" 
     << "------------------------------\n"; 
    pKingdom[0].display(); 
    std::cout << "------------------------------\n\n"; 


    delete[]pKingdom; 
    pKingdom = nullptr; 
    return 0; 
} 

Kingdom.h

#ifndef KINGDOM_H_ 
#define KINGDOM_H_ 

#include <iostream> 
#include <string> 

namespace sict { 

    class Kingdom { 
    private: 
     std::string m_name; 
     int m_population; 
    public: 
     Kingdom(); 

     void display(); 
     void read(); 

     const std::string& getName(); 
     void setName(std::string name); 
     const int& getPopulation(); 
     void setPopulation(int population); 
    }; 
} 
#endif 

Kingdom.cpp

#include "Kingdom.h" 

namespace sict { 

    Kingdom::Kingdom() 
     :m_name(""), 
     m_population(0) 
    {} 

    void Kingdom::display() { 
     std::cout << "Name: " << m_name << ", population: " << m_population << "\n"; 
    } 

    void Kingdom::read() { 
     std::cout << "\n\nEnter the name of the Kingdom: "; 
     std::getline(std::cin, m_name); 

     std::cout << "Enter the number of people living in " << m_name << ": "; 
     std::cin >> m_population; 
     std::cin.ignore(); 
    } 

    const std::string& Kingdom::getName() 
    { 
     return m_name; 
    } 

    void Kingdom::setName(std::string name) 
    { 
     m_name = name; 
    } 

    const int& Kingdom::getPopulation() 
    { 
     return m_population; 
    } 

    void Kingdom::setPopulation(int population) 
    { 
     m_population = population; 
    } 

}