'c++ How to detect an input character as ENTER [duplicate]
I'm trying to do a simple terminal calculator, but i'm stuck in a problem.
#include <iostream>
using namespace std;
int main(){
float n1, n2 = 0;
char op;
cout << "Value: ";
cin >> n2; //as you can see, i MUST click enter to the number to be registered.
while (true){
cin.ignore(1);
cout << "Operator: ";
system("stty raw");
op = getchar();
system("stty cooked");
cout << "\nValue: ";
cin >> n1;
if(n1)
{
if(op == '+')
{
n2 = n2 + n1;
cout << " =\n " << n2 << "\n";
}
if(op == '-')
{
n2 = n2 - n1;
cout << " =\n " << n2 << endl;
}
}
else
{
cout << "wrong."; //not really using it yet.
}
}
}
I want to read every character of keyboard input (think it's stdin), and when it reads the "+" operator, automatically get a new line and stop the keyboard input.
Like: im typing 5... 3... 4... and to choose the operator just click "+", not ENTER to run the number, and then choose the operator.
Hope i could explain.
Thanks in advance, i'm an extreme begginer in c++, so be nice pls :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
