I cannot properly call the functions

Hello. I tried to make a sort of a menu that accesses different smaller programs by calling functions (each menu option accesses a function). The problem is that when I use the calculator, my program exits after the first user input, and the poll generator skips the first user input. I cannot understand what's wrong. Please help!

Thank you!

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  // 29. Test 4 - Menu With Functions.cpp

#include <iostream>
#include <string>

using namespace std;

void beer_song()
{
	int bottles = 99;
	for (;;) // The loop is used to repeat the lyrics of the song.
	{
		if (bottles > 2)
		{
			cout << bottles << " bottles of beer on the wall, " << bottles << " bottles of beer." << endl;
			cout << "Take one down and pass it around, " << --bottles << " bottles of beer on the wall." << endl << endl;
		}
		else if (bottles == 2)
		{
			cout << bottles << " bottles of beer on the wall, " << bottles << " bottles of beer." << endl;
			cout << "Take one down and pass it around, " << --bottles << " bottle of beer on the wall." << endl << endl;
		}
		else if (bottles == 1)
		{
			cout << bottles << " bottle of beer on the wall, " << bottles << " bottle of beer." << endl;
			cout << "Take one down and pass it around, no more bottles of beer on the wall." << endl << endl;
			bottles--;
		}
		else if (bottles == 0)
		{
			cout << "No more bottles of beer on the wall, no more bottles of beer." << endl;
			cout << "Go to the store and buy some more, 99 bottles of beer on the wall." << endl << endl;
			break;
		}
	}
}

void calculator()
{
	string operation;
	double x, y;
	cout << "Enter one of the four arithmetic operations (+, -, *, /): ";
	getline(cin, operation, '\n');
	cout << "Enter the first argument: ";
	cin >> x;
	cout << "Enter the second argument: ";
	cin >> y;

	if (operation == "+")
	{
		cout << "\n\nYour operation is: " << x << " " << operation << " " << y << " = ";
		cout << x + y << endl;
	}
	else if (operation == "*" || operation == "x")
	{
		cout << "\n\nYour operation is: " << x << " " << operation << " " << y << " = ";
		cout << x * y << endl;
	}
	else if (operation == "/" || operation == ":")
	{
		cout << "\n\nYour operation is: " << x << " " << operation << " " << y << " = ";
		cout << x / y << endl;
	}
	else if (operation == "-")
	{
		cout << "\n\nYour operation is: " << x << " " << operation << " " << y << " = ";
		cout << x - y << endl;
	}
	else
	{
		cout << "The type of operation you entered is not valid!" << endl;
	}
}

void poll_generator()
{
	string question, answer1, answer2, answer3;
	int answer, counter, one, two, three, i, max, egal;
	max = 0;
	counter = 1;
	one = 0;
	two = 0;
	three = 0;

	cout << "Please enter your question or the poll header:" << endl << endl;
	getline(cin, question, '\n');
	cout << "Enter the first possible answer: ";
	getline(cin, answer1, '\n');
	cout << "Enter the second possible answer: ";
	getline(cin, answer2, '\n');
	cout << "Enter the third possible answer: ";
	getline(cin, answer3, '\n');
	cout << "The poll will now start..." << endl << endl;

	cout << "The question is: " << question << endl << endl;
	cout << "The answers are:" << endl << endl;
	cout << "1." << answer1 << endl << "2." << answer2 << endl << "3." << answer3 << endl << endl;
	cout << "Answer the question by typing 1, 2 or 3, depending on your opinion. The answer ""0"" will close the poll." << endl << endl;

	for (;;)
	{
		cout << "Voter " << counter << ": ";
		cin >> answer;
		counter++;

		if (answer == 1)
		{
			one++;
		}
		else if (answer == 2)
		{
			two++;
		}
		else if (answer == 3)
		{
			three++;
		}
		else if (answer == 0)
		{
			cout << "\n\nThank you! The poll is over and the results are shown below:" << endl << endl;
			break;
		}
		else
		{
			cout << "This answer is invalid and it won't be counted in the poll!" << endl;
		}
	}

	cout << "1: ";
	for (i = 0; i <= one; i++)
	{
		cout << "| ";
	}
	cout << endl;

	cout << "2: ";
	for (i = 0; i <= two; i++)
	{
		cout << "| ";
	}
	cout << endl;

	cout << "3: ";
	for (i = 0; i <= three; i++)
	{
		cout << "| ";
	}
	cout << endl << endl;

	max = one;
	if (two > max)
	{
		max = two;
	}
	if (three > max)
	{
		max = three;
	}
	if (one == max && two != one && three != one)
	{
		cout << "It looks like the majority of votes are for " << answer1 << "." << endl;
	}
	else if (two == max && one != two && three != two)
	{
		cout << "It looks like the majority of votes are for " << answer2 << "." << endl;
	}
	else if (three == max && one != three && two != three)
	{
		cout << "It looks like the majority of votes are for " << answer3 << "." << endl;
	}
	else if (one == two && (one == max || two == max) && three != one)
	{
		cout << "It looks like is a tie match between " << answer1 << " and " << answer2 << ". Interesting situation..." << endl;
	}
	else if (one == three && (one == max || three == max) && two != one)
	{
		cout << "It looks like is a tie match between " << answer1 << " and " << answer3 << ". Interesting situation..." << endl;
	}
	else if (two == three && (two == max || three == max) && one != two)
	{
		cout << "It looks like is a tie match between " << answer2 << " and " << answer3 << ". Interesting situation..." << endl;
	}
	else if (one == two && one == three)
	{
		cout << "It looks like is a tie match between all of the answers. Is that even possible??" << endl;
	}
}

int main()
{
	cout << "Welcome to our first menu!" << endl << endl;
	for (;;)
	{
		int option;
		cout << "Please select one of the options below by typing the corresponding number: " << endl << endl;
		cout << "1. \"99 Bottles of Beer\" song" << endl;
		cout << "2. Calculator" << endl;
		cout << "3. Poll generator" << endl;
		cout << "0. Exit" << endl;
		cin >> option;

		if (option == 1)
		{
			beer_song();
			break;
		}
		else if (option == 2)
		{
			calculator();
			break;
		}
		else if (option == 3)
		{
			poll_generator();
			break;
		}
		else if (option == 0)
		{
			break;
		}
		else
		{
			cout << "This option does not exist. Please try again!" << endl << endl;
		}
	}

	cin.ignore();
	cin.get();
	return 0;
}
Last edited on
I'm just guessing but: remove the breaks from main, except the one on line 219.
Last edited on
It still doesn't work. The program keeps skipping the first user inputs from the calculator and poll generator.
You have a mix of operator>> and getline(). See Notes:

http://en.cppreference.com/w/cpp/string/basic_string/getline

When used immediately after whitespace-delimited input, e.g. after int n; std::cin >> n;, getline consumes the endline character left on the input stream by operator>>, and returns immediately. A common solution is to ignore all leftover characters on the line of input with cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); before switching to line-oriented input.
The problem is that when I use the calculator, my program exits after the first user input


The program keeps skipping the first user inputs from the calculator


So something has changed..

See here:
http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input
Last edited on
Thank you guys! Your magic solved my problem!
Topic archived. No new replies allowed.