Thread: WIP: Command line calculator. New to C

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    WIP: Command line calculator. New to C

    GitHub - e14tech/calculator

    I've spent maybe the past week really trying to learn the ins and outs of C. I've downloaded some e-books as well as some online tutorials.

    I'm currently working on removing the redundant 0s when performing a float calculation.

    Pretty much everything in truncate.h I got from stackoverflow, however, I didn't just simply copy and paste it. I typed it all out trying to see what the code is doing. Difficult to read but learning.

    Suggestions are welcomed.

    Eventually I'd like to start learning some Swift. My goals are to create C programs and use Cocoa wrapper written in Swift.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    668
    You need a more thorough knowledge of C before attempting to understand this program.

    Plus this is not a good project to start with.

    Internet "e-books", and "tutorials" (Especially video tutorials) are notoriously bad for learning the language.

    Even a good book on C is not as good as a full introductory course in C given by a qualified instructor.

    Short of a full course in C, for a good book on C I recommend the following:

    C Primer Plus, 6th Edition, Stephen Prata

    C Programming, A Modern Approach, K. N. King

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    1. Remove your global variables, and pass parameters between functions.
    2. Put all your function definitions currently in foo.h into a foo.c file.
    So you should end up with
    Code:
     	main.c 	More descriptive variable names.
    	ops.h 	Changed floats to doubles
    	ops.c 	Implementation of ops.h
    	truncate.h 	Added the rest of the func_printresult() function. 
    	truncate.c 	Implementation of truncate.h
    > for(int it2 = 0; it2 < sizeof(ft2) / sizeof(*"%lf", ft2); ft2++)
    This really doesn't do what you want it to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I haven't pushed a current version yet but I did remove the for loop since reading it through made me realize I didn't need it. It was due to the tutorial I was looking at.

    I was trying to translate the code for use in my program so I was bound to make some mistakes. It was written originally to have an array of values pre-programmed. I had to re-write it to accept values from the mathematical functions.

    I'll check out those books.

    I've also been downloading a bunch of opensource projects like the OpenBSD kernel and MINIX to study the code.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    Just got done commiting and pushing to github.

    Compiles and runs

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    Better.
    Now get rid of the global variables, and pass/return values in your functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    Are you talking about changing the keyword to static?

    I'm lost at your second suggestion. Sorry

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ImageJPEG View Post
    Are you talking about changing the keyword to static?

    I'm lost at your second suggestion. Sorry
    it's called scope (techy talk)

    C Scope Rules

    you could practice your error checking too while you're at it.
    Mishaps if someone does not enter the proper data on your command line, how to handle it to prevent your program from blowing up or returning boas results,or as it is put, undetermined behavior.
    Last edited by userxbw; 09-25-2017 at 05:27 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    I mean doing things like this
    Code:
    double func_pfirst() {
        double firstnum;
        printf("Enter the first number: ");
        scanf("%lf", &firstnum);
        return firstnum;
    }
    Then in main
    Code:
    double n1,n2,sum;
    n1 = func_pfirst();
    n2 = func_psecond();
    sum = func_add(n1,n2);
    func_printresult(sum);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I decided to start a new calculator project. I'm trying to be as verbose as I can in the coding and making understandable variable names.

    freecalc/freecalc at master * e14tech/freecalc * GitHub
    Last edited by ImageJPEG; 09-26-2017 at 08:32 PM.

  11. #11
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I *think* my issue right now is functions and pointers.

    If you look at my code, you guys may know what I'm talking about?

    I'm trying to move numbers around different functions while having variables local to each function. That requires pointers, right?

    If that still is the case, I'm still trying to wrap my head around pointers and how to apply them.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    The only pointers you need are when you call scanf.
    Everything else can be regular scalar parameters and return results.

    Oh, and replace your goto with a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I figured someone would say something about that goto lol. I've read on how that frowned upon.

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    Note that if a user does not enter a numeric string, scanf() will fail and not advance it's internal pointer or take further input, resulting in an infinite loop. You could check to see if scanf() returned a 1 to indicate it successfully input a value, and if it didn't, then use scanf("%*[^\n]"); to advance to just before a new line character to skip past the bad input. An alternative would be to use fgets() which inputs a line of text, then sscanf() to convert the input string to a value.

  15. #15
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    That is a future plan. I'm just trying to get my modularized code working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-15-2017, 07:24 AM
  2. Command-line Calculator Error:
    By mmario10 in forum C Programming
    Replies: 2
    Last Post: 10-31-2011, 04:29 AM
  3. Problems building a "command-line" calculator
    By mmario10 in forum C Programming
    Replies: 6
    Last Post: 10-31-2011, 04:27 AM
  4. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  5. Command line
    By Dr Saucie in forum C Programming
    Replies: 6
    Last Post: 02-23-2010, 02:23 PM

Tags for this Thread