簡體   English   中英

cout輸出混亂的消息行,而不是實際用戶的cout語句

[英]cout outputting jibberish line of message instead of actual user's cout statement

這是我的頭文件

#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');
}

當代碼進入“輸入王國”部分時,會提示用戶回答,但在提示之前,它只會輸出亂碼

https://i.imgur.com/MSSHgvz.png

同樣,當輸入居住人數時,在我什至不能輸入有效數字之前,它也會輸出“ -842150451”。

有任何解決問題的猜測嗎?

您的程序將打印垃圾,因為未初始化變量( char[]int )。 實際行為是不確定的。 要解決此問題,您可能應該在類中添加一個構造函數並初始化變量。

進一步閱讀:

另外,當您使用std::cin讓用戶在固定大小的char數組中輸入王國名稱時,他們很容易產生緩沖區溢出。 這通常是不希望的。 請改用std::string

using namespace std; 不鼓勵。 頭文件中尤其如此。

進一步閱讀:

除非您有很好的理由,否則通常不應該使用指針動態分配對象或數組。 如果需要在運行時分配數組,請改用std::vector

進一步閱讀:

您可能應該為類的<<和>>運算符添加重載。 然后,您無需宣布這些成員為公開成員。

進一步閱讀:

您可以這樣做。 我做得很快,所以並不完美。

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;
}

王國

#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

王國

#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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM