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

  • Thread starter Thread starter leroyjenkens
  • Start date Start date
  • Tags Tags
    Program
Click For Summary
SUMMARY

This discussion focuses on creating a menu-driven calculator in C that allows users to input three numbers and perform operations such as finding the smallest, largest, sum, or average. Key issues identified include missing function prototypes for 'smallest', 'largest', and others, as well as incorrect usage of pointers in the 'getData' function. The importance of compiling frequently to catch errors early is emphasized, along with the suggestion to use dummy functions for testing. Debugging techniques, such as using printf statements, are recommended to track function calls and logic flow.

PREREQUISITES
  • C programming fundamentals
  • Understanding of function prototypes in C
  • Knowledge of pointer usage in C
  • Basic debugging techniques in C
NEXT STEPS
  • Learn about function prototypes in C programming
  • Research pointer usage and memory management in C
  • Explore debugging techniques using printf statements in C
  • Study how to implement and test dummy functions in C
USEFUL FOR

Beginner to intermediate C programmers, students working on programming assignments, and anyone looking to improve their debugging skills in C.

leroyjenkens
Messages
621
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 46 ·
2
Replies
46
Views
9K
Replies
6
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K