Input validation issues. Need a second pair of eyes pls.

I'm working on a project for class and i cant get one function to work correctly. Basically I'm trying to validate the user input as an interger between 1 and 3 using an if statement. However in the else section I want to either call the function from the beginning or just reinput the information into the variable, but all i get it the program exiting... I'm sure I've over looked something small but I need another set of eyes.

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
int storyLine::initialPath(void)//user chooses path to start for game
{
	{
	system("cls");
	cout<<"What path will you take?\n"
		"Wormhole 1, 2, or 3?\n"
		"Please make your choice"<<endl;
	}

	{
		cin>>pathChoice;		//input 1 2 or 3. 1 easy start. 2 harder start. 3 is death
	}

	{
	if ((pathChoice > 0) && (pathChoice < 4))
		{
			return pathChoice;	// returns a value to be used by sceneChoice function
							// *see eventChoices.cpp*
		}
		else
		{
			cout << "Invalid choice. Your options are 1, 2, or 3."<<endl;
			cin >> pathChoice;
		}
	}
}

Hi there. How about this :

1
2
3
4
5
6
		else
		{
			cout << "Invalid choice. Your options are 1, 2, or 3."<<endl;
			system("pause");
                        storyLine::initialPath();
		}

hmmm I'm wondering if there is like an IsNumeric type check...

devilirium that did get me to load the function back in but im still unable to get the validation to work.
You might fancy a variation of the following as a way of excluding the input you don't want and to keep asking for the correct input:

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
#include <iostream>
#include <cstdlib>
#include <limits>

using namespace std;


int getInt ()

{
	int x = 0;
	
	while(!(cin >> x) || x<1 || x>3)
	{
		cout << "Invalid selection, Please try again: ";
		cin.clear();
	}
	
	return x;
}


int main ()

{
int choice;

cout << "Please input selecetion 1-3\n";

choice=getInt();
cout << choice;

return 0;
}


Hope this helps,

Dan
Topic archived. No new replies allowed.