Thread: issue trying to store math equation answers in readline()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33

    issue trying to store math equation answers in readline()

    So I decided to try and make a command line calculator, but discovered that I don't understand memory management and buffering well enough to parse out an expression like this:

    5 + (4 * 1)

    it would be really interesting to make a command line version of the graphical calculators that can do that, something different from bc and python.

    So, I've settled just for making a really simple calculator as a project to help me understand C better. This version works:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<clearBuff.h>
    
    int main()
    {
      while(1)
      {
        char operator;
        long double number1, number2, answer;
    
        printf("Number: ");
        scanf("%Lf", &number1);
    
        clearBuff();
    
        printf("Operator (+, -, *, /, or ^): ");
        scanf("%c", &operator);
    
        clearBuff();
    
        printf("Number: ");
        scanf("%Lf", &number2);
    
        clearBuff();
    
        switch(operator)
        {
          case '+':
            answer = number1 + number2;
            break;
          case '-':
            answer = number1 - number2;
            break;
          case '*':
            answer = number1 * number2;
            break;
          case '/':
            answer = number1 / number2;
            break;
          case '^':
            answer = pow(number1, number2);
            break;
          default:
            puts("Invalid operator, only +, -, *, /, or ^ are accepted.");
            return 1;
        }
    
        printf("%Lf\n", answer);
      }
    }
    However, I don't think something like this is very useful at all without being able to press the up arrow and cycle through previous answers so that larger values don't have to be re-typed.

    It's possible to use the readline library as a history aspect for what you enter, but i was wondering if it was possible to pass my "answer" (which might have to be a pointer or a node in a linked list instead of a variable), but it doesn't do anything, gcc gives no errors but pressing the up button just generates ansi jibberish:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<clearBuff.h>
    #include<readline/readline.h>
    #include<readline/history.h>
    
    int main()
    {
      while(1)
      {
        long double *input;
        char operator;
        long double number1, number2, answer;
    
        printf("Number: ");
        scanf("%Lf", &number1);
    
        clearBuff();
    
        printf("Operator (+, -, *, /, or ^): ");
        scanf("%c", &operator);
    
        clearBuff();
    
        printf("Number: ");
        scanf("%Lf", &number2);
    
        clearBuff();
    
        switch(operator)
        {
          case '+':
            answer = number1 + number2;
            break;
          case '-':
            answer = number1 - number2;
            break;
          case '*':
            answer = number1 * number2;
            break;
          case '/':
            answer = number1 / number2;
            break;
          case '^':
            answer = pow(number1, number2);
            break;
          default:
            puts("Invalid operator, only +, -, *, /, or ^ are accepted.");
            return 1;
        }
    
        printf("%Lf\n", answer);
    
        using_history();
        input = readline(&answer);
    
        if (*input)
          add_history(input);
      }
    }
    i was also wondering if there were any good alternatives to the readline library, as it probably wasn't designed to do this.
    Last edited by C_me_run; 11-18-2023 at 07:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with math equation.
    By syahidfz in forum C Programming
    Replies: 6
    Last Post: 02-04-2015, 05:25 AM
  2. Math equation help!!
    By alex33zebras in forum C Programming
    Replies: 2
    Last Post: 09-27-2011, 03:31 PM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. A math equation
    By dizolve in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-17-2002, 12:56 PM
  5. why different answers for this equation
    By james_nkh in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 12:29 PM

Tags for this Thread