fixing segmentation fault

Hello all,

in previous thread i've supplied the last 2 functions of what i built, but it turned out that those 2 functions were correct. however, i don't know what is the source of the segmantation fault in my project. i think it is caused by the way i am handling in main. could you please look and help me fix it? i've been stuck on this for more than a week, and have spent more than 5 hours on it today, but i did not find how to fix the error.

Link to my project:https://onlinegdb.com/B1bUYjkre


it crashes when i'm using any function that uses vector, for instance when i put data to the structure.

thank you very much for your precious help!

Last edited on
Hello cppnoobie,

What I have found so far is:

Starting with option 1 it ends up with Example: Insert somename 111111 so I entered "John Doe 222222". The split function worked fine and split the information into three elements for the vector.

The problem came in the "returnID" function. The vector was successfully passed, but when it tried to do int id = std::stoi(args[1]); that was the segmentation fault because element 1 is "Doe" and element 2 is the number that you want.

Based on the prompt example and the use of getline in the "getArgs" function I thought you wanted a full name, so I entered first and last names then the id number.

The "returnID" function could check for the size of the vector first and maybe an if statement to decide which element to use.

That is as far as I have managed to get for now. After dinner I will have more for you.

Hope that helps,

Andy
Hello cppnoobie,

In the "insertElement" function of the "functionallity" file I found two problems:

First is the same problem as with the "returnID" function. The "stoi" function is trying to convert the wrong element of the vector. I solved the problem in both functions with int id1 = std::stoi(args[args.size() - 1]); based on the last element of the vector being the ID number to put into an "int".

The second problem is std::string name = join(args, 2);. I am not sure what the 2 is for, but the returned value is the ID number. So in the next line you are inserting ID as an "int" and ID as a string under the name "name". Later when I chose option 8 from the main menu it printed the ID twice. I do not think that is what you want.

Now for other thing I saw.

In the two header files you use #ifndef statements to include header files like "iostream". This is not the way to do this or the place. Header files like "iostream", "string" and others need to be in the ".cpp" files. In the ".cpp" files after the normal includes you include your header file, i.e., anything that uses double quotes around the file name. This way the above header files will cover what is in your header files.

After all the "#if..."s I saw the line "#pragma once". This should be at the top of the file. The first line to work properly. You can eliminate the need for "#pragma once" with the use of a header guard like:
1
2
3
4
5
6
#ifndef __FUNCITIONALLITY_H__
#define __FUNCITIONALLITY_H__

// your code here.

#endif  // end !__FUNCITIONALLITY_H__ 

The underscores at the beginning and the end are optional. I think I used something I already had when I created this bit of code.

At the beginning of "main" you have several functions. I would consider putting these in their own ".cpp" file.

I do not mean that you have to change your header files for this program because it does work, but in the future do not put include files like 'iostream" in the header files. Put them in the ".cpp" files where they belong.

Hope that helps,

Andy
thank you very much!

applying the suggestions. will post if i have any additional questions.

thanks again!
Topic archived. No new replies allowed.