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

  • Thread starter leroyjenkens
  • Start date
  • Tags
    Program
In summary: You simply type in too much code before making sure it all works!In summary, the conversation discusses the need to write a menu driven program that allows a user to enter five numbers and choose between finding the smallest, largest, sum, or average. The code provided is not complete and the main goal is to get it to compile. Suggestions are given, such as adding function prototypes and starting with simpler functions to test the program. It is also mentioned that there may be errors in the code and it is suggested to start over and type smaller portions of code to ensure they work before continuing.
  • #1
leroyjenkens
616
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
  • #2
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!
 
  • #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.
 

What is a calculator-like program?

A calculator-like program is a software application that allows users to perform mathematical calculations using a computer or device. It typically has a user-friendly interface and can handle various types of calculations, such as basic arithmetic, algebraic equations, and scientific functions.

How does a calculator-like program work?

A calculator-like program works by taking input from the user, usually in the form of numbers and mathematical operators, and then processing the input using a set of algorithms. The result is then displayed on the screen for the user to see. It may also have memory functions that allow users to save and recall previously calculated values.

What are the benefits of using a calculator-like program?

Using a calculator-like program can save time and effort when performing calculations, especially for complex or lengthy equations. It can also reduce the chances of human error, as the calculations are done automatically. Furthermore, some calculator programs have additional features such as unit conversions and graphing capabilities, making them useful for a variety of tasks.

Can a calculator-like program be used for advanced mathematics?

Yes, there are calculator-like programs that are specifically designed for advanced mathematics, such as calculus, trigonometry, and statistics. These programs often have more advanced functions and can handle more complex equations compared to basic calculator programs.

Are calculator-like programs available for different devices?

Yes, calculator-like programs are available for various devices, including computers, smartphones, and tablets. They may also come in different forms, such as online calculators or downloadable applications. Some devices may have built-in calculator programs, while others may require users to install a separate program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top