Why doesn't this work?

Discussion in 'C++' started by Chris Lount, Aug 30, 2003.

  1. Chris Lount

    Chris Lount Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi, I'm pretty new to c++ . I'm trying to work out why the following code
    doesn't work.
    I've just learned about cin.get() and written the following program to enter
    a string and store it in an array.

    When I run the program it asks for the first string and then prints it.
    However, it doesn't ask for input for the second string, it simply prints
    "Enter the second string", then "The second string is: ".

    Why doesn't cin.get() ask for input the second time around?

    Thanks alot for any help

    Chris.



    #include<iostream>
    #include<string>

    int main()
    {
    char bufferOne[50];
    std::cout << "Enter the first string: ";
    std::cin.get( bufferOne, 49 );
    std::cout << "The first string is " << bufferOne << std::endl;

    char bufferTwo[50];
    std::cout << "Enter the second string: ";
    std::cin.get( bufferTwo, 49 );
    std::cout << "The second string is " << bufferTwo << std::endl;

    return 0;

    }


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQE/TncRrAxy75AP1L8RAneAAJ91c46HwkcLcCJfCk7+ybcOy0AcBACdHrUc
    X3eTqKmgH6XGCCfw7u5u/9g=
    =JgsO
    -----END PGP SIGNATURE-----
    Chris Lount, Aug 30, 2003
    #1
    1. Advertising

  2. Chris Lount

    Rolf Magnus Guest

    Chris Lount wrote:

    > Hi, I'm pretty new to c++ . I'm trying to work out why the following
    > code doesn't work.
    > I've just learned about cin.get() and written the following program to
    > enter a string and store it in an array.
    >
    > When I run the program it asks for the first string and then prints
    > it. However, it doesn't ask for input for the second string, it simply
    > prints "Enter the second string", then "The second string is: ".
    >
    > Why doesn't cin.get() ask for input the second time around?


    get() reads until (but not including) the first \n character. That
    character is left in the input buffer. When you use get() again, the
    first character that is found is again that \n, so nothing is read. Use
    getline() instead of get().

    > #include<iostream>
    > #include<string>


    You're not using anything from <string>, though you actually should. Why
    aren't you using std::string instead of char arrays?

    > int main()
    > {
    > char bufferOne[50];
    > std::cout << "Enter the first string: ";
    > std::cin.get( bufferOne, 49 );
    > std::cout << "The first string is " << bufferOne << std::endl;
    >
    > char bufferTwo[50];
    > std::cout << "Enter the second string: ";
    > std::cin.get( bufferTwo, 49 );
    > std::cout << "The second string is " << bufferTwo <<
    > std::endl;
    >
    > return 0;
    >
    > }
    Rolf Magnus, Aug 30, 2003
    #2
    1. Advertising

  3. Chris Lount

    Chris Lount Guest

    Rolf Magnus wrote:

    > Chris Lount wrote:
    >
    >> Hi, I'm pretty new to c++ . I'm trying to work out why the following
    >> code doesn't work.
    >> I've just learned about cin.get() and written the following program to
    >> enter a string and store it in an array.
    >>
    >> When I run the program it asks for the first string and then prints
    >> it. However, it doesn't ask for input for the second string, it simply
    >> prints "Enter the second string", then "The second string is: ".
    >>
    >> Why doesn't cin.get() ask for input the second time around?

    >
    > get() reads until (but not including) the first \n character. That
    > character is left in the input buffer. When you use get() again, the
    > first character that is found is again that \n, so nothing is read. Use
    > getline() instead of get().
    >
    >> #include<iostream>
    >> #include<string>

    >
    > You're not using anything from <string>, though you actually should. Why
    > aren't you using std::string instead of char arrays?


    I'm still new, the only string functions i know are getlen() strcpy() and
    strncpy(). I'm currently going through my Linux info pages to find out more
    info on the standard functions.

    Thanks for the help, I'll look into cin.getline() and also explore the
    functions in string a bit more.


    >
    >> int main()
    >> {
    >> char bufferOne[50];
    >> std::cout << "Enter the first string: ";
    >> std::cin.get( bufferOne, 49 );
    >> std::cout << "The first string is " << bufferOne << std::endl;
    >>
    >> char bufferTwo[50];
    >> std::cout << "Enter the second string: ";
    >> std::cin.get( bufferTwo, 49 );
    >> std::cout << "The second string is " << bufferTwo <<
    >> std::endl;
    >>
    >> return 0;
    >>
    >> }
    Chris Lount, Aug 30, 2003
    #3
  4. Chris Lount

    PT Guest

    > Hi, I'm pretty new to c++ . I'm trying to work out why the following code
    > doesn't work.
    > I've just learned about cin.get() and written the following program to enter
    > a string and store it in an array.
    >
    > When I run the program it asks for the first string and then prints it.
    > However, it doesn't ask for input for the second string, it simply prints
    > "Enter the second string", then "The second string is: ".
    >
    > Why doesn't cin.get() ask for input the second time around?


    The problem is, because cin.get() reads characters from stdin until it
    encounters a newline ('\n'). Then it finishes and writes a terminating 0 at
    the end of the string. But the newline is pushed back to the input stream.
    :-(
    Then you run your second cin.get(). It starts reading characters from the
    input stream and what does it notice first? The first character available
    is '\n'! That means that we must stop and write the terminating 0!
    That is the problem!

    The simplest solution would be:

    #include<iostream>

    // #include<string> is not necessary here!

    int main()
    {
    char bufferOne[50];
    std::cout << "Enter the first string: ";
    std::cin.get( bufferOne, 49 );
    std::cout << "The first string is " << bufferOne << std::endl;

    //OK, let's just read the remaining '\n'!
    std::cin.get();
    char bufferTwo[50];
    std::cout << "Enter the second string: ";
    std::cin.get( bufferTwo, 49 );
    std::cout << "The second string is " << bufferTwo << std::endl;

    return 0;

    }

    I know that there are other solutions also, but mine worked fine...
    I hope it is useful for you.
    PT, Aug 30, 2003
    #4
  5. > > You're not using anything from <string>, though you actually should. Why
    > > aren't you using std::string instead of char arrays?

    >
    > I'm still new, the only string functions i know are getlen() strcpy() and
    > strncpy(). I'm currently going through my Linux info pages to find out

    more
    > info on the standard functions.


    Forget about those functions, there are remnants from C and in most cases
    not needed in C++. The std::string class is much easier and safer to use:

    #include<iostream>
    #include<string>

    int main()
    {
    std::string str1;
    std::cout << "Enter the first string: ";
    std::getline(std::cin, str1);
    std::cout << "The first string is " << str1 << std::endl;

    std::string str2;
    std::cout << "Enter the second string: ";
    std::getline(std::cin, str2);
    std::cout << "The second string is " << str2 << std::endl;

    return 0;
    }

    You see, no need to worry about the length of the string. I recommend you
    get a good C++ beginners book; learning C++ from just info pages is going to
    be a very slow and painfull process.

    Good luck with your study!
    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Prowak

    Why oh why doesn't my data view work?

    David Prowak, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    731
    Alvin Bruney [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  4. Sara
    Replies:
    6
    Views:
    255
    John W. Krahn
    Apr 12, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    258
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page