getline(cin, VAR) not working in do/while loop if repeated

Hello,

I would appreciate some help on this. below is my program so far. The "isinputgood" function is a placeholder for now. I'm using getline(cin, sInputDates) as an input for a Do/While loop- the first time, it picks up what I type and saves it as sInputDates; however, the second time in the do/while loop will not pick anything up. the field is blank, which then passes a blank string into the 'periodrange' function, which is not good at all.. Any help on why this is happening?



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <string>
#include <iostream>

using namespace std;

// forward declaration of functions
bool isinputgood(string);
string periodrange(string);



int main()
{
	// declare variables
	string sInputDates;
	bool bInputValid_Flag;
	bool bContinue_Flag = 1;
	char chContinue_letter;
	enum Period { NEOGENE = 23, PALEOGENE = 65, CRETACEOUS = 136, JURASSIC = 192, 
		TRIASSIC = 225, PERMIAN = 280, CARBONIFEROUS = 345,  DEVONIAN = 395, 
		SILURIAN = 435, ORDOVICIAN = 500, CAMBRIAN = 570, PRECAMBRIAN = 4500};
	
	do {
		cout << "Please enter a range of dates (in the format XX-YY) in millions of years. " << endl;
		cout << "For example, (64-700): ";
		getline(cin, sInputDates);
		cin.clear();
		cin.ignore(100, '\n'); // used to flush the cin (and therefore the sInputDates string for when the user repeats the loop)

		cout << endl << "sInputDates: " << sInputDates << endl;
		// check validity of input
		bInputValid_Flag = isinputgood(sInputDates);
		
		if (bInputValid_Flag)
		{
			// Determine Periods in range
			periodrange(sInputDates);


			// Ask if the user wants to continue
		}
		

		cout << "Would you like to Repeat? Y or N: ";
		cin >> chContinue_letter;
		if (chContinue_letter == 'N' || chContinue_letter == 'n')
			bContinue_Flag = 0;

	} while (bContinue_Flag); // continue while continue flag is true
	
	cout << "\nProgram exiting...\n";
	return 0;
}


bool isinputgood(string sInputDates)
{
	// This function determines if the input is good or not by making 
	// checks for the format and if the inputs are all numbers. 
	bool ValidFlag;

	ValidFlag = 1;
	

	return ValidFlag;
}


string periodrange(string sInputDates)
{
	// This function 

	// These variables parse the beginning dates and end dates the user
	// typed so they can be used in if statements against the enum 'Period'.
	int nStartingDate = stoi(sInputDates.substr(0,sInputDates.find('-')));
	int nEndingDate = stoi(sInputDates.substr(sInputDates.find('-')+1,sInputDates.length()-1));
	
	cout << nStartingDate << "-" << nEndingDate << endl;

	return "DUMMY";
}
nevermind. I needed a cin.ignore() and cin.clear() after line 45 for the second cin statement..
Topic archived. No new replies allowed.