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 7938379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:47:55+00:00 2026-06-03T22:47:55+00:00

Practicing some C++, I ran into some code from a book. The usage of

  • 0

Practicing some C++, I ran into some code from a book.
The usage of the if statement with the break seams a bit unecessary to me:

if (!(cin >> dstep))
break;

This seams a bit hackish to me and simply using a cin >> dstep with no if or break runs the program fine. Any thoughts?

                    int main()
                {
                    using namespace VECTOR;

                    srand(time(0));     //seed random-number generator
                    double direction;
                    Vector step;        //creates default object
                    Vector result(0.0, 0.0);    //
                    unsigned long steps = 0;
                    double target;
                    double dstep;

                    cout << "Enter target distance (q to quit): ";

                    while (cin >> target)
                    {
                        cout << "Enter step length: ";
                        if (!(cin >> dstep))    //if NOT inputing into dstep THEN break/means if INPUTING is TRUE, keep going and don't break out of loop
                            break; 
                      //cin >> dstep  // why not just use this?
                        while (result.magval() < target)
                        {
                            direction = rand() % 360;
                            step.set(dstep, direction, 'p'); //sets the values dstep and direction based on the form; in this case 'p'
                            result = result + step;
                            steps++;
                        }
                        cout << "After " << steps << " steps, the subject "
                            "has the following location:\n";
                        cout << result << endl;
                        result.polar_mode();
                        cout << " or\n" << result << endl;
                        cout << "Average outward distance per step = "
                            << result.magval() / steps << endl;
                        steps = 0;
                        result.set(0.0, 0.0);
                        cout << "Enter target distance (q to quit): ";
                    }
                    cout << "Bye!\n";

                    cin.get();
                    return 0;
                }
  • 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-06-03T22:47:57+00:00Added an answer on June 3, 2026 at 10:47 pm

    You have to either enable exceptions or check every stream input operation, otherwise you risk getting into an infinite loop, or just getting wrong results, when the input is malformed.

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

Sidebar

Related Questions

Whilst practicing some TDD at work for an ASP.Net MVC project, I ran into
I'm using g++ on fedora linux 13. I'm just practicing some exercises from my
I've been doing some exercises from a book and I'm wondering if you could
I've been practicing some animation on Android but I could not get my code
I have been practicing TDD and (some) XP for a few years now and
I'm practicing my knowledge of ADTs by implementing some data structures, even if most
While practicing the jxl API, i cant extract details from certain Excel sheet its
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
I'm practicing on Django and using some online tutorial to build a web blog.
I'm practicing some file upload with PHP and I was uploading a file numerous

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.