[ create a new paste ] login | about

Link: http://codepad.org/50qrNZvd    [ raw code | output | fork ]

C++, pasted on Jul 14:
#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    struct_gene *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list; //file from which all genes will be read
    struct_gene * t_struct_gene; //temp place where struct_gene element will be preapered for inserting in vector
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene; //gene vector where will be writen genes from file gene_list

    //read all genes from file in vector
    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

    //reading all gene values in vector<struct_gene>
    cout <<"All genes in vector"<<endl;;
    for (int i =0; i<vector_gene.size(); i++) {
        cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
    }

    struct_unit *t_struct_unit; //place where truct_unit values will be preapered
    t_struct_unit = new struct_unit;
    srand ( time(NULL) );
    vector<struct_unit> vector_units; //vector for units

    //making vector with units
    for (int i=0; i<default_unit_count; i++) {
        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = &vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    //reading all writen values in vector<struct_unit>
    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<12; j++) {
            cout <<"has gene: "<<vector_units[i].has_genes[j]->name<<" "<<vector_units[i].has_genes[j]->description <<endl;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;

    vector<*struct_gene> vector_child_genes; //vector for genes what child can have

    return 0;
}


Output:
1
2
3
4
5
6
cc1plus: warnings being treated as errors
In function 'int main()':
Line 46: warning: comparison between signed and unsigned integer expressions
Line 72: warning: comparison between signed and unsigned integer expressions
Line 82: error: `*' cannot appear in a constant-expression
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: