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;
}
|