Error occurred when I compile the following code

0

When I was trying to run the program Visual Studio threw the error like this: " Error: Unable to open file C:\Users...\test1\Debug\investment.obj. Error code = 0x80070002.

I have tried many ways mentioned online but still not working. So, I was thinking if there was any problem in the code.

What's going on? Please help.

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

struct Investment
{
    string Name;
    string BankAccount;
    string SortCode;
    float Investment;
    float Contribution;
};

string Trim(string str)
{
    str.erase(0, str.find_first_not_of(" \t\r\n"));
    str.erase(str.find_last_not_of(" \t\r\n") + 1);
    return str;
}

void Get_Data(const string& filename, vector<Investment>& data)
{
    ifstream fin(filename);
    if (!fin.is_open())
        return;

    string line;
    if (!getline(fin, line))
        return;

    while (getline(fin, line))
    {
        istringstream sin(line);
        vector<string> fields;
        string field;

        while (getline(sin, field, ','))
        {
            fields.push_back(field);
        }

        Investment inv;
        inv.Name = Trim(fields[0]);
        inv.BankAccount = Trim(fields[1]);
        inv.SortCode = Trim(fields[2]);
        inv.Investment = atof(Trim(fields[3]).c_str());
        inv.Contribution = 0.0f;

        data.push_back(inv);
    }
}

void Save_Data(const string& filename, const vector<Investment>& data)
{
    ofstream fout(filename);
    if (!fout.is_open())
        return;

    fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";

    for (auto& inv : data)
    {
        fout << inv.Name << " "
            << inv.BankAccount << " "
            << inv.SortCode << " "
            << inv.Investment << " "
            << inv.Contribution << "\n";
    }
}

int main()
{
    vector<Investment> Data_investment;
    Get_Data("aaa.csv", Data_investment);

    float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f,[](float sum, const Investment& inv) { return sum + inv.Investment; }
    );

    for (auto& inv : Data_investment)
    {
        float percentage = (inv.Investment * 100.0f) / total;
        inv.Contribution = percentage;
    }

    Save_Data("aaa_new.csv", Data_investment);

    return 0;
}
c++
compiler-errors
asked on Stack Overflow Oct 30, 2019 by Yuning Wu

1 Answer

0

I have successfully compiled your source code into Visual Studio 2017 with very slight modification. Please have a look.

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

struct Investment
{
std::string Name;
    std::string BankAccount;
    std::string SortCode;
    float Invest; // Changed due the variable name goes same as struct name if use Investment
    float Contribution;
};

string Trim(string &str)
{
    str.erase(0, str.find_first_not_of(" \t\r\n"));
    str.erase(str.find_last_not_of(" \t\r\n") + 1);
    return str;
}

void Get_Data(const string& filename, vector<Investment>& data)
{
    ifstream fin(filename);
    if (!fin.is_open())
        return;

    string line;
    if (!getline(fin, line))
        return;

    while (getline(fin, line))
    {
        istringstream sin(line);
        vector<string> fields;
        string field;

        while (getline(sin, field, ','))
        {
            fields.push_back(field);
        }

        Investment inv;
        inv.Name = Trim(fields[0]);
        inv.BankAccount = Trim(fields[1]);
        inv.SortCode = Trim(fields[2]);
        inv.Invest = atof(Trim(fields[3]).c_str());
        inv.Contribution = 0.0f;

        data.push_back(inv);
    }
}

void Save_Data(const string& filename, const vector<Investment>& data)
{
    ofstream fout(filename);
    if (!fout.is_open())
        return;

    fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";

    for (auto& inv : data)
    {
        fout << inv.Name << " "
            << inv.BankAccount << " "
            << inv.SortCode << " "
            << inv.Invest << " "
            << inv.Contribution << "\n";
    }
}

int main()
{
    vector<Investment> Data_investment;
    Get_Data("aaa.csv", Data_investment);

    float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f, [](float sum, const Investment& inv) { return sum + inv.Invest; }
    );

    for (auto& inv : Data_investment)
    {
        float percentage = (inv.Invest * 100.0f) / total;
        inv.Contribution = percentage;
    }

    Save_Data("aaa_new.csv", Data_investment);

    return 0;
}

Below is the modified part

std::string Name;
std::string BankAccount;
std::string SortCode;
float Invest; // Changed due the variable name goes same as struct name if use Investment
answered on Stack Overflow Oct 31, 2019 by gsmaker • edited Nov 2, 2019 by gsmaker

User contributions licensed under CC BY-SA 3.0