d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Returning To The Beginning Of A Function? > Iso Answer. :)
12Next
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 9 2014 06:09pm
Hey guys. I have a program set up where the operator is asked to choose a choice of 1 or 2. If any other input is entered and error will return announcing an incorrect input. What can I do to have the function return to the beginning so the operator may input another answer?

Code
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 if(number != 1 && number != 2){
cout << "Input error."; // If input is not 1 or 2.
return;}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2014 06:17pm
loop
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 9 2014 06:19pm
Code
do
{
//do shit
}while(shitsNotDoneCorrectly);
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
May 9 2014 06:26pm
Quote (NinjaSushi2 @ May 9 2014 06:09pm)
Hey guys. I have a program set up where the operator is asked to choose a choice of 1 or 2. If any other input is entered and error will return announcing an incorrect input. What can I do to have the function return to the beginning so the operator may input another answer?

Code
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 if(number != 1 && number != 2){
        cout << "Input error."; // If input is not 1 or 2.
  return;}
}


You could do something like this..


Code
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 if(number != 1 && number != 2){
cout << "Input error try again: " << endl; // If input is not 1 or 2.
cin >> number;
writeProverb(number);
}
}


Member
Posts: 29,614
Joined: Sep 23 2008
Gold: 0.00
May 9 2014 06:33pm
Quote (Rejection @ May 9 2014 07:26pm)
You could do something like this..


Code
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 if(number != 1 && number != 2){
        cout << "Input error try again: " << endl; // If input is not 1 or 2.
        cin >> number;
        writeProverb(number);
        }
}


Holy mother of recursion!

I'd rather use what Minko suggested.. It's cleaner, simpler and less likely to mess up..

Code
do
{
cout<<"Enter a number (0 to stop): ";
cin>>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 if(number != 1 && number != 2){
cout << "Input error.";
}while(number!=0);


This post was edited by Ashwin on May 9 2014 06:33pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 9 2014 06:58pm
Quote (Minkomonster @ 9 May 2014 19:19)
Code
do
{
//do shit
}while(shitsNotDoneCorrectly);


lol
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 9 2014 07:04pm
Quote (Rejection @ May 9 2014 07:26pm)
You could do something like this..


Code
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 if(number != 1 && number != 2){
        cout << "Input error try again: " << endl; // If input is not 1 or 2.
        cin >> number;
        writeProverb(number);
        }
}


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.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 9 2014 07:20pm
Obviously I am doing something wrong.

Code
void writeProverb(int number)
{
do{
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.";} // If input is not 1 or 2.
} while (number != 1 && number != 2);
}


It is not set in an infinite loop. If I comment out the trailing else if statement I sit at the prompt screen with it blank. Can someone give me a guiding hand?

Entire code: If anyone requires comments for the code let me know.

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)
{
do{
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.";} // If input is not 1 or 2.
} while (number != 1 && number != 2);
}


This post was edited by NinjaSushi2 on May 9 2014 07:20pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2014 07:23pm
weren't you the one who posted this same sort of problem before?

} while (number != 1 && number != 2)

tell me, with what values of number would that statement be true? make a truth table if you want:

boolean a = number != 1;
boolean b = number != 2;
boolean result = a && b;

number, a, b, result
0, ?, ?, ?
1, ?, ?, ?
2, ?, ?, ?


/edit: yup. see your original topic: http://forums.d2jsp.org/topic.php?t=70358617&f=122

This post was edited by carteblanche on May 9 2014 07:25pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
May 9 2014 11:53pm
Yeah that isn't my problem. I want to make my function restart if someone inputs any other value than 1 or 2.

This post was edited by NinjaSushi2 on May 9 2014 11:55pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll