Quick cin.ignore() Question

Hi, I'm coding a calculator and made a condition for whether it should exit or return to the beginning once an equation has been done. The only thing is that to have this work, I need a cin.ignore() since there will be something in the buffer making the code not work properly. However, this makes the user need to press enter twice . It's hard to explain without the code so here it is :

1
2
3
4
5
6
7
8
9
10
11
12
13

	std::cin.ignore(std::numeric_limits<int>::max(), '\n');
	
	std::cout << '\n' << "Press '/' To Exit." << '\n' << "Press Enter Twice To Continue Calculations." << '\n' <<
		"-----------------------------------------------------------------------------" << '\n';

	std::getline(std::cin, e); //e Is A String

	if (e == "/") // Pressing / Exits Program
	{
		std::cout << '\n';
		std::exit(0);
	}



If e is any other value, then it moves on to the rest of the code where it clears the buffer of errors and values then there is a "goto" function - leading back to the beginning.

The problem is that when pressing Enter to move on - you have to press it twice. This is because of the first line : std::cin.ignore(std::numeric_limits<int>::max(), '\n'); . This line is needed so that e doesn't always get a '\n' input, but forces the person to click enter twice - simple yet frustrating when you want to have the perfect code !

So my two questions are this :

1. The std::cin.ignore() is suppose to ignore inputs until it gets to a '\n' . This is why (at least I think) you need to press enter twice to go back into the equation. But if this is true, how come it accepts this '/' the first time instead of ignoring it until it gets a '\n' value??

2. Is there a way to clear the buffer without cin.ignore - so that the code will allow for needing only one hit of the enter key?

BTW : There is a way I can "bypass" this problem - by replacing std::getline with just plain std::cin . In this case, you can enter any number/character (other than enter/space) for e (e is a string) and press enter, and it'll accept it. But, I don't like this method because I want someone to be able to input anything (including enter) and be able to move on.
^ However, this again brings me to my first question, why will it accept those inputs even though they were entered before hitting enter? Shouldn't they be ignored until after the user presses enter one time first?

An explanation would be helpful, thanks !

EDIT : It may be hard to grasp without ALL the code - so here is the full calculator code - though you certainly don't have to go over it all.

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
#include "stdafx.h"

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <string>
#include <thread>

using namespace std;

double a;
double x;
double y;
char z;
std::string e;

int subtract()
{
	a = x - y;
	return a;
}

int add()
{
	a = x + y;
	return a;
}

int divide()
{
	a = x / y;
	return a;
}

int multiply()
{
	a = x * y;
	return a;
}

int power()
{
	a = pow(x, y);
	return a;
}

unsigned long long factorial(int f) //Loops Until int f = 1 
{									//Limit Is 64 Factorial
	if (f < 0)
	{
		std::cout << '\n' << "Cannot Have A Negative Factorial" << '\n' << std::endl; 
		std::exit(0);
	}
	if (f == 1) return a = 1;
	if (f == 0) return a = 1;
	else return a = f * factorial(f - 1);

}


int main()
{
Equation:
	std::cout << "Type In Equation: ";
	std::cin >> x;
	std::cin >> z;
	if (z == '!') // To Skip Getting A "y" Value For Factorials
	{
		goto Operation;
	}
	std::cin >> y;


	if (std::cin.fail())
	{
		std::cout << '\n' << "Not A Number" << std::endl;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<int>::max(), '\n'); // Used When Code Returns To Equation To Clear Cin Errors/Clear Cin Values
		goto Equation;
	}
	
	
Operation:
	switch (z)
	{
	case '-':
		subtract();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '+':
		add();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '/':
		if (x == 0)
		{
			std::cout << '\n' << "Not Possible" << std::endl;
			break;
		}
		divide();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '*':
		multiply();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '^':
		power();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '%':
		if (x < y)
		{
			std::cout << '\n' << "Not Applicable" << std::endl;
		}
		else
		{	// Won't Work If Not Placed Here. Can't Perform % With Anything But Integers.
			int b = x;
			int c = y;
			a = b % c;
			std::cout << '\n' << "The Remainder is: " << a << std::endl;
		}
		break;
	case '!':
	{
		factorial(x);
		std::cout << '\n' << "The Factorial Is: " << a << std::endl;
		break;
	}
	default:
		std::cout << '\n' << "Unknown Operation: '" << z << "'" << '\n' << '\n';
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<int>::max(), '\n');
		goto Equation;
	}

	std::cin.ignore(std::numeric_limits<int>::max(), '\n');
	
	std::cout << '\n' << "Press '/' To Exit." << '\n' << "Press Enter Twice To Continue Calculations." << '\n' <<
		"-----------------------------------------------------------------------------" << '\n';

	std::getline(std::cin, e);

	if (e == "/") // Pressing / Exits Program
	{
		std::cout << '\n';
		std::exit(0);
	}

	
	
	goto Happy;

	Happy:

	std::cin.clear();
	std::cin.ignore(std::numeric_limits<int>::max(), '\n');
	
	std::cout << '\n';

	goto Equation;

	return 0;
}
Last edited on
Hello zapshe,

From what you describe, by having to press enter twice, it sounds that there is a "cin.ignore()" that does not need to be there. I would put a comment on line 138 and see what happens.

Your use of the "goto"s is bad form and bad programming. These are unconditional jumps around your program with no regard about what might hanging around waiting to be done and no way back to it. Most "goto"s can be replaced with do/while or while loops.

Line 143 does not need to be a "std::string", a single "char" will do.

Line 148 "return 0;" would be just as effective. I tend to leave "exit()" for things that are unusual.

Because of line 162 line 164 is never reached.

When I get the program loaded up I see if there is anything else and any other suggestions I come up with.

Hope that helps,

Andy
Hello zapshe,

1. The std::cin.ignore() is suppose to ignore inputs until it gets to a '\n' . This is why (at least I think) you need to press enter twice to go back into the equation. But if this is true, how come it accepts this '/' the first time instead of ignoring it until it gets a '\n' value??

The first parameter in the "std::cin.ignore()" that you are using just comes down to a very large number. This should be the maximum number of characters that the input buffer can hold. This number may be different on different systems or even header files for different compilers.

You need to press enter twice because there is nothing in the buffer to ignore. It is waiting for something to be entered to ignore. some people will use this to pause the program before the "return 0;".

2. Is there a way to clear the buffer without cin.ignore - so that the code will allow for needing only one hit of the enter key?

You could change line 16 from a "std::string" to a "char" and just use "std::cin >> e;" or comment out the "std::cin.ignore()" and see if it makes any difference.

It is best not to edit your post and add something new. Better to make a new message and ask your questions. I all most missed your your new questions because of this. Also it is not a good idea to change you code in the original post.

Hope that helps,

Andy
Hello zapshe,

I ended up making comments in the program, so instead of writing everything up I will post the code with comments. If you have any questions let me know.

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
#include <iostream>
#include <string>  // <--- This was included twice.
#include <cmath>
#include <cctype>  // <--- Added for the std::tolower function.
#include <iomanip>
#include <thread>  // <--- Not sure why you need this?

//using namespace std;  // <--- Best not to use. Since yu have qualified what is in the standard name space with "std::" this is not needed. It WILL get you introuble some day.

//  These may be nice as global variables, but I would rather put them in main and send them to the needed functions.
//  Also do not count on these variables being initialized here. Better to initialize your variables and be sure.
double a{};
double x{};
double y{};
char z{};
std::string e;  // <--- Found it does work better as a std::string. Empty to start with. Does not need initialized.

//  These functions could be shortened to just "return x ? y even the pow function. With your global variables this 
//  does not work. Since you are using global variables the return statements are not needed. And the lines that call
//  these functions do not make use of the return value.

int subtract()
{
	a = x - y;
	return a;
}

int add()
{
	a = x + y;
	return a;
}

int divide()
{
	a = x / y;
	return a;
}

int multiply()
{
	a = x * y;
	return a;
}

int power()
{
	a = pow(x, y);
	return a;
}

unsigned long long factorial(int f) //Loops Until int f = 1 
{									//Limit Is 64 Factorial
	if (f < 0)
	{
		std::cout << '\n' << "Cannot Have A Negative Factorial" << '\n' << std::endl;
		std::exit(0);
	}
	if (f == 1) return a = 1;
	if (f == 0) return a = 1;
	else return a = f * factorial(f - 1);

}


int main()
{
Equation:
	std::cout << "Type In Equation: ";
	std::cin >> x;
	std::cin >> z;
	if (z == '!') // To Skip Getting A "y" Value For Factorials
	{
		goto Operation;
	}
	std::cin >> y;


	if (std::cin.fail())
	{
		std::cout << '\n' << "Not A Number" << std::endl;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<int>::max(), '\n'); // Used When Code Returns To Equation To Clear Cin Errors/Clear Cin Values
		goto Equation;
	}


Operation:
	switch (z)
	{
	case '-':
		subtract();
		std::cout << '\n' << "The Answer Is: " << std::setprecision(7) << a << std::endl;  // <--- Changed "Sum".
		break;
	case '+':
		add();
		std::cout << '\n' << "The Answer Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '/':
		if (x == 0)
		{
			std::cout << '\n' << "Not Possible" << std::endl;
			break;
		}
		divide();
		std::cout << '\n' << "The Answer Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '*':
		multiply();
		std::cout << '\n' << "The Answer Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '^':
		power();
		std::cout << '\n' << "The Answer Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '%':
		if (x < y)
		{
			std::cout << '\n' << "Not Applicable" << std::endl;
		}
		else
		{	// Won't Work If Not Placed Here. Can't Perform % With Anything But Integers.
			int b = x;
			int c = y;
			a = b % c;
			std::cout << '\n' << "The Remainder is: " << a << std::endl;
		}
		break;
	case '!':
	{
		//  <--- This function returns a value that is not captured and used. Who knows what the value of "a" is at
		//  this point.
		//factorial(x);  // <--- You can just as easily put this in the next line in place of "a".
		//std::cout << '\n' << "The Factorial Is: " << a << std::endl;
		std::cout << '\n' << "The Factorial Is: " << factorial(x) << std::endl;
		break;
	}
	default:
		std::cout << '\n' << "Unknown Operation: '" << z << "'" << '\n' << '\n';
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<int>::max(), '\n');
		goto Equation;
	}

	std::cin.ignore(std::numeric_limits<int>::max(), '\n');

	std::cout << '\n' << "Press 'e' To Exit." << '\n' << "Press Enter To Continue Calculations." << '\n' <<
		"-----------------------------------------------------------------------------" << '\n';

	std::getline(std::cin, e);
	e[0] = std::tolower(e[0]);  // <--- Added. Needs header file cctype.

	if (e == "e") // Pressing 'e' Exits Program
	{
		std::cout << '\n';
		std::exit(0);
	}

	//goto Happy;  // <--- Since "Happy:" is the next line there is no need for this line.

Happy:  // <--- Left this here for now.

	//std::cin.clear();  // <--- Not needed because there is nothing to clear
	//std::cin.ignore(std::numeric_limits<int>::max(), '\n');  // <--- Causes the wait for the second enter.

	std::cout << '\n';

	goto Equation;

	return 0;
}


Overall the program works. Some of the small things like asking for the equation and giving an example of how it should be entered.

I still need to do something with the "goto" statements.

Hope that helps,

Andy
Thanks Andy for the detailed posts. I had already put my question into the post, I just added some extra details as I thought they would be useful. Now - I'll try to address some of the code.

Line 143 is needed, otherwise pressing "/" wouldn't exit the program since it would have something else already in the buffer.

I noticed you changed your mind about e being a string, but the reason I made it such was to be able to use getline in order to allow for pressing "enter" (since cin wouldn't accept white space) it can start up again.

I realized my header problem after posting, I had just been editing the code so I was adding headers in for different things I was trying out. Same for the goto Happy line, I was trying something out and didn't take it out since I was still messing with it. Since it took it to the very next line I figured I'd leave it there until I was done with it.

Thanks for letting me know about these "global" variables. The reason the functions return a; is because when I started this calculator it was only using integers, then I started making it more complex. However, those functions need a return statement, so best to simply leave them be.

What you wrote for case '!' will crash the console if a value over 64 is placed. My version will simply output 0. I intend to make something to handle those bigger outputs (as well as the inaccuracy in the answers after around 20 factorial).

Lastly, I'v never seen e[0] = std::tolower(e[0]); before. What does this do?

Thanks again Andy, but I'm still confused with my original question. I understand that the cin.ignore line is causing the need to press enter twice - But how come that isn't the cause when inputting "/" ? It simply accepts this value after pressing enter once, even though it should be ignoring all values until '\n' is found? Thanks again.
As far as possible, avoid variables at namespace scope.
Unless it makes the code more readable, avoid unconditional jump with goto
As far as possible, avoid quitting the program with a direct call to [tt]std::exit()[tt]

Here's a version without any of the above:

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

long long subtract( long long x, unsigned int y ) { return x-y ; }
unsigned long long add( unsigned long long x, unsigned int y ) { return x+y ; }
double divide( double x, unsigned int y ) { return x/y ; }
unsigned long long multiply( unsigned long long x, unsigned int y ) { return x*y ; }
double power( unsigned int x, unsigned int y ) { return std::llround( std::pow(x,y) ) ; }
unsigned long long factorial( unsigned int n ) { return n < 2 ? 1 : n * factorial(n-1) ; }

bool get_equation( unsigned int& x, char& op, unsigned int& y )
{
    std::cout << "type in equation (type 'q' to quit) " ;
    char first ;
    if( std::cin >> first && ( first == 'q' || first == 'Q' ) ) return false ;

    // not quit, place the first character (the character that was read) back into the input buffer
    std::cin.putback(first) ;

    if( std::cin >> x && std::cin >> op )
    {
        if( op == '!' )
        {
            std::cout << x << "! == " ;
            return true ;
        }
        else if( std::cin >> y )
        {
            std::cout << x << ' ' << op << ' ' << y << " == " ;
            return true ;
        }
    }

    std::cout << "invalid input: try again\n" ;
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    return get_equation( x, op, y ) ;
}

void eval_print( unsigned int x, char op, unsigned int y )
{
    switch(op)
    {
        case '+' : std::cout << add(x,y) << '\n' ; break ;

        case '-' : std::cout << subtract(x,y) << '\n' ; break ;

        case '*' : std::cout << multiply(x,y) << '\n' ; break ;

        case '/' :
            if( y == 0 ) std::cout << "divide by zero!\n" ;
            else std::cout << divide(x,y) << '\n' ;
            break ;

        case '!' :
            if( x > 18 ) std::cout << "out of range!\n" ;
            else std::cout << factorial(x) << '\n' ;
            break ;

        default: std::cout << "invalid operation\n" ;
    }
}

int main()
{
    unsigned int x ;
    char op ;
    unsigned int y ;

    while( get_equation( x, op, y ) )
    {
        eval_print( x, op, y ) ;
        std::cout << '\n' ;
    }
}
Hi JLBorges - Thanks for the code and advice! I have comments on the code you wrote - However, my main concern was why I needed to press enter only once for inputting "/" but twice to input "'\n'" - But I think I figured it out. std::cin.ignore will ignore up to '/n' then accept later outputs. Since there is a '/n' already in the buffer, entering "/" ignores that '/n' and accepts the "/". However, pressing enter enters the '/n' value already in buffer (which gets ignored), then you have to press enter again to insert a new '/n' into cin . - I've got that correctly?

Anyway, about your code - I see that you did several things to avoid having to use the goto, exit function, and other stuff. While I understand that these functions can easily be used in such a way that may cause problems in the future, I don't see how that can be the case for coding a calculator - So is it necessary to code it differently?

Also, sorry to nitpick, but in your code - x and y are unsigned - meaning one can't do something like " -9 + -12 " . Moreover, the code will output the variables and the operation before knowing whether the operation is valid or not - so one can get an output like this " 10 a 12 == invalid operation " (which isn't bad, but personally invalid operation alone would be better). However, I like the way you left int main() at the bottom and only calls the functions that handle everything. So pressing q returns "false" which means the while function never happens - subsequently ending the code - without need of a return 0; or exit function.

BTW, I'm still new to this, so some of these functions/general coding techniques I haven't seen or used before - like std::cin.putback(first) ; . And while I've seen bool, I've never used it yet. So I do have some questions about the code. Why do you use & in bool get_equation? Without the & it doesn't work, but what's its function? My other other question is why did you use an if function for if( std::cin >> x && std::cin >> op ) ? It works the same without having the code in ( ) being part of the if function right?

Anyway, Thanks for uploading that code - Being a noob at coding means I feel the need to format as I've learned to format things. Seeing your code allowed me to get a new take on coding and will help me expand my own coding style, so thanks a lot !

EDIT : Here is my current code

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
#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>


double a;
double x;
double y;
char z;


int subtract()
{
	a = x - y;
	return a;
}

int add()
{
	a = x + y;
	return a;
}

int divide()
{
	a = x / y;
	return a;
}

int multiply()
{
	a = x * y;
	return a;
}

int power()
{
	a = pow(x, y);
	return a;
}

unsigned long long factorial(int f) //Loops Until int f = 1 
{									//Limit Is 64 Factorial
	if (f < 0)
	{
		std::cout << '\n' << "Cannot Have A Negative Factorial" << '\n' << std::endl; 
		return 0;
	}
	if (f > 20)
	{
		std::cout << '\n' << "Factorial Too Big!" << '\n' << std::endl;
		return 0;
	}
	if (f == 1) return a = 1;
	if (f == 0) return a = 1;
	else return a = f * factorial(f - 1);

}


int main()
{
	std::cout << "Press / To Exit." << '\n' << "Use 'a' To Symbolize Previous Value." << '\n' << "----------------------------------------" 
	<< '\n';
Equation:
	std::cout << "Type In Equation: ";
	char e;
	std::cin >> e;

	if (e == '/') // Pressing "/" Stops Program 
	{
		std::cout << '\n';
		return 0;
	}
	if (e == 'a' || e == 'A') // Input "a" Will Give You You're Previous Answer To Work With
	{
		if (a == 0)
		{
			std::cout << '\n' << "No Previous Value" << '\n' << '\n';
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<int>::max(), '\n');
			goto Equation;
		}
		x = a; 
		std::cin.putback(e);
		std::cin.ignore(10, 'a');
		goto Op;
	}
	if (e == 'A') // Input "A" Will Give You You're Previous Answer To Work With
	{
		if (a == 0)
		{
			std::cout << '\n' << "No Previous Value" << '\n' << '\n';
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<int>::max(), '\n');
			goto Equation;
		}
		x = a;
		std::cin.putback(e);
		std::cin.ignore(10, 'A');
		goto Op;
	}

		std::cin.putback(e);
	
	std::cin >> x;
	Op:
	std::cin >> z;
	if (z == '!') // To Skip Getting A "y" Value For Factorials
	{
		goto Operation;
	}
	std::cin >> y;


	if (std::cin.fail())
	{
		std::cout << '\n' << "Not A Number" << '\n' << std::endl;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<int>::max(), '\n'); // Used When Code Returns To Equation To Clear Cin Errors/Clear Cin Values
		goto Equation;
	}

	
Operation:
	switch (z)
	{
	case '-':
		subtract();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '+':
		add();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '/':
		if (x == 0 || y == 0)
		{
			std::cout << '\n' << "Not Possible" << std::endl;
			break;
		}
		divide();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '*':
		multiply();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case 'x':
		multiply();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '^':
		power();
		std::cout << '\n' << "The Sum Is: " << std::setprecision(7) << a << std::endl;
		break;
	case '%':
		if (x < y)
		{
			std::cout << '\n' << "Not Applicable" << std::endl;
		}
		else
		{	// Won't Work If Not Placed Here. Can't Perform % With Anything But Integers.
			int b = x;
			int c = y;
			a = b % c;
			std::cout << '\n' << "The Remainder is: " << a << std::endl;
		}
		break;
	case '!':
	{
		factorial(x);
		if (a == 0)
		{
			break;
		}
		std::cout << '\n' << "The Factorial Is: " << a << std::endl;
		break;
	}
	default:
		std::cout << '\n' << "Unknown Operation: '" << z << "'" << '\n' << '\n';
	}

	double c(a);

	std::cin.clear();
	std::cin.ignore(std::numeric_limits<int>::max(), '\n');
	
	std::cout << '\n';

	goto Equation;

	return 0;
}
Last edited on
> While I understand that these functions can easily be used in such a way that may cause problems in the future,
> I don't see how that can be the case for coding a calculator - So is it necessary to code it differently?

While learning programming, it is important to cultivate good programming habits: a sound programming hygiene. Unlearning old habits is a lot harder than you imagine.


> Why do you use & in bool get_equation? Without the & it doesn't work, but what's its function?

The & indicates that the parameters are passed by (lvalue) reference. With a parameter passed by reference, the caller of the function and the called function use the same object; when the function modifies the object, the change is visible to the caller.
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/


> why did you use an if function for if( std::cin >> x && std::cin >> op )

It verifies that the user did enter a valid number for x. If, for instance, the user entered "abc" as the input, the condition in if( std::cin >> x && std::cin >> op ) would evaluate to false, and control would reach line 35 std::cout << "invalid input: try again\n" ; ... clear error state etc.
Topic archived. No new replies allowed.