Thread: String inputs, 1 or 2?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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

  4. #4
    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

  5. #5
    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.

  6. #6
    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.

  7. #7
    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

  8. #8
    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