Input error crashes process

I'm working on a console hack of mine everything seem good but when I press enter twice it crashes the process here is some of my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <Windows.h>
#include<iostream>
#include <string>
#include <cctype>
#include <sstream>
#include <vector>
#include <iterator>
using namespace std;

std::string Input() {
	std::string inputstring;
	getline(std::cin, inputstring);
	return inputstring;
}

vector<string> split(string s) {
	vector<string> elems;
	stringstream ss(s);
	istream_iterator<string> begin(ss);
	istream_iterator<string> end;
	vector<string> vstrings(begin, end);
	return vstrings;
}
//inside my main thread function just didn't want to post all my code
1
2
3
4
5
6
7
vector<string> option = split(Input());
			if (std::cin.fail())
			{
				std::cin.clear();
				std::cin.ignore(32767, '\n');
				continue;
			}
next the only parts of the string vector I accessed were
option.at(0) and if option.at(0) meets certain requirements and the total vector size is equal to 2 I'd access option.at(1) also I tried to check the compatibility of each character with this function.

1
2
3
4
5
6
7
8
9
bool CheckStringCompatibility(string str,int(__cdecl *strfunc)(int))
{
for(int i = 0;i < str.size();i++)
{
	if (!strfunc(str.at(i)))
		return 0;
}
return 1;
}
Last edited on
What are you exactly trying to do in this program? Also, could you please use the code tags to make the code more readable?
Last edited on
I'm using the code in a console to separate a string from input like separating health me 100 into the separate string "health","me","100" but when I don't enter anything and press enter twice it crashes.
To try and diagnose what's going on I've stripped out the intervening functions in your program and looked at the code step by step - you getline() cin into string inputString, sstringstream inputString into class object ss and essentially push_back vector<string> vstring with the strings via istream iterator adapters as they are read off ss. I've also asked the program to output vstring.size() and print vector elements with an identifier (here *) b/w them to double-check:
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
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
    string inputString;
    cout<<"Enter string: \n";
    getline(cin, inputString);
    if (std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(32767, '\n');//replace magic number with numeric_limits<streamsize> (#include <limits>)
        //continue; cannot have continue w/o loop
    }
    stringstream ss(inputString);
    istream_iterator<string> begin(ss);
	istream_iterator<string> end;

    vector<string> vstring(begin, end);
    cout<<"Vector size: "<<vstring.size()<<"\n";
    for (auto& elem : vstring)
    {
        cout<<elem <<"*";
    }
}

Now when I press enter twice in my simple program above the output is as follows:
1
2
3
4
5
6
Enter string:

Vector size: 0

Process returned 0 (0x0)   execution time : 1.610 s
Press any key to continue.

Since there was no other code in my program and the way it's set up my main() returned but I suspect returning a zero sized vector is messing up some other part of your program that you've not posted. The way to preclude this is to have the input validation as part of the Input() function in the first instance and not within main()
Topic archived. No new replies allowed.