Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Null char in the input queue (http://www.go4expert.com/forums/null-char-input-queue-t3403/)

blackey 15Mar2007 05:33

Null char in the input queue
 
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;
}


DaWei 15Mar2007 07:48

Re: Null char in the input queue
 
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 ().

shabbir 15Mar2007 07:52

Re: Null char in the input queue
 
Its something like fflush(stdin) that was needed in C for more than once inputting the string.

DaWei 15Mar2007 11:38

Re: Null char in the input queue
 
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.

blackey 15Mar2007 14:17

Re: Null char in the input queue
 
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

Colin Mac 15Mar2007 21:32

Re: Null char in the input queue
 
The newline character is in the buffer from hitting the the enter key.

blackey 15Mar2007 23:38

Re: Null char in the input queue
 
Quote:

Originally Posted by Colin Mac
The newline character is in the buffer from hitting the the enter key.

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?

DaWei 15Mar2007 23:40

Re: Null char in the input queue
 
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.


All times are GMT +5.5. The time now is 08:28.