Cin (input) is being ignored in do while loop.

Hello.

The cin (input) is being ignored after the post-test.

Any solution?

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

double addition_function( double funcadd1, double funcadd2 );

double subtraction_function( double funcsub1, double funcsub2 );

double multiplication_function( double funcmul1, double funcmul2 );

double division_function( double funcdiv1, double funcdiv2 );

double root_function( double func_root_num, double func_root );

double power_function( double func_power_num, double func_power );

void introduction()
{
	// Introduction.
	// ...

	std::cout << "Calculator.\n";
	std::cout << "Input the number left of the type of operation.\n";
	std::cout << "\n";
	std::cout << "1. Addition.\n";
	std::cout << "2. Subtraction.\n";
	std::cout << "3. Multiplication.\n";
	std::cout << "4. Divison.\n";
	std::cout << "5. Find a root of a number.\n";
	std::cout << "6. Raise a number to a power.\n";
}

int main()
{
	double add1 = 0;
	double add2 = 0;

	double sub1 = 0;
	double sub2 = 0;

	double mul1 = 0;
	double mul2 = 0;

	double div1 = 0;
	double div2 = 0;

	double root_num = 0;
	double root = 0;

	double power_num = 0;
	double power = 0;
		
	std::string use_again = "y";

	unsigned short int main_input = 0;
	
	do
	{	
		introduction();

		std::cin >> main_input;
		std::cin.ignore();

		if ( main_input == 1 )
		{
			std::cout << "Addition:\n";

			std::cout << "Input first number:\n";
				std::cin >> add1;
				std::cin.ignore();
	
			std::cout << "Input second number:\n";
				std::cin >> add2;
				std::cin.ignore();

			std::cout << add1 << " + " << add2 << " = " << addition_function( add1, add2 ) << ".\n";
		}
		
		else if ( main_input == 2 )
		{
			std::cout << "Subtraction:\n";
			
			std::cout << "Input first number:\n";
				std::cin >> sub1;
				std::cin.ignore();
				
			std::cout << "Input second number:\n";
				std::cin >> sub2;
				std::cin.ignore();

			std::cout << sub1 << " - " << sub2 << " = " << subtraction_function( sub1, sub2 ) << ".\n";	
		}		
		
		else if ( main_input == 3 )
		{
			std::cout << "Multiplication:\n";
			
			std::cout << "Input the first number:\n";
				std::cin >> mul1;
				std::cin.ignore();
			
			std::cout << "Input the second number:\n";
				std::cin >> mul2;
				std::cin.ignore();
			
			std::cout << mul1 << " * " << mul2 << " = " << multiplication_function( mul1, mul2 ) << ".\n";
		}

		else if ( main_input == 4 )
		{
			std::cout << "Division:\n";
			
			std::cout << "Input first number:\n";
				std::cin >> div1;
				std::cin.ignore();

			std::cout << "Input second number:\n";
				std::cin >> div2;
				std::cin.ignore();
			
			std::cout << div1 << "/" << div2 << " = " << division_function( div1, div2 ) << ".\n";
		}

		else if ( main_input == 5 )
		{
			std::cout << "Find a root of a number:\n";
					
			std::cout << "Input the number:\n";
				std::cin >> root_num;
				std::cin.ignore();
					
			std::cout << "Input the root:\n";
				std::cin >> root;	
				std::cin.ignore();

			std::cout << root_function( root_num, root ) << ".\n";
		}

		else
		{
			std::cout << "Raise a number to a power.\n";
				
			std::cout << "Input the number:\n";
				std::cin >> power_num;
				std::cin.ignore();
		
			std::cout << "Input the power:\n";
				std::cin >> power;
				std::cin.ignore();

			std::cout << power_num << "^" << power << " = " << power_function( power_num, power ) << ".";
		}
		
		main_input = 0;

		add1 = 0;
		add2 = 0;
		sub1 = 0;
		sub2 = 0;
		mul1 = 0;
		mul2 = 0;
		div1 = 0;
		div2 = 0;
		root_num = 0;
		root = 0;
		power_num = 0;
		power = 0;
		
		std::cin.get();
	}
	while ( use_again == "y" );

	return 0;
}

double addition_function( double funcadd1, double funcadd2 )
{
	return funcadd1 + funcadd2;
}

double subtraction_function( double funcsub1, double funcsub2 )
{
	return funcsub1 - funcsub2;
}

double multiplication_function( double funcmul1, double funcmul2 )
{
	return funcmul1 * funcmul2;
}

double division_function( double funcdiv1, double funcdiv2 )
{
	return funcdiv1/funcdiv2;
}

double root_function( double func_root_num, double func_root )
{
	return pow ( func_root_num, 1/func_root );
}

double power_function( double func_power_num, double func_power )
{
	return pow ( func_power_num, func_power );
}
Topic archived. No new replies allowed.