Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calculator-like program

  1. Jun 8, 2012 #1
    I need to write a menu driven program that allows a user to enter five numbers and then choose between finding the smallest, largest, sum, or average.

    So far I have this, which still gives me lots of errors, and I'm not even sure if I can fix it.

    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>

    void getData (float*a, float*b, float*c);
    float calc(int option, float a, float b, float c);
    int main (void)
    {
        int option;

        do
        {
            option = getOption();
            if (option != 5)
                getData (&a, &b ,&c);
            calc (option, a, b, c);
        }
        while();
        return 0;
    }
    int getOption (void)
    {
        printf("Menu\n1- Smallest\n2- Largest\n3- Sum\n4- Avg");
        scanf("%d", &option);
        return option;
    }
    float calc (int option, float a, float b, float c)
    {
        float result;
        switch (option)
        {
            case 1: result = smallest (a, b, c);
                break;
            case 2: result = largest (a, b, c);
                break;
            case 3: result = Sum (a, b, c);
                break;
            case 4: result = Avg (a, b, c);
                break;
        }
        result = 1.0;
        return result;
    }
    void getData (float*a, float*b, float*c)
    {   printf("Enter 3 numbers: ");
        scanf("%f %f %f", &a, &b, &c);
        return 0;
    }
    float smallest (float a, float b, float c)
    {
        if (a<b && a<c)
            return a;
        else if (b<a && b<c)
            return b;
    }
     
     
  2. jcsd
  3. Jun 8, 2012 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    It's obviously not complete, but it doesn't look too bad so far.

    The first goal is getting it to compile, because you can't test anything before that.

    You have function prototypes for getdata() and calc() at the top of the file. You need a prototype for every function before you call it. So add prototypes for smallest(), largest(), etc.

    If you get stuck trying to figure out how to write a routine lke "smallest", sometimes it's a good idea to leave the details till later and try to put the whole program together. You can start with just a "dummy" routine like
    Code (Text):

    float smallest (float a, float b, float c)
    {
            printf("smallest called\n");
            return a;
    }
     
    Of course the value it returns will usually be wrong, but you can use that routine to check the complete program is working properly. The "Sum" and "Average" routines are probably easier so you can write the proper versions of those, then come back to "smallest" and "largest" later when at least something works (and getting something working often is a good motivator to keep going).

    You made one error that will probably crash the program, and the compiler won't pick it up for you. Apparently you knw that the arguments to scanf() should be pointers to the variables. But in your GetData() routine, variable a b and c are already pointers to floats, so you don't need the "&"s when you call scanf.

    Another problem is that so far you aren't producing any output! You need to print the value returned from calc().

    For debugging, it can be useful to put calls to printf() in the routines, like I did in smallest(), so you can see what routines were actually called, if some of your logic is wrong.

    Good luck!
     
  4. Jun 8, 2012 #3
    We usually don't work on homework around here.

    By the way, don't get offended, but I smell something wrong, here...your comments and the code don't match up...is this really your code? You comments show ignorance, the code looks well structured, actually, as written by somebody who has some idea of how to go about it... unfinished, sure, but looking good.

    If it is your code and it is broken beyond repair...start over! This time, type a little bit of code, compile and make sure everything is still error free etc.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook