Segmentation fault error

hello all!

so glad to find this nice place and become part of it!

i've a trouble with implementing a function that takes a string input from the user, and returns a string vector containing each word. for instance if i write "hello asd 132134", i want the first index to be "hello", second "asd" and third the number.

when i tried to implement that, i've stumbled upon a segmentation error, and i'm sitting for hours not knowing how to fix it. i implemented it using 2 functions(will be using it multiple times in my code, so no other option).

 



thank you in advance!
Last edited on
The code seems to work fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

std::vector<std::string> split(const std::string&);

std::vector<std::string> getArgs()
{
	std::string line;
	std::getline(std::cin, line);
	auto results = split(line);
	return results;
}

std::vector<std::string> split(const std::string& s)
{
	std::vector<std::string> setArgs;
	std::string token;
	std::istringstream tokenStream(s);
	while (std::getline(tokenStream, token, ' '))
	{
		setArgs.push_back(token);
	}
	return setArgs;
} 

int main()
{
	std::vector<std::string> args = getArgs();
	for (auto& arg : args)
	{
		std::cout << arg << '\n';
	}
}

Are you sure it is the code that you have posted that is causing the segfault?
sec, misread a line.
Last edited on
the code worked without this error before i introduced those 2 functions. i'll supply a link to online gdb with my full code (both of the mentioned functions are in functionality.cpp): https://onlinegdb.com/SkL-SosoX (the error begins whenever i use args, for instance when i want to insert input to the tree by pressing 1)
it may be how you are using the function?
This works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  //std::vector<std::string> split(const std::string& s)
{
   string s = "one two three four";
   std::vector<std::string> setArgs;
   std::string token;
   std::istringstream tokenStream(s);
   while (std::getline(tokenStream, token, ' '))
   {
      setArgs.push_back(token);
   }
   for(int i = 0; i < setArgs.size(); i++)
	   cout << setArgs[i] << endl;
  // return setArgs;
} 
@jonin: could you please look at the program link i gave( https://onlinegdb.com/SkL-SosoX) ? i am stuck on it for days and long hours today and i don't know what i'm missing :( would really appreciate help me with
Ah, the classical problem of mixing >> and getline. The problem is that >> leaves the newline character in the stream so if you use getline right after it will find a newline character right away and not wait for more input so it just gives you an empty string. The simplest solution to this problem in my opinion is to use std::ws before each time you use std::getline.

 
std::getline(std::cin >> std::ws, line);

 
while (std::getline(tokenStream >> std::ws, token, ' '))


You also need to fix your error checking code. The check functions just print error messages but the code continues as normal and doesn't prevent the code that expects the input to be correct from running. You need to somehow abort the action if you detect an error. If you are familiar with "exceptions" that might be something you want to consider.
Last edited on
I am blocked from external links and files at work.
give an example input that causes segfault
by the way http://www.cplusplus.com/forum/general/138037/


> use std::ws before each time you use std::getline.
sadly, you can't input an empty line then
Last edited on
That is true. What I showed will not work if there is a need to accept empty lines. It will also not work if you care about leading whitespace characters at the beginning of the line.
Last edited on
thank you very much for your help!
Topic archived. No new replies allowed.