Thread: String inputs, 1 or 2?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    String inputs, 1 or 2?

    Basically, in the program I'm working on now, there is a point that calls for the user to input a string. Apparently, the compiler sees
    characters separated by a space as separate strings. In my program there are options for this string input, for example:

    Bardiche
    Earth Render

    Thus far I can only set it to accept one or the other; if I use
    a cin followed by two strings, I can continue with Earth Render
    but if I enter Bardiche the program still demands a second string.
    Likewise, if I use a cin followed by one string the program will only check the input BEFORE the space for the conditions that follow. Any help on how to remedy this issue, so that the program can immediately check the input if it is one string long and if so not force the user to enter a second string?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    try
    Code:
    cin >> whatever:
    cin.ignore(256,'\n');

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Have you got any problematic code you can post up so we can help you?

    Anyways, If you mean when you hit a statement like this:

    Code:
    cin >> mystrvar;
    And you enter input like this:

    Code:
    Lee Thomas
    And you try to output mystrvar and you only get this:

    Code:
    Lee
    Then use fgets instead:

    Code:
    fgets (mystrvar.c_str(), 20);
    Actually I'm pretty certain my call to fgets above is incorrect so check with MSDN / man first just to make sure
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by swanley007
    try
    Code:
    cin >> whatever:
    cin.ignore(256,'\n');
    Damn, beat me
    I've never seen that done before either. Learnt something new.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Ya my code will pull all in until it encounters the enter key being pressed to input and will ignore and not add the return character to the string

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    2 quick ways I see:

    First way:
    For C++ strings use std::getline, for C-strings (arrays) use std::cin.getline
    These will store everything you enter until the user hits the enter-key. Then just check how many spaces there are.

    second way:
    Have the user input one word first, and if the word is Earth, have the user input another word.

    And as a comment to swanleys "solution": That wont help the poster. How in the world does calling cin.ignore going to help him pull out the second string if there exists one?? It will just "delete" it from memory.
    Last edited by Shakti; 10-01-2005 at 02:16 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Shakti, if you can post an example as to how to use std::getline, I think that would solve the problem.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <string>
    
    ...
    
    std::string line;
    
    ...
    
    // store everything up until the first newline character into "line"
    std::getline(std::cin,line);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Well......that doens't work......the best I got was

    Code:
    std::getline(cin>>"Earth Render",line)
    cout<<line
    cout<<line showed only " Render"

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    no no no, getline is used instead of std::cin, you use the call exactly as it was showed to you. Here is a completely working program:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string inputString;
        std::getline(std::cin, inputString);
        std::cout << inputString << std::endl;
        std::cin.get();
    }
    if I input something like "this is just a test to see if everything works", in the console that pops up it will output the same.

    Not to mention that your example doesnt compile.
    Last edited by Shakti; 10-01-2005 at 02:42 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Hmm......does for me..."Earth Render" is what I actually enter, mind you.

    In any case, using the code you posted, what happens is that in the
    Code:
    for(;;)
    {
    cout<<"Which weapon?"<<endl;
        for(int i=0;i<5;i++)
        {
        cout<<sWeap[iCount][i]<<endl;
        }
    /////////////////////////
    string sWeapIN;
    getline(cin,sWeapIN);
    cout<<sWeapIN<<endl;
    system("PAUSE");
    /////////////////////////
    This is, of course, not the entire for(; loop but the relevant part of it. The first time the program encounters this snippet, it basically goes straight to the pause...sWeapIN is empty on the output. Once I hit a key, the loop goes back to the beginning and doens't skip the lines the second time.
    Last edited by SkyRaign; 10-01-2005 at 05:07 PM.

  12. #12
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Tried extracting whatevers already in the cin stream? You should be clearing the stream after every time you use cin, but for this case, just try entering cin.ignore(); before the getline(). You could also try cin.clear();, or I believe I got this off Prelude:
    Code:
    #include <limits>
    
    cin.ignore (std::numeric_limits <std::streamsize>::max (), '\n');
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your entire code if it's small enough would be helpful to diagnose the problem. It probably requires a call to ignore() to ignore a leftover newline as Dae mentioned, but if the example program provided by Shakti doesn't work it might be a known bug in VC++ 6.0 (if you use that).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM