Thread: Ignoring data after valid entered

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Ignoring data after valid entered

    I have searched for this and could not find it. It is not in my textbook either.

    Okay my prompt looks this... say the user enters "1 4 junk data"
    i.e.
    Enter two numbers: 1 4 junk data

    How do I get those 2 numbers and ignore "junk data"? Thank you for your help.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. One is to keep track of the total number of spaces " " At space equals 2 or greater, stop processing cin.

    Kuphryn

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>How do I get those 2 numbers and ignore "junk data"?
    Just read them, as long as the two numbers come before the junk and the junk doesn't count as a number or there's whitespace between the second number and the junk then it'll work fine.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      int a, b;
    
      cout<<"Enter two numbers: "<<flush;
      cin>> a >> b;
    
      cout<<"You entered -- "<< a <<" -- "<< b <<endl;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. Is long long int a valid Data Type
    By shiv_tech_quest in forum C Programming
    Replies: 2
    Last Post: 11-12-2003, 08:59 AM