Do while loop not repeating

I am having issues getting my code to repeat once the user has entered 'y' or 'Y'. Right now, I am only able to get the program to output the rainfall amount for one month (see searchData() function). After displaying the rain data for the month entered, the program should ask the user if they want to continue or exit. However, when I enter 'y' to continue, the prompt does not allow me to enter a new month (I can only enter y or no). Can anyone help me sort this out or give me a bit of guidance on how to solve this issue?

Current output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Please enter the monthly rainfall amounts
Rainfall Amount for January:10
Rainfall Amount for February:20
Rainfall Amount for March:30
Rainfall Amount for April:40
Rainfall Amount for May:50
Rainfall Amount for June:60
Rainfall Amount for July:70
Rainfall Amount for August:80
Rainfall Amount for September:33
Rainfall Amount for October:22
Rainfall Amount for November:11
Rainfall Amount for December:9
Search Month Name: June
June Rainfall = 60 inches

Enter 'y' to search again or 'n' to exit:y
Search Month Name: 
Enter 'y' to search again or 'n' to exit:May


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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

const int ARRAY_SIZE = 13;
const int SIZE = 12;

const string months[SIZE] = {"January", "February","March","April","May",
			"June","July","August","September","October","November","December"};

double localRainFall[ARRAY_SIZE];

void searchData();
void getRainFall();

void getRainFall() {

	cout << "Please enter the monthly rainfall amounts\n";

	for (int i = 1; i < ARRAY_SIZE; i++) {

		cout << "Rainfall Amount for " << months[i - 1] << ":";
		cin >> localRainFall[i + 1];

		if (localRainFall[i + 1] < 0) {

			cout << "Negative rainfall amounts are invalid.\n"
					"Please reenter a positive rainfall amount: ";
			cin >> localRainFall[i + 1];
		}
	} searchData();
}

void searchData() {

	string monthName;
	char ans;
	//cout << "Search Month Name: ";
	getline(cin, monthName);

	do {

		cout << "Search Month Name: ";
		getline(cin, monthName);

		for (int i = 0; i < SIZE; i++) {

			if (monthName.compare(months[i]) == 0) {

				cout << monthName << " Rainfall = " << localRainFall[i+2]
					 << " inches" << endl;
			}
		}

		cout << "\nEnter 'y' to search again or 'n' to exit:";
		cin >> ans;

	} while ((ans == 'Y') || (ans == 'y'));
}

int main() {

	getRainFall();

	return 0;
}
A FAQ. You are mixing formatted and unformatted input.

Your >>ans extracts one char from the stream. Does the user hit Enter after the 'y'? That Enter puts a newline into the stream. The following getline reads everything up to a newline. In other words, the getline has already extracted an empty string by the time you start to write a month-name.
Hi keskiverto,

Thank you very much for replying to my post. The user does not hit enter after the 'y'. I see what you mean about the empty string being read. So, I've modified my code a bit. It seems to be working, but the question is does it look proper to you or is there a more conventional/proper way of fixing my issue?

Modification: I've added the getline() function to line 26

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
void searchData() {

	string monthName;
	char ans;

	//cout << "Search Month Name: ";
	getline(cin, monthName);

	do {

		cout << "Search Month Name: ";
		getline(cin, monthName);
		monthName[0] = toupper(monthName[0]);

		for (int i = 0; i < SIZE; i++) {

			if (monthName.compare(months[i]) == 0) {

				cout << monthName << " Rainfall = " << localRainFall[i+2]
					 << " inches" << endl;
			}
		}

		cout << "\nEnter 'y' to search again or 'n' to exit: ";
		cin >> ans;
		getline(cin, monthName);

	} while ((ans == 'Y') || (ans == 'y'));
}
Last edited on
is there a more conventional/proper way of fixing my issue?
Yes.
http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
In your case you need to modify your line 12 getline(cin >> ws, monthName);.
This will skip whitespace characters, including newline, before the string. Thai is exactly what operator>> does before each operation.
Okay, excellent! Thank you for that information MiiNiPaa.
Topic archived. No new replies allowed.