How to Create a Simple Menu-Driven Calculator in C?

  • Thread starter Thread starter leroyjenkens
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion centers on creating a menu-driven program in C that allows users to input five numbers and perform operations like finding the smallest, largest, sum, or average. The initial code provided has several issues, including missing function prototypes for "smallest," "largest," and others, which are necessary for compilation. It is suggested to focus on getting the program to compile first, possibly by using dummy functions to test the overall structure before implementing the complete logic for calculations. A critical error noted is the incorrect use of pointers in the `scanf` function within `getData()`, where the ampersands should not be used since the parameters are already pointers. Additionally, the program currently lacks output functionality, as it does not print the results of the calculations. Debugging tips include adding print statements to track function calls and ensuring that the code matches the comments. If the code is too flawed, starting over with a more incremental approach is recommended.
leroyjenkens
Messages
615
Reaction score
49
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:
#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;
}
 
Technology news on Phys.org
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:
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!
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top