-1

When I run the code it stops after entering the second number. I'm pretty sure I have a logic error somewhere but I can't seem to find it. Also this is my first try using functions, maybe I did something wrong there?

This is my code:

#include <iostream>

using namespace std;

/*Variables*/
double d_Zahl1 = 0.0;
double d_Zahl2 = 0.0;
double d_Ergebnis = 0.0;
char c_Operator = ' ';

/*Functions*/
double add(double d_Zahl1, double d_Zahl2)
{
    d_Ergebnis = d_Zahl1 + d_Zahl2;
    return d_Ergebnis;
    /*Output of result*/
    cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};

double substract(double d_Zahl1, double d_Zahl2)
{
    d_Ergebnis = d_Zahl1 - d_Zahl2;
    return d_Ergebnis;
    /*Output of result*/
    cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};

double divide(double d_Zahl1, double d_Zahl2)
{
    d_Ergebnis = d_Zahl1 / d_Zahl2;
    return d_Ergebnis;
    /*Output of result*/
    cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};

double multiply(double d_Zahl1, double d_Zahl2)
{
    d_Ergebnis = d_Zahl1 / d_Zahl2;
    return d_Ergebnis;
    /*Output of result*/
    cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};


/*Main function*/
int main()
{
    /*Output of head*/
    cout << "\t----------Calculator----------\n\n";



    /*Input of 1. number*/
    /*checking if the input is a number*/
    while (cout << "\n\nPlease enter your first number: " && !(cin >> d_Zahl1))
    {
        cout << "\nThat's not a valid input. Try again. " << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    /*Einlesen des Operanden*/
    cout << "\nPlease enter your operator (+,-,*,/): ";

    do {                                //Fehler durch do-while Schleife abfangen

        cin >> c_Operator;

        switch (c_Operator) {

        case '+':
            /*Eingabeaufforderung für zweite Zahl*/
            cout << "\nPlease enter your second number: ";

            /*Einlesen der 2. Zahl*/
            cin >> d_Zahl2;

            add(d_Zahl1, d_Zahl2);
            break;
        case '-':
            /*Eingabeaufforderung für zweite Zahl*/
            cout << "Please enter your second number: ";

            /*Einlesen der 2. Zahl*/
            cin >> d_Zahl2;

            substract(d_Zahl1, d_Zahl2);
            break;
        case '*':
            /*Eingabeaufforderung für zweite Zahl*/
            cout << "Please enter your second number: ";

            /*Einlesen der 2. Zahl*/
            cin >> d_Zahl2;

            multiply(d_Zahl1, d_Zahl2);
            break;
        case '/':
            /*Eingabeaufforderung für zweite Zahl*/
            cout << "Please enter your second number: ";

            /*Einlesen der 2. Zahl*/
            cin >> d_Zahl2;

            divide(d_Zahl1, d_Zahl2);
            break;
        default:
            cout << "-----Wrong input!-----\n\n\n\n\n\n\n\n\nPlease enter your operator (+,-,*,/): ";
        }
    } while (c_Operator != '+' || c_Operator != '-' || c_Operator != '*' || c_Operator != '/'); /*Solange keines der Rechenzeichen --> repeat*/



    system("pause");
    return 0;
}

I wanted it to go like:

Enter 1. Number: 5

Enter Operator: +

Enter 2. Number: 5

5 + 5 = 10

but right now its just:

Enter 1. Number: 5

Enter Operator: +

Enter 2. Number: 5

halfer
  • 19,824
  • 17
  • 99
  • 186
  • In your functions, like `add` you return before outputting anything – Michael Veksler Feb 12 '19 at 08:28
  • The output should either be in `main`, or in the functions *before* they return. (Doesn't your compiler warn you about having code after `return`?) – molbdnilo Feb 12 '19 at 08:28
  • @molbdnilo no it doesnt ^^ but I just found out that I used || instead of && and thanks for the fast reply! :) – Jeremias T. Feb 12 '19 at 08:41
  • 1
    You don't need all this code to demonstrate your problem. Always try to paste as small a program as possible, i.e. what is called an [mcve](https://stackoverflow.com/help/mcve) – default Feb 12 '19 at 09:06

2 Answers2

2

@asaf92 already mentioned your error, but I would like to give some hints on what can be done better, here in your code. There also some flaws.

first of all using namespace std; is not good practice. You can read here why.

A function doesn't need a semicolon at the end. Also your function just produces output and therefore is in no need to return a number. Since you made d_Zahl1 and d_Zahl2 global there's no need to pass them as Arguments.

so

double add(double d_Zahl1, double d_Zahl2){...};

becomes

void add(){...}

You're writing:

cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;

4 times. you can put that into a function:

void print_result(double d_Ergebnis) {
    std::cout << "\n\n\n" << d_Zahl1 << ' ' << c_Operator << ' ' << d_Zahl2 << " = " << d_Ergebnis << '\n';
}

(You missed a '\n' at the end, too).

do {...} while (c_Operator != '+' || c_Operator != '-' || c_Operator != '*' || c_Operator != '/');

is quite ugly. instead go with:

bool done = false
while (!done){
    done = true
    switch (){
        ...
        default: done = false
    }
}

system("pause") is probably the most evil here. avoid at all cost. You can read here why. Just keep in mind that it adds a ton of overhead loading all the windows(!) instructions. Instead, use something like:

void system_pause() {
        std::cout << "press enter to continue . . . ";
        if (!std::cin.good()) {
            std::cin.clear();
        }
        else {
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

and than call system_pause() when you need it. Some more things:

  • Your multiply function divides instead of multiplying.

  • You're repeating yourself a lot with "Input second number" etc. - You can put that into functions, too.

  • You need to #include <limits> when you're using it.

  • Try to avoid std::endl - always go with '\n' (Read here why).

full code:

#include <iostream>
#include <limits>

double d_Zahl1 = 0.0;
double d_Zahl2 = 0.0;
double d_Ergebnis = 0.0;
char c_Operator = ' ';

void system_pause() {
    std::cout << "Press enter to continue . . . ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


void print_result(double d_Ergebnis) {
    std::cout << "\n\n\n" << d_Zahl1 << ' ' << c_Operator << ' ' << d_Zahl2 << " = " << d_Ergebnis << '\n';
}



void add(){
    d_Ergebnis = d_Zahl1 + d_Zahl2;
    print_result(d_Ergebnis);
}

void substract(){
    d_Ergebnis = d_Zahl1 - d_Zahl2;
    print_result(d_Ergebnis);
}

void divide(){
    d_Ergebnis = d_Zahl1 / d_Zahl2;
    print_result(d_Ergebnis);
}

void multiply(){
    d_Ergebnis = d_Zahl1 * d_Zahl2;
    print_result(d_Ergebnis);
}



void input_first_number() {
    while (std::cout << "Please enter your first number: " && !(std::cin >> d_Zahl1)) {
        std::cout << "That's not a valid input. Try again.\n";
        system_pause();
        std::cout << '\n';
    }
}
void input_second_number() {
    while (std::cout << "Please enter your second number: " && !(std::cin >> d_Zahl2)) {
        std::cout << "That's not a valid input. Try again.\n";
        system_pause();
        std::cout << '\n';
    }
}


int main(){
    std::cout << "\t----------Calculator----------\n\n";

    input_first_number();

    std::cout << "Please enter your operator (+,-,*,/): ";

    bool done = false;
    while (!done){

        std::cin >> c_Operator;

        done = true;
        switch (c_Operator) {
        case '+':
            input_second_number();
            add();
            break;
        case '-':
            input_second_number();
            substract();
            break;
        case '*':
            input_second_number();
            multiply();
            break;
        case '/':
            input_second_number();
            divide();
            break;
        default:
            std::cout << "-----Wrong input!-----\n\nPlease enter your operator (+,-,*,/): ";
            done = false; //repeat if wrong input
        }
    }

    system_pause();
    return 0;
}

example run:

        ----------Calculator----------

Please enter your first number: 2938
Please enter your operator (+,-,*,/): /
Please enter your second number: 193



2938 / 193 = 15.2228
Press enter to continue . . .

with wrong input:

        ----------Calculator----------

Please enter your first number: a
That's not a valid input. Try again.
Press enter to continue . . .

Please enter your first number: 123
Please enter your operator (+,-,*,/): a
-----Wrong input!-----

Please enter your operator (+,-,*,/): *
Please enter your second number: 678



123 * 678 = 83394
Press enter to continue . . .
asaf92
  • 1,557
  • 1
  • 19
  • 30
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thanks a lot :)! This is very helpfull, since im still at the very beginning im very happy about advice like this. I'll study your Code and try to implement it into mine as well so I'll learn it. Thank you! – Jeremias T. Feb 12 '19 at 09:32
  • 1
    It's kind of embarrassing to do such stupid mistakes ^^' but im very thankfull for all your advice and improvements! – Jeremias T. Feb 12 '19 at 09:36
  • I am glad I could help! =) – Stack Danny Feb 12 '19 at 09:43
1
d_Ergebnis = d_Zahl1 + d_Zahl2;
return d_Ergebnis;
/*Output of result*/

This is your problem. Everything under "return" does not execute. 'return' statements end the function.

asaf92
  • 1,557
  • 1
  • 19
  • 30