0

I'm making a calculator and it´s supposed to be able to "erase or go back". Most of the other questions I have found online talk about how they reset the program, not how to go back/erase one step.

EDIT: I want to make a calculator that can: add, sub, div, multi, erase and reset. I know/will figure out how to do all of these but not the erase one. Like on a normal calculator you can use the AC/EC button to erase the last number that was written, but i dont know how to do this in code or what to search for to understand it.

This is the code so far:

#include<iostream>
using namespace std;

int main()
{
    char op;
    float num1,num2;

    cout << "Enter operator either + or - or * or / :";
    cin >>op;
    cout<<">Enter two operands :";
    cin >>num1>>num2;

    switch(op)
    {
    case'+':
        cout<<num1+num2;
        break;

    case '-':
        cout<<num1-num2;
        break;

    case '*':
        cout<<num1*num2;
        break;

    case '/':
        cout<<"The answer is " <<num1/num2;
        break;

    default:

        cout<<"The operator is not correct ";

    }
    return 0;
}

MFerguson
  • 1,739
  • 9
  • 17
  • 30
Alice
  • 1
  • 2
  • 1
    What does it mean for a calculator to go back one step? – mkrieger1 Dec 20 '22 at 20:34
  • Are you talking about erasing a previous character, symbol or number? – Thomas Matthews Dec 20 '22 at 20:35
  • The problem here is that I don't understand what you mean by 'erase or go back', can you explain it please? – john Dec 20 '22 at 20:39
  • Hi! Thank you for your answers, i am sorry if i were not clear. I need the calculator to be able to erase the last number that was written. Like how you can use the "AC" button on a phone calculator to "go back one step" and erase the last written number so i can put in a new number instead. – Alice Dec 20 '22 at 20:40
  • @Alice Doesn't the backspace key work for you? – john Dec 20 '22 at 20:41
  • @Alice I think you still need to spell out precisely what you mean, what keys are pressed, what appears on the screen, etc, etc. This might be possible in C++, but it might not, it depends on the details. – john Dec 20 '22 at 20:43
  • What is the topic of the chapter in your C++ textbook where this practice problem is from? Practice problems like this one are designed to improve the understanding of each particular C++ topic that's presented in each chapter in a textbook. It is unclear what, specifically, you are meant to learn and understand, by this practice program. So, explaining the chapter's topic will help in pointing you in the right direction. – Sam Varshavchik Dec 20 '22 at 20:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 20 '22 at 20:54
  • Hey! Thanks for your answers. Like a normal calculator, I want my code to be able to erase the last number that was written. Not by using the backspace, which usually doesn´t exist on a normal calculator, but instead there is an AC/EC button. This button erases your last written number and allows you to write another one instead. I don't know what to search for or how to do this in code. My schoolbook lacks this information and it has not helped me much. I study remotely and it is almost impossible to get in touch with teachers. I'm doing my best but need some support. – Alice Dec 20 '22 at 20:59
  • @Alice -- The problem is that your code doesn't even come close to what you are looking for, as there is no attempt to perform the task that you are seeking. Any answer would be just an explanation (like storing your numbers in a stack or similar data structure), if not that an entire new set of code (which is frowned upon). – PaulMcKenzie Dec 20 '22 at 21:03
  • Oh okey, i know im only at the beginning of the code and if it´s not possible to guide me then i understand it. If you know what i should search for to understand it/find some help i would appriciate it. – Alice Dec 20 '22 at 21:07
  • Maybe researching the term "state machine" could help you. – mkrieger1 Dec 20 '22 at 21:10
  • @Alice *not how to go back/erase one step* -- What if the EC "button" is pressed twice? Does it clear both numbers (first and second)? If so, then the problem is more sophisticated than what you are probably used to. This is where a data structure (probably a stack) is what should be used to hold the input. – PaulMcKenzie Dec 20 '22 at 21:11
  • See Answer to [(C language) How can i use backspace while using getchar(); in this function?](https://stackoverflow.com/a/53249714/3422102) which shows how to handle the `del` key to backup over the last character(s) typed to provide minimal editing. The ASCII codes are the same between C/C++, the output routines are the only difference `std::cout` verses `fputs()`. Note, how you enable *Raw* mode for your terminal will differ if you are on Windows or Linux. – David C. Rankin Dec 21 '22 at 00:51

1 Answers1

0

This is not doable with your current architecture. The key problem is that your program doesn't have control over the input data until user presses Enter key which is too late for you to make any changes.

For simple edition the Backspace button could help you.

For the behavior you need, you have to manage input character by character (see the getch() function), combine characters to values (see atof() function, sscanf() function, etc.) and if you see the Esc character or some other one you want to use as AC, you could restart collecting the number/string.

Damir Tenishev
  • 1,275
  • 3
  • 14