Thread: Small Help with C++ If statement

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Small Help with C++ If statement

    When i run this program, the only thing that happens is it constantly says: You get a lolli-pop! yay!....What am i doing wrong, i tried changing the if (good=="bad") to 0 or 1, but 1 wouldnt work.......it says its forbidden, plz help
    #include <iostream.h>
    #include <stdlib.h>
    int main(void)
    {
    char good[5];
    cout<<"Please State you intentions, good or bad: ";
    cin.getline(good, 5, '\n');
    if (good=="bad")
    { cout<<"You get spanked!"<<endl;
    system("PAUSE");
    }
    else if(good=="good")
    system("PAUSE");
    { cout<<"You get a lolli-pop! yay!"<<endl;
    }
    system("PAUSE");
    return 0;
    }
    THE Dark_Knight_506

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    if (good=="bad")

    is not a legal comparison

    include, string.h and use this

    if(strcmp(good,"bad"))

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    well, it works now, thanks, but now when im good i get spanked and when im bad i get a lolli-pop? any help is appreciated
    THE Dark_Knight_506

  4. #4
    Unregistered
    Guest
    Originally posted by dk8790
    well, it works now, thanks, but now when im good i get spanked and when im bad i get a lolli-pop? any help is appreciated
    Maybe you have the strcmp's in the wrong place?
    if (strcmp(good, "good")
    cout << "lollipop!";
    else if (strcmp(good, "bad")
    cout << "Spanked!";
    else
    cout << "That's not right!";

    Also, in these original lines:
    else if(good=="good")
    system("PAUSE");
    { cout<<"You get a lolli-pop! yay!"<<endl;
    }
    the system call is in the wrong place. The cout here isn't tied to the if statement.

  5. #5
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    incase you don't know, some compilers make it so an if statement with strcmp gives 0 if it is true. it's weird, but maybe you should try this:

    if(strcmp(good, "bad")==0) // notice the ==0
    cout << "you get spanked";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  2. Creating small executables
    By cloudy in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 01:17 PM
  3. Replies: 1
    Last Post: 01-19-2006, 02:37 PM
  4. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  5. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM