Program stops working: std::bad_alloc

I'm making a program and compiling it with g++ although I have a problem which I can't solve, It stops then says '.exe has stopped working', I'm trying to parse the text. So each word is added to the array 'LineArray' with the delimiters ' ' ',' '.' although it's not working. Also when the program ends I get an std::bad_alloc error:
what(): std::bad_alloc

Edit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
		std::ifstream FSFileParse(RunFile.c_str());
		std::string Line;
		std::string LineArray[50];
		std::string c_s;

		while (std::getline(FSFileParse, Line)) {
		// Main loop
		// Check each char
		int i = 0;
		for (char& c : Line) {
			while (c != ' ' or c != ',' or c != '.') {
				c_s += c;
			}
			LineArray[i] += c_s;
			i++;
		}
		// Test if it works, remove when it is working
		for (int x = 0; x == 4; x++) {
			std::cout << "Array [" << x << "]: " << LineArray [x] << " ";
		}
Last edited on
I'm not signing up for mediafire just to download your program. also what are you expecting it to do otherwise?
<Deleted>
Last edited on
Dude you posted the same link. Post the code here if you want help here.

Thx
I've shown the code, take a look now
The inner loop never ends.

The only situation in which the or operator will return false is when all operands are false.

c != ' ' is false when c is equal to ' '.

c != ',' is false when c is equal to ','.

There is no way c can be equal to both ' ' and ',' at the same time so c != ' ' or c != ',' will always return true.
How do I solve this?
I've tried this:
1
2
3
4
5
6
7
8
		for (char& c : Line) {
			if (c != ' ' and c != ',' and c != '.') {
				c_s += c;
			} else {
				LineArray[i] += c_s;
			}
			i++;
		}

But it still doesn't work, it just exits straight away (after I've typed in my input)
Last edited on
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
// http://ideone.com/rGl3xF
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> tokenize(const std::string& line, const std::string& delimiters)
{
    std::vector<std::string> tokens;
    std::size_t token_start = 0;
   
    auto const npos = std::string::npos;
    while ((token_start = line.find_first_not_of(delimiters, token_start)) != npos)
    {
        auto token_end = line.find_first_of(delimiters, token_start);
        auto len = token_end == npos ? line.size() - token_start : token_end - token_start;

        tokens.push_back(line.substr(token_start, len));

        token_start = token_end;
    }

    return tokens;
}

int main()
{
    auto tokens = tokenize("one.two three,4.5 six seven,eight.9.ten", " ,.");

    for (auto& token : tokens)
        std::cout << token << '\n';
}
Can I do this with a normal array - not a vector?
sure but why wouldn't you use a vector?
Topic archived. No new replies allowed.