Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:26:45+00:00 2026-05-26T02:26:45+00:00

Im trying to add code so if the user mistypes they can go back

  • 0

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 ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:26:45+00:00Added an answer on May 26, 2026 at 2:26 am

    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'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add functionality in my program that will allow the user to
I am trying to add some extra WizardPane depends on user's choice, but unfortunatelly
I'm trying to add jquery-ui to a Greasemonkey script. my full code: test.user.js :
we are trying to add ColumnDefinition to a Grid in WPF dynamically from code
I'm trying to add items to a listbox,combobox, radiolist using reflection. The code I
I have a plot (sample code pasted below) that I am trying to add
I am trying to write simple Visual Studio Add-In for code generation. In my
I'm trying to programmatically add dots to a radar screen. The code runs without
I'm trying to find a way to automate some exception logging code to add
I have been trying to learn how to add testing to existing code --

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.