Making cin attach no value to an array when you push enter

This is a discussion on Making cin attach no value to an array when you push enter within the C++ Programming forums, part of the General Programming Boards category; How do you make cin to allow the program to continue to the next line, if there is no value ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Making cin attach no value to an array when you push enter

    How do you make cin to allow the program to continue to the next line, if there is no value to store in the array when you push enter?

    Example:

    Code:
    cout << "Enter a name: "
    cin >> X;
    cout << "Continuing the application anyhow."
    Program shows:

    Enter a name: // You push only the enter button, and want the program to continue instead of jumping rows.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Perhaps try cin.ignore after cin >> X.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps:
    Code:
    cout << "Enter a name: "
    cin.get();
    cout << "Continuing the application anyhow."
    Assuming there is nothing left behind to read in cin.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by anon View Post
    Perhaps:
    Code:
    cout << "Enter a name: "
    cin.get();
    cout << "Continuing the application anyhow."
    Assuming there is nothing left behind to read in cin.
    It's in a for loop, and it dosent seem to work the way you put it. There are lots of cin and cout in the program, and the loop aswell.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    OK, so you want to accept empty strings as well.

    The simplest way is to use getline instead of >>
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s;
        while (getline(cin, s))
            cout << "\"" << s << "\"\n";
    }
    The difference, of course, is that this will read an entire line into a string, not up to first whitespace.

    Also, if there are lots of cin >> 's in the program, you should empty the input buffer after using >> and before using getline:
    Code:
    #include <limit>
    
    ...
         cin.ignore(numeric_limits<streamsize>::max(), '\n');
    Last edited by anon; 05-27-2008 at 04:21 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by anon View Post
    OK, so you want to accept empty strings as well.

    The simplest way is to use getline instead of >>
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s;
        while (getline(cin, s))
            cout << "\"" << s << "\"\n";
    }
    The difference, of course, is that this will read an entire line into a string, not up to first whitespace.

    Also, if there are lots of cin >> 's in the program, you should empty the input buffer after using >> and before using getline:
    Code:
    #include <limit>
    
    ...
         cin.ignore(numeric_limits<streamsize>::max(), '\n');
    Thanks
    Last edited by Glauber; 05-27-2008 at 05:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  2. Making an array for a class
    By sbook in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2005, 02:33 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 06:30 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

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