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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:07:26+00:00 2026-06-11T08:07:26+00:00

I have a game where the user needs to enter x and y coordinates

  • 0

I have a game where the user needs to enter x and y coordinates within a certain range, one at a time. Right now my input code is as follows:

do {
    printf("\n");
    cout << "X: ";
    cin >> x;
    cout << "Y: ";
    cin >> y;
} while (cin.fail());

I’m new to c++ but reading documentation lead me to believe this was an acceptable method for verifying user input. It works perfectly when the input is valid, however when the input is of a different type (for instance entering “a”) it infinitely loops with “X: Y: “. What do I need to do differently to have it wait for user input as if the lines were being read for the first time?

  • 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-11T08:07:28+00:00Added an answer on June 11, 2026 at 8:07 am

    The biggest problem with trying to parse per variable is that when a user makes a mistake, their whole input becomes suspect.

    Consider if I skip invalid input and enter: “3e4 40”. I meant “34 40”, but skipping the e might make “3 4” and leave “40” for later or might leave “4 40”.

    Consider using getline to read a whole line in and parsing it such as with istringstream just as you are now — any error becomes an error without leaving the inputstream in a weird state.

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

Sidebar

Related Questions

I'm currently creating a game (Gomoku) where user needs to input his moves to
I currently have some code which needs to perform multiple updates per user for
I have a game where user controls a character (with his finger) and i
I have created a game where the user can upload their high score and
I am making a game, and have a Game model and a User model.
Imagine that HTML page is a game surface (see picture). User can have n
I am trying to code a global lookup table of sorts. I have game
I have a flash application (a game) and it needs to pass data to
I have tried various setups with input and my one second timer but nothing
Let’s say that I have two C# applications - game.exe (XNA, needs to support

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.