Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:55:55+00:00 2026-06-18T15:55:55+00:00

I am making a console based calculator application. The application processes user keypresses to

  • 0

I am making a console based calculator application. The application processes user keypresses to perform its operations. Integral inputs work fine; however, I am facing problems writing the code for the case where the user presses a backspace to erase a decimal number.

The code that I wrote to erase decimal spaces is as follows:

decimalcount--; // number of decimal places is subtracted by 1
lnum -= fmod (lnum, pow (10, -decimalcount + 1)); // subtraction
cout << setprecision (decimalcount) << lnum << endl; // display the code

However, for certain numbers like 12.00400679, the values are being improperly subtracted:

12.00400679
12.00400670
12.0040060
12.004000
12.00400
12.0040
12.000
11.90
11.0
10

The full source of the program is as below:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
using namespace std;

int sgn (double x)
{
    if (x < 0)
    {
        return -1;
    }
    return 1;
}

int main ()
{
    cout.setf (ios::fixed);

    double lnum = 0, expr = 0;
    int decimalcount = 0;
    char ch, op;

    while (true)
    {
        ch = _getch ();
        if (isdigit (ch))
        {

            if (! decimalcount)
            {
                if (sgn (lnum) == sgn (lnum * 10 + sgn (lnum) * (ch - 48)))
                {
                    lnum = lnum * 10 + sgn(lnum) * (ch - 48);
                    cout << setprecision (0) << lnum << endl;
                }
            }
            else
            {
                if (decimalcount < 9)
                {
                    lnum += sgn (lnum) * (ch - 48) * pow (10, -decimalcount);
                    cout << setprecision (decimalcount) << lnum << endl;
                    decimalcount++;
                }
            }
        }
        else if (ch == '\b')
        {

            if (! decimalcount)
            {
                lnum -= fmod (lnum, 10);
                lnum /= 10;
                cout << setprecision (0) << lnum << endl;
            }
          // This is where I am having problems
            else
            {
                decimalcount--;
                lnum -= fmod (lnum, pow (10, -decimalcount + 1));
                cout << setprecision (decimalcount) << lnum << endl;
            }
        }
        else if (ch == '.')
        {
            if (! decimalcount)
            {
                decimalcount = 1;
                cout << setprecision (decimalcount) << lnum << endl;
            }
        }
        else if (ch == 'x')
        {
            return 0;
        }
    }

}

Can anyone show me where I am doing it wrong?

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:55:56+00:00Added an answer on June 18, 2026 at 3:55 pm

    You’re converting too early. During input, you should keep the
    input as a string, and only convert to your internal format when
    the input is finished, and you’re ready to do the calculations.

    But you seem to be doing things the hard way. Why not just use
    std::getline on std::cin, and parse the text you receive?
    That way, the system handles things like back space, and you
    don’t have to. If for some reason this isn’t acceptable, you
    should still factor the input out into a separate function,
    which does more or less the same thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a console-based application in Objective-C which relies on being able to clear
I'm making a console-based game of black jack that prompts the user asking him/her
I am making application for user login based authentication programmatically that is using a
I have been using the Rails console a fair bit lately and its making
Making a simple application, so when the user logs out of Windows, it of
I'm making a small console-based rpg, to brush up on my programming skills. I
I'm making a console application for unix platforms, and I'm using the curses (or
I am making a document based application using CoreData. I can create a new
I'm making a console calculator, and I want to remove any whitespace that the
I'm making a console based game over telnet. It would be nice to play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.