Returning Employee and Salary when Given a Location Substring

ε祈祈猫儿з 提交于 2020-02-12 05:47:25

问题


I am given two files one with the name of person and the location that they are from (Evan Lloyd|Brownsville) and one with the name and salary (Evan Lloyd|58697) (the line number that you find the employee on in the first file is not necessarily the line number that find the employee on in the second). The user inputs a location (whole or part). For example if they input "ville" or "Ville" it should include all of the employees in Brownsville, Clarksville, Greenville, etc. I am supposed to join the the name and salary and return them if they are in the city searched for i.e. "ville" or "Ville."

I have no errors or warnings when compiling, but I get a segmentation fault for all input.

#include <fstream>
#include <iostream>
//#include <vector>
#include <string>
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str;
    cout << "Enter the location: ";
    cin >> str;
    ifstream addresses;
    addresses.open("personnel_addresses.txt");
    multimap<string, string> name_address;
    ifstream salaries;
    salaries.open("personnel_salaries.txt");
    multimap<string, string> name_salary;
    while(!addresses.eof() && !salaries.eof()) {
        string tmpstr;
        getline(addresses, tmpstr);
        int pos = tmpstr.find("|");
        string name2address = tmpstr.substr(0, pos - 1);
        string address = tmpstr.substr(pos + 1);
        name_address.insert({address, name2address});
        getline(salaries, tmpstr);
        pos = tmpstr.find("|");
        string name2employee = tmpstr.substr(0, pos - 1);
        string salary = tmpstr.substr(pos + 1);
        name_salary.insert({name2employee, salary});
    }
    // do{
    vector<string> employees;
    for(auto n = name_address.find(str); n != name_address.end(); n++) {
        employees.emplace_back(n->second);
    }
    for(int i = 0; i < sizeof(employees); i++) {
        string x = employees[i];
        //          if (name_salary.find(employees[i]))
        cout << employees[i] << ":" << name_salary.find(x)->second << "\n";
    }
    //}while(name_address.end());
    addresses.close();
    salaries.close();
    return 0;
}

Someone recommended that I alter the code by populating a set full of the cities in the while loop and iterating over the set immediately after declaring vectoremployees instead the code that directly below it by the following code

for(string const& search : cities)
{
if(find(search.begin(), search.end(), str) != std::string::npos)
{
string y = search;
employees.emplace_back(y);

, but there is something wrong with syntax where I am trying to iterate over the set.


回答1:


A segmentation fault generally occurs when you are trying to access memory in some capacity, but have no permission to that location in memory. I notice that in one of your final for-loops, you say for(int i = 0; i < sizeof(employees); i++), but I believe that the function sizeof(object) reports the number of bytes in memory its parameter is consuming. It seems like that is not what you intended, and the number of bytes will most likely be much larger than the number of employees you are tracking. Thus, the body of your for-loop will try to access memory it is not allowed to and then cue a segmentation fault.

Another quick couple notes:

  1. Are you guaranteed that both of your input files will contain information about the same number of employees? If not, you will quit reading in from both files as soon as you made it to the end of one of them because of your looping condition.

  2. In most cases, I believe ifstream::eof() is pretty dangerous because it only indicates whether you have attempted to read past the end of a file. This usually means that you will perform more operations than you intended to. See here if you want more information from more knowledgeable people than I.



来源:https://stackoverflow.com/questions/59922474/returning-employee-and-salary-when-given-a-location-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!