2

Im trying to add code so if the user mistypes they can go back in the program and retype the input but im not sure if the code i found and used is correct. here it is in my function:

/********************************************/
// Name: inspools                            /
// Description: Ask for and get number of    /
// spools                                    /
// Parameters: N/A                           /
// Reture Value: spoolnum                    /
/********************************************/
int spoolnum()
{
  int spoolnum;
  char type;

  cout << "Number of spools to be shipped: " << endl;
  cin >> spoolnum;
  cout << spoolnum << " spool(s) of wire will be shipped" << endl;
  cout << "Is this correct? [y/n] ";
  cin >> type;
  if ('n') << spoolnum;

  if ('y') break;

  return spoolnum ;
}
7
  • 8
    Hint: Use a loop. Commented Oct 11, 2011 at 0:56
  • i tried looking online for an example of a loop and thats as close as i got Commented Oct 11, 2011 at 0:57
  • what's if ('n') << spoolnum; supposed to do? Commented Oct 11, 2011 at 0:58
  • 3
    Another hint: DO keep at it WHILE your program isn't correct. Commented Oct 11, 2011 at 0:59
  • 1
    WHILE he's at it he should check google, it shouldn't take him no more than a short WHILE to find the answer. Commented Oct 11, 2011 at 1:00

1 Answer 1

2

You said you searched for loops, but I don't buy it. I imagine you are pretty new at programming. I'm going to give you the answer but not without some explanation first.

How While Loops Work

From Wikipedia:

enter image description here

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

Your Problem

Your problem is that you want to keep making the user enter a choice until they enter y. To do this, you need at least a WHILE loop, or as other commenters have said a DO/WHILE loop.

I have never preferred DO/WHILE loops but others do prefer it.

The problems you may have with the below code is that you have more than just y returned in cin such as a newline (\n) character. You will have to handle that condition.

int spoolnum()
{
  int spoolnum = 0;
  char type = 'n';

  while (type != 'y') {
      cout << "Number of spools to be shipped: " << endl;
      cin >> spoolnum;
      cout << spoolnum << " spool(s) of wire will be shipped" << endl;
      cout << "Is this correct? [y/n] ";
      cin >> type;
  }
  return spoolnum;
}

or the alternative DO/WHILE:

int spoolnum()
{
  int spoolnum = 0;
  char type = 'n';

  do {
      cout << "Number of spools to be shipped: " << endl;
      cin >> spoolnum;
      cout << spoolnum << " spool(s) of wire will be shipped" << endl;
      cout << "Is this correct? [y/n] ";
      cin >> type;
  } while (type != 'y');

  return spoolnum;
}

In the above code, I removed your if ('n') << spoolnum; because frankly it does not make sense.

I also removed if ('y') break; because the while(...) loop will break once the condition is met, which is type equal to 'y'.

Sign up to request clarification or add additional context in comments.

4 Comments

You should guide the OP in the right direction... but let them do their own homework assignments.
One day we are going to have to work with this person. I don't want to be explaining do loops to co-workers.
I think this is a perfect scenario for do/while. It prevents you from having to pre-initialize "type" in this example and saves you a line of code.
so this is all i need? how does it know to go back if anything besides y is entered?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.