newbie cin help

This is a discussion on newbie cin help within the C++ Programming forums, part of the General Programming Boards category; Ok im new at this and trying to learn but im having problems trying to write an or function for ...

  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
    20,954
    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";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:19 PM
  4. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 11:42 AM
  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


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