robinzero200
robinzero200

Reputation: 53

C++ class function isn't performed

It's a very simple bug: I have a small project where I'm supposed to generate a bunch of folders and files. It serves as dummy data for the actual storage engine I'm supposed to make. The data is about "Humans" who, along other properties have a confidence level and height. Every time I make a new file, I update the selected Human so their height gets bigger and confidence level changes. The program compiles but when I check my files I notice the Humans never change these properties and thus don't get updated. I don't understand anymore why, as the code seems so small and simple I must be overlooking something but I have been trying to find this tiny bug for weeks now.

Human.cpp :

#include "human.h"

Human::Human(const std::string& _fname, const std::string& _lname, int _gender, const std::string& _position) {
    m_firstname = _fname;
    m_lastname = _lname;
    m_height = (rand() % 271) + 0.1;  // a person's height between 30 and 300 cm
    m_height = m_height + 30; // writing this in one line gives a warning for arthimetic overflow
    m_gender = _gender;
    m_position = _position;
    m_confidence = (rand() % 100) + 0.01 * (rand() % 101);
}

void Human::UpdateValues() {
    m_height = m_height + 0.1* (rand()% 11); // new packages each hour: grow 1 cm max
    if (m_height >= 300) { // don't grow over 3 meter
        m_height = 300;
    }

    // T or F: random number is even or uneven
    int is_even = round(m_height);
    if (( is_even % 2) == 0) {
        m_confidence = m_confidence + (rand() % 20) + 0.01 *(rand() % 100); // fluctuates by 20% max 
        if (m_confidence > 100) {
            m_confidence = 100;
        }
    }
    else {
        m_confidence = m_confidence - (rand() % 20) + 0.01 * (rand() % 100); // fluctuates by 20% max 
        if (m_confidence < 0) {
            m_confidence = 0;
        }
    }
}

main.cpp:

#pragma warning(push,0) //header files from third party cause errors in console, ignore these since it is not our code
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#pragma warning(pop)
#include "human.h"

namespace fs = std::filesystem;

// issues left to resolve:
// - humans don't get updated?

fs::path createSubFolder(const fs::path& _parent, const std::string &_subfolder) {
    fs::path sub_path = _parent / _subfolder;
    fs::create_directories(sub_path);
    return sub_path;
}
std::string createString(int number) { // for the generation of file names: single digit days/months/hours need to be converted to double digit string
    if (number >= 10) {
        return std::to_string(number);
    }
    // else
    return "0" + std::to_string(number);
}

int main() {
    cv::Mat blank(500, 700, CV_8UC3, cv::Scalar(230, 230, 230)); // blank image to write the imagestring on later for each package file
    fs::path root = "C:\\data_system_programming"; // path of the main directory where we will generate all data

    // generate some Humans for the packages
    std::vector<std::string> names = { "Tom", "Paul", "Anna", "Cindy", "Mina", "Skye", "Justin" }; //will uses these vectors to generate names
    std::vector<std::string> lastnames = { "Jansen", "Johnson", "Smith", "Hathaway", "Merckx", "Granger", "Versendaal" };

    std::vector<std::shared_ptr<Human>> Humans; //size 6

    for (int i = 0; i <= 6; i++) {
        Humans.push_back(std::shared_ptr<Human>(new Human(names[i], lastnames[i], 1, "Unemployed")) );
    }
    for (int i = 2; i <= 4; i++) {
        Humans.push_back(std::shared_ptr<Human>(new Human(names[i], lastnames[i], 0, "Employed")));
    }
    for (int i = 5; i <= 6; i++) {
        Humans.push_back(std::shared_ptr<Human>(new Human(names[i], lastnames[i], 2, "Student")));
    }


    for (int year = 2018; year <= 2020; year++) {
        bool is_leapyear = 0; // need this bool to know if februari has 28 or 29 days
        if (year % 4 == 0) { // a year is a leapyear when it is divisible by 4 but not by 100
            is_leapyear = (year % 100 != 0);
        }
        if (year % 400 == 0) { // unless it is divisible by 400
            is_leapyear = 1;
        }
        std::string string_year = std::to_string(year);
        fs::path year_path = createSubFolder(root, string_year); // create folder for the year
        for (int month = 1; month <= 12; month++) {
            int month_length = 31;
            switch (month) {
            case 2:
                if (is_leapyear) {
                    month_length = 29;
                }
                else {
                    month_length = 28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                month_length = 30;
                break;
            }
            std::string string_month = createString(month);
            fs::path month_path = createSubFolder(year_path, string_month); // create folder for the month in year folder
            for (int day = 1; day <= month_length; day++) {
                std::string string_day = createString(day);
                fs::path day_path = createSubFolder(month_path, string_day); // create folder for the day in month folder
                for (int hour = 0; hour <= 23; hour++) {


                    int amount_pckgs = rand() % 4; // gives a random number between 0 and 20 = number of packages to create
                    for (int counter = 1; counter <= amount_pckgs; counter++) {
                        // create a package with txt and png:

                        std::string date_string = std::to_string(year) + createString(month) + createString(day);
                        std::string time_string = createString(hour);

                        // need randomised minutes, seconds (7 digits total), miliseconds
                        for (int digit = 0; digit < 3; digit++) {
                            time_string = time_string + std::to_string(rand() % 7); // append to timestring a number from 0 to 6
                            time_string = time_string + std::to_string(rand() % 10); // append to timestring a number from 0 to 9
                        }
                        time_string = time_string + std::to_string(rand() % 10); // append to timestring a number from 0 to 9

                        std::string filenamestring = date_string + "-" + time_string + "-";
                        // select a human
                        int select_human = (rand() % 7);
                        Human H = *(Humans[select_human]);

                        filenamestring = filenamestring + H.m_firstname + "-" + H.m_lastname + "-" + std::to_string((int)round(H.m_height));
                        // we wil use this to generate package, .txt and .jpg filename
                        fs::path package_path = day_path / ("p-" + filenamestring);
                        fs::create_directories(package_path);

                        
                        // filling in the text file:
                        std::ofstream text_file;
                        text_file.open(package_path / ("t-" + filenamestring + ".txt")); //this creates said text file
                        text_file << "001" << date_string << std::endl;
                        text_file << "002" << time_string << std::endl;
                        text_file << "003" << H.m_firstname << std::endl;
                        text_file << "004" << H.m_lastname << std::endl;
                        text_file << "005" << std::to_string((int)round(H.m_height)) << std::endl;
                        text_file << "006" << std::to_string(H.m_gender) << std::endl; // 0 women, 1 men, 2+ others
                        text_file << "007" << H.m_position << std::endl;
                        std::stringstream ss;
                        ss << std::fixed << std::setprecision(2) << H.m_confidence; // keep only 2 decimal points
                        text_file << "008" << ss.str() << std::endl;

                        H.UpdateValues(); // update Human so they grow and change confidence

                        text_file.close();
                        // creating the image file
                        std::string text = date_string + "-" + time_string + "-" + H.m_firstname + "-" + H.m_lastname;
                        cv::Mat image = blank.clone();
                        cv::putText(image, text, cv::Point(50, 250), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0), 1);
                        std::string path_string = (package_path / ("i-" + filenamestring + ".jpg")).u8string();
                        cv::imwrite(path_string, image);
                    }
                }
            }
        }
    }
    return 0;
}

Upvotes: 2

Views: 80

Answers (1)

cigien
cigien

Reputation: 60308

On this line:

Human H = *(Humans[select_human]);

you are making a copy of the Human in Humans. So when you do:

H.UpdateValues(); // update Human so they grow and change confidence

you are not actually modifying the contents of Humans, which seems to be the intent of the code.

Instead, you can make a reference to the Human object you want to update:

Human & H = *(Humans[select_human]);
   // ^ reference

and now modifying H will modify the contents of Humans.

Upvotes: 4

Related Questions