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

  • Home
  • SEARCH
  • 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 8295191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:29:41+00:00 2026-06-08T14:29:41+00:00

I have a simple calculator that allows me to keep entering new numbers and

  • 0

I have a simple calculator that allows me to keep entering new numbers and spits out a profit amount. But I’d like to be able to use the arrow key (up) to get the last entry.

I have this:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// ------------------------------------------------

int main (int argCount, char *argv[]) {
    float 
        shares = atof(argv[1]),
        price = atof(argv[2]),

        // Commission fees
        fee = 4.95,

        // Total price paid (counts buy + sell commission)
        base = (price * shares) + (fee * 2),
        profit;

    printf("TOTAL BUY: %.3f\n", base);

    /**
     * Catch the input and calculate the 
     * gain based on the new input.
     */
    char sell[32];

    while (1) {
        printf(": ");
        fflush(stdout);
        fgets(sell, sizeof(sell), stdin);

        profit = (atof(sell) * shares) - base;
        printf(": %.3f", profit);

        // Show [DOWN] if the estimate is negative
        if (profit < 0)
            printf("\33[31m [DOWN]\33[0m\n");

        // Show [UP] if the estimate is positive
        else printf("\33[32m [UP]\33[0m\n");
    }

    return 0;
}

UPDATE: The answer is below.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <readline/readline.h>
#include <readline/history.h>

// ------------------------------------------------

int main (int argCount, char *argv[]) {
    float 
        shares = atof(argv[1]),
        price = atof(argv[2]),

        // Commission fees
        fee = 4.95,

        // Total price paid
        base = (price * shares) + (fee * 2),
        profit;

    printf("TOTAL BUY: %.3f\n", base);

    /**
     * Catch the users input and calculate the 
     * gain based on the new input.
     *
     * This makes it easy for active traders or
     * investors to calculate a proposed gain.
     */
    char* input, prompt[100];

    for(;;) {
        rl_bind_key('\t', rl_complete);
        snprintf(prompt, sizeof(prompt), ": ");

        if (!(input = readline(prompt)))
            break;

        add_history(input);

        profit = (atof(input) * shares) - base;
        printf(": %.3f", profit);

        // Show [DOWN] if the estimate is negative
        if (profit < 0)
            printf("\33[31m [DOWN]\33[0m\n");

        // Show [UP] if the estimate is positive
        else printf("\33[32m [UP]\33[0m\n");
        free(input);
    }

    return 0;
}
  • 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-08T14:29:50+00:00Added an answer on June 8, 2026 at 2:29 pm

    You can use the GNU readline library, which provides line-editing and command history. Here is the project’s homepage. It seems to be what you want.

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

Sidebar

Related Questions

I have a simple HTML Form that allows the user to enter some text
I have a form that allows a user to update their profile information, but
I've got a simple table that allows users to enter 2 numbers, and then
i'm using Moscow ML combined with CGI. I have a site that calculates simple
I have a SQL statement (MS SQL Server 2005) that does a simple calculation
I have coded my calculator in simple mode, I have added a button for
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass
I made a program in Xcode, being a simple calculator that takes a first
I have a simple java gui calculator, with 3 number systems (there are some

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.