首页 > 解决方案 > 使用对象和动态数组(使用指针创建)或向量时 for 循环不起作用

问题描述

主体中的第二个循环仅在一步后停止。我不知道为什么!我的代码{

#include <iostream>

#include <string>

#include <vector> // using dynamic array

using namespace std;

int i; // for loops

class person{
    private:
        string name; // the name of the person
        vector < string > ne; // the names that are in the list
        int mn_f; // the money at the first before giving to other people
        int n; // the number of people in the list
        int mn_l = 0; // the money at the end of the party
    public:
        void set_name(string name){ // setting the name
            this -> name = name;
        }
        string get_name(){ // returning the name
            return name;
        }
        void set_mn_f(int mn_f){ // getting the first money of the person
            this -> mn_f = mn_f;
        }
        void set_n(int n){ // getting the n
            this -> n = n;
        }
        void set_list(){ // getting the people that are in the list
            string a; // a member in the list
            for( i = 0; i < n; i++){
                cin >> a;
                ne.push_back(a);
            }
            //cout << "started\n";  
        }
        void get_list(){
            for( i = 0; i < n; i++){
                cout << i + 1 << ' ' << ne[i] << endl;
            }
            //cout << "finished\n";
        }
        void mod(){
            mn_l += (mn_f % n); // the mod of money to the n is for the person
        }
        void set_mn_l(int n){ // calculating the last money of the person
            mn_l += n;
        }
        int get_mn_l(){
            return mn_l; // returning the last money of the person
        }
        ~person(){
            ne.clear(); // deleting ne
        }
};

int main(){
    int n; // the number of attendees
    string ne; // name of the people
    cin >> n;
    person Person; // the person and its information
    vector < person > a; // the list of the people in the party
    int mn_f; // the money at the first before giving to other people
    int nu; // the number of people in the list
    for( i = 1; i <= n; i++){
        cin >> ne; // entering the names for the first time
    }
    for( i = 0; i < n; i++){ // entering the names for the second time and completing the information
        cin >> ne >> mn_f >> nu;        
        Person.set_name(ne); // name
        Person.set_mn_f(mn_f); // money at first
        Person.set_n(nu); // the number of the people in the list of Person
        Person.set_list(); // entering the people in the list
        a.push_back(Person); // putting the Person in the list
        cout << "loop\n";
    }
    cout << a.size() << endl;
    for( i = 0; i < n; i++){
        Person = a[i];
        cout << Person.get_name() << endl;
        Person.get_list();
        cout << "loop2\n";
    }
    cout << a.size() << endl;
    a.clear(); // deleting a
    return 0;
}

标签: c++

解决方案


推荐阅读