1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Null char in the input queue

Discussion in 'C++' started by blackey, Mar 15, 2007.

  1. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Using the code below i can't seem to figure out why i have a Null character in the input queue and therefore unable to use the cin.getline() for the name input.

    I know if i uncomment the cin.get() it will work, but the question is... Why do i need it? Also if the Pizza::name is inputted first, before the diameter and weight it will work, anybody know what causes this problem?

    BTW rock on, first post! :) :)

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct pizza
    {
    char name[20];
    float diameter;
    float weight;
    };
    
    int main()
    {
    	pizza *ptr = new pizza;
    
    	cout << "Enter the pizza diameter: ";
    	cin >> ptr->diameter;
    
    	cout << "Enter the pizza weight: ";
    	cin >> ptr->weight;
    
    	//cin.get();  // this is required to capture the rogue null char from the input queue
    
    	cout << "Enter the pizza company name: ";
    	cin.getline(ptr->name,20);
    
    	cout << endl 
    		<< "Name:     " << ptr->name << endl
    		<< "Diameter: " << ptr->diameter << endl
    		<< "Weight:   " << (*ptr).weight << endl;   
    
    cout << "\n\n\n\n";
    system("pause");
    return 0;
    }
    
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    It isn't a nul, and it isn't mysterious; it's stuff that was in the stream that you didn't take out, commonly a newline. cin.get () is only effective if there's just one. Check out cin.sync () and cin.ignore ().
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Its something like fflush(stdin) that was needed in C for more than once inputting the string.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Unfortunately, fflush (stdin) is not part of the standard. Some compilers support it, some don't. Fflush, per the standard, is for output. rewind (stdin), however, will do the job, although it sounds goofy.
     
  5. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Nice, the cin.sync() looks like it could be pretty useful. Although I'm still puzzled as to why (more then likely) i had a newline in the input buffer, not only that, but why the second cin didn't use the newline from the input buffer but instead get's saved for the cin.getline().?

    I'm pretty new to this so the more i clarify the better in the long run. :D
     
  6. Colin Mac

    Colin Mac New Member

    Joined:
    Jan 21, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    The newline character is in the buffer from hitting the the enter key.
     
  7. blackey

    blackey New Member

    Joined:
    Mar 14, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    	cout << "Enter the pizza diameter: ";
    	cin >> ptr->diameter;
    
    	cout << "Enter the pizza weight: ";
    	cin >> ptr->weight;              // why doesn't this cin get autofilled by the newline char after inputting the value above? (ptr->diameter)?
    
    	cout << "Enter the pizza company name: ";
    	cin.getline(ptr->name,20);
    
    Right, i get that. But if this is the case, how come the second 'cin >>' isn't auto filled by the newline char created by the first cin?
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    To expand on Colin's post, nothing happens on input in standard C/C++ functions until and end-of-line is seen. Even if you're parsing on whitespace (as with >>, by default), the buffer is not available for that extraction until a new line or EOF happens. The second extraction didn't use the newline because it doesn't return whitespace. I recommend that you reread the documentation for the various input mechanisms. They explain what is returned, what is not returned, and whether or not the things that are not returned are retained in the stream, or stripped. In your reading, you will also note that there ae failure mechanisms, and ways to see if they occurred. If they do occur, the stream is broken and will not function again until repaired. When I see a "cin" kind of operation without an accompanying test of the stream, I know that the writer is either a novice, who hasn't learned yet, or a schlock who doesn't care. You cannot depend on your user to provide what you expect, even if you have instructed the user on what that is. Users might be lazy, inattentive, malicious, ignorant, or just go to sleep with their forehead on the keyboard. Hell, you can't even depend on a file being read without error. Test your operations for success.
     

Share This Page