confused with istringstream

This is a discussion on confused with istringstream within the C++ Programming forums, part of the General Programming Boards category; hello im trying to validate a social number with istrigstream my program works great but i'm a little confused why ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    confused with istringstream

    hello im trying to validate a social number with istrigstream
    my program works great but i'm a little confused why it works heres the code:

    confution explained later...

    Code:
    #include <iostream>
    using namespace std;
    
    #include <string>
    using std::string;
    
    #include <sstream>
    using std::istringstream;
    
    bool validate(istringstream &);
    
    int main()
    {
    	string sNumber("111-22-3333");
    
    	istringstream innString(sNumber);
    
    	if(validate(innString))
    		cout << "\nValid social number";
    	else
    		cout << "\nWrong social number";
    	
    	cout << endl;
    	return 0;
    }
    
    bool validate(istringstream &innNumber)
    {
    	int p1;
    	int p2;
    	int p3;
    	char char1;
    	char char2;
    
    	if(innNumber >> p1 >> char1 >> p2 >> char2 >> p3)
    	
    		return true;
    	else 
    		return false;
    }
    if you see the social number is not separated by blank spaces, how the istringstream knows that the first "111" must be in p1
    how knows that '-' must be in char1, etc?

    when the string is separated by blank spaces and you use >>
    it discharges the spaces and thats fine. but here i dont have any
    space, how this works?

    or in other words how knows when it is an int and when is a char? if theres no blank spaces?

    please any help?
    and excuse my poor english

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think the explanation is that the >> terminates at the first char incompatible with the type it is extracting from the input buffer. This is often a whitespace char, but it need not be. Whatever it is, the terminating char is left in the buffer.

    In this case the first call to >> looks for an int in the stream input buffer and extracts all elements of the stream until it finds something that isn't compatable with an int at which time it stops input into the int and leaves the terminating char in the buffer. If the terminating char for the first call to >> is a whitespace char the second call to >> ignores it. However, if the terminating char to the first call to >> isn't whitespace, the second call to >> will attempt to place whatever it is, in this case a char, in the next variable it sees, in this case a char. It extracts the char from the input buffer and leaves everything else behind for the the next call to >> etc.

    If the first char of the input stream isn't compatable with the type >> is looking for, it can't extract anything from the stream, but keeps trying, ending up with the infamous infinite loop when a char is entered when an int is expected.

    Note: the sequential calls to >> works here because >> is only looking for int and char. If >> was looking for string then it will not terminate except on whitespace, as all other char are compatable with the string type.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    thanks a lot elad
    know is clear for me

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Note that the istringstream works the same as cin or an ifstream would in this case when using >>.

    Also note that your validate function would return true on 1111s22s333 or 1-2-3, so depending on your requirements you might have to change it to be more accurate.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    copy that ,
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istringstream problem in switch block?
    By wolfindark in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2007, 12:56 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Initializing an istringstream
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2006, 09:49 PM
  4. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  5. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21