Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all - for a school project I have to make a calculator which does the basic + - * / % operations and, if the user does not enter "x" (x stopps the program and prints out the result) the calculator should function like any other, so, input for example 5+3, prints out the result, and then the user can do other operation on that, for example -1, then the result is printed, and so on, until the user hits X.

there are of course other requirements, like if there is division with rest, user is prompted to choose whether to round up the result or end the calculation... and something like "division with 0 is not allowed"

current "skill" status - we've done switch/case, loops and functions.

i've been playing around with do-while loop but i cannot make anything happen; so far i only have the basic switch-case thingy and i know, the whole thing looks terrifying. any help would be greatly appreciated!

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

int calculate(int num1, char op, int num2)
{
    int gueltig = 0; //valid operation
    int ungueltig = 0; // operation is not valid
    int answer = 0;
    int rest = 0; // rest from int division
    switch (op)
    {
        case '+':
            answer = num1 + num2;
            ++gueltig;
            printf("Ergebnis: %d.\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig); // Result: XYZ. Program is completed. There were X valid operations and Y non valid operations.
            break;
        case '-':
            if (num1 > num2) {
                answer = num1 - num2;
                ++gueltig;
                printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
            } else {
                printf("Operation ist ungueltig!\nErgebnis darf nicht negativ sein!\nErgebnis: %d", num1);
            }
            break;
        case '*':
            answer = num1 * num2;
            ++gueltig;
            printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
            break;
        case '/':
            if (num2 == 0) {
                ++ungueltig;
                printf("Operation ist ungueltig!\nKeine Divisionen durch 0!\nErgebnis: %d", num1);
            } else if (num2 != 0) {
                if (num1 % num2 == 0) {
                    answer = num1 / num2;
                    ++gueltig;
                    printf("Ergebnis: %d\nProgram wird beendet. Es wurden %d gueltige Operationen und %d ungueltige Operationen durchgefuehrt.", answer, gueltig, ungueltig);
                } else if (num1 % num2 !=0) {
                    answer = num1 / num2;
                    rest = num1 % num2;
                    printf("Operation ist ungueltig! Ergebnis ist %d mit %d Rest. Soll gerundet(r), abgeschnitten(c) oder verworfen(x) werden?", answer, rest);
                }
            }
    }

}

int main()
{
    int num1, num2;
    char op;

    do
    {
        scanf("%d %c %d", &num1, &op, &num2);

        // call the calculator and display the result
        int result = calculate(num1, op, num2);
    } while ( op != 'x'); // repeat until user types 0 0 0 */
Posted
Updated 16-Oct-20 0:28am
v2
Comments
Richard MacCutchan 16-Oct-20 6:40am    
What is the problem? do you get any output, is it correct, if not what is wrong with it? Please provide explicit details.
Member Ana 16-Oct-20 6:47am    
My apologies - the switch - case functions with single operations with two numbers does work, so for example user enters number 1, then some operator, then number 2, clicks Enter and result is displayed and that is it.

What i need it to do is to provide options for further calculations, until the user inputs letter X (done with calculations), so it should look like 3+5 enter, displayed is "Result: 8" and then the user can, lets say, input -2, hits Enter, output is "Result is 6", and so on, until the user inputs "x", and the program ends with the last result that was calculated.
Richard MacCutchan 16-Oct-20 6:54am    
That should not be too difficult. You first need to return the answer from the calculate function. Your main function needs to save this answer. It can then request the next operator and number from the user. So the next time it calls calculate it should pass the saved answer in as num1, and the new value as num2. You should also add messages in main to tell the user what to enter each time.
Member Ana 16-Oct-20 7:10am    
Thank you, Richard, I'll try to do it in accordance with your tips and will feel free to ask additional questions should they arise
Richard MacCutchan 16-Oct-20 7:39am    
In the meantime take a look at this article by OriginalGriff: How to Write Code to Solve a Problem, A Beginner's Guide[^]. It contains a few simple hints to help you think about the structure of your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900