d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Returning To The Beginning Of A Function? > Iso Answer. :)
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 10 2014 12:14am
Quote (NinjaSushi2 @ May 10 2014 12:53am)
Yeah that isn't my problem. I want to make my function restart if someone inputs any other value than 1 or 2.


youre not recapturing user input...
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 10 2014 07:22am
Quote (NinjaSushi2 @ May 10 2014 01:53am)
Yeah that isn't my problem. I want to make my function restart if someone inputs any other value than 1 or 2.


the fuck was i thinking LOL :wallbash:

gonna go back to trying to find a cute neko avatar :/
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
May 10 2014 06:08pm
Quote (Minkomonster @ May 9 2014 07:04pm)
Incoming stack overflow. And while I admire your "ingenuity," making a recursive call does not satisfy the criteria as "returning to the beginning of the function" as you are instead just starting a DIFFERENT method.


I suppose it doesn't go back to that same function, but where exactly is the stack overflow?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 10 2014 06:27pm
Quote (Rejection @ May 10 2014 07:08pm)
I suppose it doesn't go back to that same function, but where exactly is the stack overflow?


Your solution fails if the user inputs the wrong number nCallStack times. It will keep adding a new frame to the stack over and over until it rejects it with a stack overflow. It's an edgecase, seeing as how they would have to incorrectly enter the number 1000+ times. But Murphy's law mandates that eventually one user will come along dumb enough to trigger that edge case. You don;t ahve that probably with a simple loop.
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
May 11 2014 11:04am
Quote (Minkomonster @ May 10 2014 06:27pm)
Your solution fails if the user inputs the wrong number nCallStack times. It will keep adding a new frame to the stack over and over until it rejects it with a stack overflow. It's an edgecase, seeing as how they would have to incorrectly enter the number 1000+ times. But Murphy's law mandates that eventually one user will come along dumb enough to trigger that edge case. You don;t ahve that probably with a simple loop.


Hmm, I see

So this:

Code
void writeProverb(int number)
{
while(number != 1 && number != 2) {
if(number == 1){
cout << "Now is the time for all good men to come to the aid of their party.";}
else if(number == 2){
cout << "Now is the time for all good men to come to the aid for their country.";}
else if(number != 1 && number != 2){
cout << "Input error. Try again: " << endl; // If input is not 1 or 2.
cin >> number;
}
}
}


Could loop indefinitely, until the CPU uses power, as long as the user keeps entering wrong values?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 11 2014 11:53am
Quote (Rejection @ May 11 2014 01:04pm)
Hmm, I see

So this:

Code
void writeProverb(int number)
{
    while(number != 1 && number != 2) {
        if(number == 1){
      cout << "Now is the time for all good men to come to the aid of their party.";}
        else if(number == 2){
      cout << "Now is the time for all good men to come to the aid for their country.";}
        else if(number != 1 && number != 2){
            cout << "Input error. Try again: " << endl; // If input is not 1 or 2.
            cin >> number;
        }
    }
}


Could loop indefinitely, until the CPU uses power, as long as the user keeps entering wrong values?


i recommend taking an intro course to assembly. when you trace your recursive code, you can see the stack building up vs iteration. it's one of the most interesting courses i ever took in college.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 12 2014 12:59pm
Wow. Good read and info guys thanks. :thumbsup:
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 15 2014 12:43pm
Quick question, shouldn't you use the following:
Quote
cin.clear()
std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');

to clear fail state and throw away irrelevant data waiting in input buffer?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 18 2014 11:15am
Quote (Foxic @ 15 May 2014 13:43)
Quick question, shouldn't you use the following:

to clear fail state and throw away irrelevant data waiting in input buffer?


Got me. :huh:
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
May 20 2014 02:43am

Code
// Lab 6.2
// Exercise 2:

// This program will allow the user to input from the keyboard
// whether the last word to the following proverb should be party or country:
// "Now is the time for all good men to come to the aid of their"
// Inputting a 1 will use the word party.
// Inputting a 2 will use the word country.


#include <iostream>
#include <string>
using namespace std;

void writeProverb(int);

int main()
{

int wordCode;

cout << "Given the phrase:" << endl;
cout << "Now is the time for all good men to come to the aid of their ____\n\n";
cout << "Input a 1 if you want the sentence to be finished with party." << endl;
cout << "Input a 2 for the word country." << endl << endl;
cout << "Any other input will end in an error. Please input your choice now" << endl;
cin >> wordCode;
cout << endl;

writeProverb(wordCode);
return 0;
}

void writeProverb(int number)
{
if(number == 1)
{
cout << "Now is the time for all good men to come to the aid of their party.";
}
else if(number == 2)
{
cout << "Now is the time for all good men to come to the aid for their country.";
}
else
{
tryAgain();
}
}

void tryAgain()
{
int newNumber

cout << "Please enter a valid option"
cin >> newNumber

writeProverb(newNumber);
}




This post was edited by Calamitymic on May 20 2014 02:49am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll