why cin.get work here??
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: why cin.get work here??

  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    why cin.get work here??

    look at this..
    Code:
    #include <iostream>
    int main()
    {
    using namespace std;
    cout << "What year was your house built?\n";
    int year;
    cin >> year;
    cout << "What is its street address?\n";
    char address[80];
    cin.getline(address, 80);
    cout << "Year built: " << year << endl;
    cout << "Address: " << address << endl;
    cout << "Done!\n";
    return 0;
    }
    It doesn't give me time to type address..
    output is something like this
    Code:
    What year was your house built?
    1966
    What is its street address?
    Year built: 1966
    Address
    Done!
    Why it doesn't ask for address?
    Answer; after giving to inti_g_er as we press "Enter key" there comes a new line character in input queue that is transfered to address.. that's why it is not give us time to type address..
    OK...
    but what if i assign address as a char(just as an experiment since address can't be a character)

    Code:
    #include <iostream>
    #include<cstring>
    int main()
    {
    using namespace std;
    cout << "What year was your house built?\n";
    int year;
    cin >> year;
    cout << "What is its street address?\n";
    char address;
    cin>>address;
    cout << "Year built: " << year << endl;
    cout << "Address: " << address << endl;
    cout << "Done!\n";
    return 0;
    }
    this time it asks for address variable...
    Code:
    What year was your house built?
    1966
    What is its street address? P
    Year built: 1966
    Address P
    Done!
    why new line character is not transferred to address variable in this case???????????????????

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    Re: why cin.get work here??

    In the first case, you use getline(). getline() processes input until a '\n'
    is encountered. So after entering the year (followed by return), the '\n'
    is still in the input stream buffer, so addess ends up empty (as you observed).

    In the second case, you are using operator >> ... By default, operator >>
    ignores whitespaces, so the fact that the '\n' is still in the input buffer
    stream is not relevant.

  3. #3
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: why cin.get work here??

    Quote Originally Posted by Philip Nicoletti View Post
    In the first case, you use getline(). getline() processes input until a '\n'
    is encountered. So after entering the year (followed by return), the '\n'
    is still in the input stream buffer, so addess ends up empty (as you observed).

    In the second case, you are using operator >> ... By default, operator >>
    ignores whitespaces, so the fact that the '\n' is still in the input buffer
    stream is not relevant.
    so you mean if we get very next input by >>(after cin.getline) operator then no such probem will appear..

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: why cin.get work here??

    Quote Originally Posted by vkash
    so you mean if we get very next input by >>(after cin.getline) operator then no such probem will appear..
    In general, yes. Consequently, one method to avoid this kind of problem is to read line by line into strings, then parse the line, e.g., with the help of a stringstream.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center