0
#include <iostream>
#include "funktionen.h"

using namespace std;

int rechnung()
{
    cout  << "Please choose the operator you want to calculate with" << endl;
    int eingabe1;
    int eingabe2;
    int eingabe;
    int dummy;
    char zeichen;
    char again;
    cin >> zeichen;
    cout << endl << "1. Eingabe: ";
    cin >> eingabe1;
    cout << endl << "2. Eingabe: ";
    cin >> eingabe2;
    switch (zeichen)
    {
    case '+':
        eingabe=eingabe1 + eingabe2;
        break;
    case '-':
        eingabe=eingabe1 - eingabe2;
        break;
    case '*':
        eingabe=eingabe1 * eingabe2;
        break;
    case '/':
        eingabe=eingabe1 / eingabe2;
        break;
    }

    cout << endl << "Das Ergebnis ist | " << eingabe << " | " << endl << endl;
    cout << "Wanna calculate again? ";
    cin >> again;
    while(again=='Y')
    {
        rechnung();
    }
    return 0;
}

So this is my code in an implementation file. My problem is, that the main program always loops the whole "rechnung()" function even though I don't type "Y" into the console once it asks for it. In the beginning, when I type something else than "Y", then the console closes (as it shall) but if I do a calculation, type "Y", do another calculation and type "k" for example, it also starts from the beginning of "rechnung()". Why does it do so? I mean I told him that he shall only recall the "rechnung()" if the character input is "Y".

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Felix
  • 56
  • 1
  • 5
  • You never modify `again` inside the loop - the function call creates a new local variable, which has nothing to do with the one from the calling function – UnholySheep Feb 01 '19 at 22:14
  • On a different note, there is absolutely no reason to use recursion here - you should just wrap the entire part you want to repeat inside the `while` instead – UnholySheep Feb 01 '19 at 22:16
  • What do you mean by "repeat it inside" So I shall just write all the code, which is above the loop, again into the loop? Where is the problem of just calling the function once again? – Felix Feb 01 '19 at 22:19
  • I already explained what the problem is you are having with calling it recursively. And you shouldn't copy the code, you should move it inside the loop – UnholySheep Feb 01 '19 at 22:21
  • So you mean I should put again='Y' in the beginning and then wrap everying with the while loop? – Felix Feb 01 '19 at 22:25

1 Answers1

1

Consider this maybe simpler example:

void foo() {}
void bar() {
    char again;
    // ... do something
    std::cin >> again;
    while(again=='Y') {
        foo();
    }
}

Inside the loop the value of again never changes, so there is an infinite loop. However, your code goes one step further by calling the function recursively. Strongly simplified you have:

void bar() {
   // .. do something
   while (true) {
       bar();
   }
}

ie bar is calling itself again and again and never returns. You put the loop at the wrong place. You could write it like this

void bar() {
    char again = 'Y';
    while (again == 'Y') {
        // .. do something
        std::cout << "repeat?";
        std::cin >> again;
    }
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Tyvm, that helped alot. I changed it now and it worked :) It seems very plausible; but I didn't see it in the first place – Felix Feb 01 '19 at 22:57