João Menicucci
João Menicucci

Reputation: 13

Prevent infinite recursion in c++

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 dad\n";
        return(0);
    }
    else if (i == "no" or i == "No") {
        cout << "Good for you pal\n";
        return(0);
    }
    else {
        cout << "Answer properly you overgrown flatworm\n";
        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)

Upvotes: 1

Views: 395

Answers (4)

tdao
tdao

Reputation: 17668

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 dad\n";
    }
    else if (i == "no" or i == "No")
    {
        cout << "Good for you pal\n";
        return;
    }
    else
    {
        cout << "Answer properly you overgrown flatworm\n";
        response();
    }
}

int main()
{
    response();
}

Upvotes: 0

cigien
cigien

Reputation: 60218

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 flatworm\n";
    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.

Upvotes: 1

tadman
tadman

Reputation: 211570

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 dad\n";
        return(0);
    }
    else if (i == "no" or i == "No") {
        cout << "Good for you pal\n";
        return(0);
    }
    else {
        cout << "Answer properly you overgrown flatworm\n";
    }
  }
}

Upvotes: 2

J.Backus
J.Backus

Reputation: 1441

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.

Upvotes: 4

Related Questions