Thread: newbie cin help

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question newbie cin help

    Ok im new at this and trying to learn but im having problems trying to write an or function for cin. Heres what im trying to do but the or function doesnt work. It will run but it always comes up false.

    Code:
    cin.getline ( resp1, 50 );
    if ( strcmp ( resp1, "answer1 || answer2") == 0)
       cout<<"Outputted text"<<"\n";
    else
       cout<<"outputted text"<<"\n";
    I Hope someone can help me out. if you have any ideas or think you can help let me know.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let me guess: you want to check if resp1 is equal to "answer1" or "answer2". The solution is to write:
    Code:
    if (strcmp(resp1, "answer1") == 0 || strcmp(resp1, "answer2") == 0)
    But really, you should be using std::string, in which case your entire snippet could be simplified to:
    Code:
    getline(cin, resp1);
    if (resp1 == "answer1" || resp1 == "answer2")
       cout << "Outputted text\n";
    else
       cout << "outputted text\n";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Laserlight you are amazing. thanks for the fast help. Not only did you help me solve my issue but you also made it much more simplified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ input stream and cin
    By Tool in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2010, 11:02 AM
  2. CIN inputing the wrong type
    By Bill Agnor in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2010, 06:24 PM
  3. i can not cin
    By kryptkat in forum C++ Programming
    Replies: 12
    Last Post: 01-20-2010, 05:19 PM
  4. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM

Tags for this Thread