for循环不包括需要包含数组的一部分



这是我的标题文件

#ifndef KINGDOM_H_
#define KINGDOM_H_
// TODO: sict namespace
using namespace std;
namespace sict {
    // TODO: define the structure Kingdom in the sict namespace
    class Kingdom {
    public:
        char m_name[32];
        int m_population;
    };
    // TODO: declare the function display(...),
    void display(const Kingdom&);
    void display(Kingdom pKingdom[], int kingdomNum);
    //         also in the sict namespace
}
#endif

这两个是我的CPP文件

#include <iostream>
#include <string>
#include "Kingdom.h"
using namespace std;
// TODO: the sict namespace
namespace sict {

    // TODO:definition for display(...)
    void display(const Kingdom& pKingdom) {
        cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;
    }
    void display(Kingdom pKingdom[], int kingdomNum) {
        int total = 0;
        cout << "------------------------------" << endl;
        cout << "Kingdoms are" << endl;
        for (int i = 0; i < kingdomNum; i++) {
            cout << i + 1 << ". " << pKingdom[i].m_name << ", population" << pKingdom[i].m_population << endl;
            total += pKingdom[i].m_population;
        }
        cout << "------------------------------" << endl;
        cout << "Total population of SICT: " << total << endl;
        cout << "------------------------------" << endl;
    }

}


#include <iostream>
#include "Kingdom.h"
using namespace std;
using namespace sict;
void read(Kingdom&);
int main() {
    int count = 0; // the number of kingdoms in the array
    // TODO: declare the pKingdom pointer here (don't forget to initialize it)
    Kingdom* pKingdom = nullptr;

    cout << "==========n"
        << "Input datan"
        << "==========n"
        << "Enter the number of Kingdoms: ";
    cin >> count;
    cin.ignore();
    if (count < 1) return 1;
    // TODO: allocate dynamic memory here for the pKingdom pointer
    pKingdom = new Kingdom[count];
    for (int i = 0; i < count; ++i) {
        cout << "Kingdom #" << i + 1 << ": " << endl;
        // TODO: add code to accept user input for Kingdom i
        cout << "Enter the name of the Kingdom: ";
        cin >> pKingdom[i].m_name;
        cout << "Enter the number of people living in " << pKingdom[i].m_name << ": ";
        cin >> pKingdom[i].m_population;
    }
    cout << "==========" << endl << endl;
    // testing that "display(...)" works
    cout << "------------------------------" << endl
        << "The 1st kingdom entered is" << endl
        << "------------------------------" << endl;
    display(pKingdom[0]);
    cout << "------------------------------" << endl << endl;

    // add the new Kingdom
    cout << "==========n"
        << "Input datan"
        << "==========n"
        << "Kingdom #" << count + 1 << ": " << endl;
    // TODO: accept input for the new element in the array
    for (int i = 0; i < count; i++) {
        count = 1;
        cout << "Enter the name of the Kingdom: ";
        cin >> pKingdom[i].m_name;
        cout << "Enter the number of people living in " << pKingdom[i].m_name << ": ";
        cin >> pKingdom[i].m_population;
    }
    count++;
    cout << "==========n" << endl;
    // testing that the overload of "display(...)" works
    display(pKingdom, count);
    cout << endl;
    // TODO: deallocate the dynamic memory here
    delete[] pKingdom;
    system("pause");
    return 0;
}
// read accepts data for a Kingdom from standard input
//
void read(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');
}

,输出应询问用户的王国数,并取决于王国的数量,

它将询问相同的两个问题的数量,这些问题要求王国的名字和居住在王国的人数,直到循环结束。

现在,一旦控制台进入王国#3,用户键入所有内容,在最后一个过程中,它将陈述属于SICT名称空间的所有王国的名称和人口,

,该代码不会将王国#3的信息提供,而没有王国#3的信息将计算总人口。

所以,它应该像这样:

SICT王国

  1. the_vale,人口234567
  2. The_Reach,人口567890

    3。the_riverlands,人口123456

SICT总人口:925913

,但它只打印


SICT王国

  1. the_vale,人口234567

    2。the_reach,人口567890

SICT总人口:603,428

谁能告诉我为什么以及如何解决问题?

您的display功能很好,但是您的输入是一团糟...

您应该像其他注释一样使用std :: vector或列表,但是如果您必须使用原始数组,则需要记住在数组中要更改数据的位置,并分配新数组和当您需要更大的数据时,请复制数据。

如果您的第二轮输入只能读取一个元素,则不应该是循环。

您还应该使用read函数而不是复制输入代码,同样,您应该在"显示所有"函数中使用"显示(...)"函数而不是复制代码。

最新更新