TransWikia.com

Prevent infinite recursion in c++

Stack Overflow Asked by João Menicucci on March 2, 2021

So, I’m triying to learn c++ (coming from python), and I wanted to make a program just to see if i could do it with what i’ve learned, here’s the code

#include <iostream>
using namespace std;

int response(string i) {
    if (i == "yes" or i == "Yes") {
        cout << "nHello, sad, I'm dadn";
        return(0);
    }
    else if (i == "no" or i == "No") {
        cout << "Good for you paln";
        return(0);
    }
    else {
        cout << "Answer properly you overgrown flatwormn";
        response(i);
    };
};

int main() { 
    string i;
    cout << "Are you sad?";
    cin >> i;
    response(i);
};

Pretty simple huh? No. For some reason, yes and no answers work fine, but when I try something different I get insulted infinitely and the program crashes from exceeding it’s memory limit. How do I solve this?
(English is not my native language, so feel free to correct any ortography mistakes)

4 Answers

This can be solved by eliminating recursion ad it involves moving the input routine inside of a function that's more self-contained:

int getResponse(string i) {
  for(;;) {
    string i;
    cout << "Are you sad?";
    cin >> i;

    if (i == "yes" or i == "Yes") {
        cout << "nHello, sad, I'm dadn";
        return(0);
    }
    else if (i == "no" or i == "No") {
        cout << "Good for you paln";
        return(0);
    }
    else {
        cout << "Answer properly you overgrown flatwormn";
    }
  }
}

Correct answer by tadman on March 2, 2021

If you insist on using recursion then move the input and the check in the same function response() - that function doesn't need to return int at all. In main you can just call response().

void response()
{
    string i;
    cout << "Are you sad?";
    cin >> i;
    if (i == "yes" or i == "Yes")
    {
        cout << "nHello, sad, I'm dadn";
    }
    else if (i == "no" or i == "No")
    {
        cout << "Good for you paln";
        return;
    }
    else
    {
        cout << "Answer properly you overgrown flatwormn";
        response();
    }
}

int main()
{
    response();
}

Answered by artm on March 2, 2021

You have 2 issues:

In the else case, you are not asking for new user input.

You need to return the result of calling response(i), otherwise the code invokes undefined behavior.

else {
    cout << "Answer properly you overgrown flatwormn";
    cin >> i;   
    return response(i);
};

Alternatively, since you never use the return value from response, you can just remove all the return statements, and make it a void function.

Answered by cigien on March 2, 2021

At no point do you request further input. For bad input 'i', the response routine prints out an insult, and then calls itself with exactly the same string.

The response routine prints out an insult, and then calls itself with exactly the same string.

The response routine prints out an insult, and then calls itself with exactly the same string.

You need to allow the user to enter a new string, and then (if you want to use recursion) make the recursive call to validate the new input.

But as mentioned in the comment, this is not really a problem that needs a recursive solution.

Answered by J.Backus on March 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP