Thread: How do I use strings with if statements? (COMPARISON)

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    4

    How do I use strings with if statements? (COMPARISON)

    Hi
    I'm using something like this to give the user a way of choosing what effects to use in an audio processing console applictaion.

    Code:
     int Q;
        cout << "Do you want to add effect?\n" << "1 for Yes or 0 for No." << endl;
        cin >> Q;
        if (Q == 1)
        {
           // EFFECT FUNCTION GOES HERE
        }
        else if (Q == 0)
          //  MOVES ONTO NEXT FUNCTION

    how could I do this with strings? so the user would put "yes" or "no"? I have looked up sting.compare but I don't know how to apply it. I tried something like this.

    Code:
     string Q1("yes");
        string Q2("no");
        string Q3;
        cout << "Do you want to add effect?\n" << "yes or no" << endl;
        cin >> Q3;
        if (Q3.compare(Q1));
        {
            // EFFECT FUNCTION GOES HERE
        }
        if (Q3.compare(Q2));
            // MOVES ONTO NEXT FUNCTION
    

    but it doesn't work (I type in "no" and it still runs the effect)



    THANK YOU

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (Q3.compare(Q1));
    Two things.
    1. The ; at the end makes it a useless comparison.
    2. string::compare - C++ Reference returns 0 when the strings are equal.

    So you would need to do
    if ( Q3.compare(Q1) == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    4
    cheers my G <3

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not just write:
    Code:
    if (Q3 == Q1)
    ?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding Strings - Statements
    By YannB in forum C Programming
    Replies: 2
    Last Post: 01-29-2014, 08:26 AM
  2. Replies: 20
    Last Post: 08-14-2007, 02:15 PM
  3. Using strings in if statements
    By Brittany in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 05:15 PM
  4. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM
  5. need a little help with if statements and strings
    By kes103 in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 12:08 PM

Tags for this Thread