Hey guys, ive just started learnng c++ after a year or so of java, one issue that has just cropped up for me is repeating input from console.

if for example i ask for an input, expecting just 1, and the user inputs 2 with a space inbetween how do i get it to ignore the second part?

e.g.#
"Guess the number im thinking of."
-> 4 5
"4 is the wrong number"
"Guess again"
"5 is the wrong number"

for(;;)
{
    cin >> value;
     if(value > 0 && value < 1000)
     {
         break;
     }
     else
     {
         cout << "Invalid entry. Please try again.\n";
     }
}

If someone inputs an int, this will work fine, looping until a number between 1 and 999 is put in.... if you input a character e.g. K. it will spam "Invalid entry. Please try again"

I was hoping there would be a way to cancel all incoming consol inputs which would, as im asuming that when someone types in a character it in someway spams the console with commands.

I'm not sure exactly what to tell you about your 4 5 issue, but your second question is a pretty common one.

What you are experiencing is called "stream corruption". It happens when the data type of the input is not compatible with the data type of the variable expecting the input. Look up the functions ios::good(), istream::ignore(), and ios::clear().

I believe the my "4 5" issue can be resolved with ignore, which if i understand correctly...

In the example below it will accept the first 256 characters and ignore everything after that OR will ignore anything after ' ', i could do something similar to stop more than one number input doing the same thing... ill give it a go and see what happens.

Thanks for the help!

Example code from http://www.cplusplus.com/reference/iostream/istream/ignore/

// istream ignore
#include <iostream>
using namespace std;

int main () {
  char first, last;

  cout << "Enter your first and last names: ";

  first=cin.get();
  cin.ignore(256,' ');

  last=cin.get();

  cout << "Your initials are " << first << last;

  return 0;
}

In C++ the input is stored in a buffer of characters until the enter key is pushed. When the enter key is pushed the various input methods of cin are called to remove the information in the buffer until the buffer is empty or until the input stream, in this case cin, is corrupted.

In this case the input of "4 5" actually results in 4 characters being entered into the input buffer. Those char are 4, space, 5 and new line char. The first call to >> removes the 4 but leaves the space, the 5 and the new line char in the input buffer. The body of the loop completes and the loop repeats with the second call to >> removing the space and the 5. At that point input into the int variable is halted until additional data is entered because there is nothing beyond the terminating new line char left in the buffer. It happens this way because >> removes and ignores leading whitespace characters (spaces, tabs, newlines, etc) then puts everything it can into the indicated variable (unless the char in the input buffer is incompatible with the variable it is trying to put it into, in which case one of the state flags of the stream is flipped and the input stream is considered "corrupt" or in a "failed" state.) Input into the desired variable is safely terminated by the first whitespace char identified after the nonwhitespace char that triggered initiation of input into the variable. The terminating whitespace char is left in the input buffer when >> is used.

Thanks Lerner good to know more information on the workings of it all!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.