if statement Help!

This is a discussion on if statement Help! within the C++ Programming forums, part of the General Programming Boards category; I am having trouble with this part of my program, the statement seems to be logical but when i compile ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Question if statement Help!

    I am having trouble with this part of my program,
    the statement seems to be logical but when i compile it will always go to the condition (YOU FAIL) even if i enter "human" as i have below, it never goes to the else part of the statement. Im new to C++ your help would be appreciated.
    Code:
             //Race
             
        cout << "Race: \n";
        cin.get(race, 6);
        cin.ignore(100, '\n');
        if(!(race=="human" || race=="elf"))  
                                             //if race does not = human or elf "YOU FAIL"
                                             //if race = human or elf go to else
        {
            cout << "YOU FAIL! \n...Race: \n";     //if not =
            cin.get(race, 11);
            cin.ignore(100, '\n');
            
       
            if(!(race=="human" || race=="elf"))        //if not =, again
            {
                cout<< "YOU are a moron! \nRace:  Human\n";
                strcpy(race, "human");              //default to human
            }    
        }
        else
       {
       cout << "Name:  " <<  charName <<  "\n" << "Race:  "  <<  race << "\n";               //displays Name and Race if "if statement" is true
       }
    Thank you in advance for any help
    -Scott

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Well if you're using strcpy(), chances are you need to use strcmp() as well.
    As in
    strcmp(race,"human")==0
    as a replacement for your equality test.

    Or, since this is C++, stop using C strings and just use the much safer std::string
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    That code is a bit of a mess, it is hard to follow it.
    Could you post the entire program file? It would be easier to read if we I could see what
    happens before the cin statement.

    Also, learn to indent your code, it makes reading it 100% easier.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
        //Data Structure
        
    struct enemy{
        string name;
        int level;
        int hp;
        int fullhp;
        int str;
        int def;
        int equipstr;
        int equipdef;
    };
    
    
        int xp;
        int lvlxp;
        int hp;
        int fullhp;
        int level;
        
    
    
    
    int main(int argc, char *argv[])
    {
       
       //Variables
        
        char charName[21];
        char race[6];
      
       //Intro
       
         /* Notice how long lines can be wrapped, and still count as one string */
         cout << "In a land where dragons roam and cows are plentiful. \n"
                 "You find yourself on a ranch with a dragon eclipsing the sun.  \n"
                 "The smell of blood and cow dung fills the air, its just another day in paridise."
                 "Or is it?  \n";
       
       //Character Information
          
            //Name
            
        cout << "Enter a Name:\n" ;  
            cin.get(charName,21);
            cin.ignore(100,'\n');
        cout << "Good Morrow, "  << charName << '\n';
             
             //Race
             
        cout << "Race: \n";
            cin.get(race, 6);
            cin.ignore(100, '\n');
        if(!(race=="human" || race=="elf"))  //if race does not = human or elf "YOU FAIL"
                                             //if race = human or elf go to else
        {
            cout << "YOU FAIL! \n...Race: \n"; //if not =
                cin.get(race, 11);
                cin.ignore(100, '\n');
            
       
            if(!(race=="human" || race=="elf")) //if not =, again
            {
                cout<< "YOU are a moron! \nRace:  Human\n";
                    
                strcpy(race, "human");  //default to human
            }    
        }
     else
          {
       cout << "Name:  " <<  charName <<  "\n" << "Race:  "  <<  race << "\n";  //displays Name and Race if "if statement" is true
          }
          
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    here is the whole program so far, its a work in progress... sorry about spaceing i do not know the correct way to indent it. any help with that is great as well
    Last edited by Salem; 11-09-2006 at 04:36 PM. Reason: wrapped long lines

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Why don't you use std::strings instead of C style strings?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    > system("PAUSE");

    Try not to use this to pause the console window, use cin.get() instead.

    >return EXIT_SUCCESS;

    use return 0 instead

    To echo mantd comment, you need to insert this header

    Code:
    #include <string>
    to use C++ style strings.

    If you are using Microsofts IDE's, they automaticly indent for you. If you are using DevC++
    then you will have to manually indent to three spaces each if statement block or function block.
    Or, you can click under editor oprions and change some of the settings.
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I'm assuming that he's using Dev-C++ as this:
    Code:
    system("PAUSE");
    return EXIT_SUCCESS;
    is part of the default Dev-C++ console template.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    But I hate system pause. its a personal issue, I am currenly seeking medical advise about this condition... LOL
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >>return EXIT_SUCCESS;
    >use return 0 instead
    Wait...say what? I see no reason at all for that suggestion, and at least one reason why it shouldn't be followed.
    My best code is written with the delete key.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    >Wait...say what? I see no reason at all for that suggestion, and at least one reason why it >shouldn't be followed.

    I stand corrected, sorry, I have always used return 0, I did not realize that return EXIT_SUCSESS is a better way to terminate the program
    I'm just trying to be a better person - My Name Is Earl

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    0 and EXIT_SUCCESS are synonymous.

    Quote Originally Posted by c99 draft
    7.20.4.3 The exit function
    Synopsis
    1 #include <stdlib.h>
    void exit(int status);
    Description
    2 The exit function causes normal program termination to occur. If more than one call to
    the exit function is executed by a program, the behavior is undefined.
    3 First, all functions registered by the atexit function are called, in the reverse order of
    their registration.239)
    4 Next, all open streams with unwritten buffered data are flushed, all open streams are
    closed, and all files created by the tmpfile function are removed.
    5 Finally, control is returned to the host environment. If the value of status is zero or
    EXIT_SUCCESS
    , an implementation-defined form of the status successful termination is
    returned. If the value of status is EXIT_FAILURE, an implementation-defined form
    of the status unsuccessful termination is returned. Otherwise the status returned is
    implementation-defined.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I did not realize that return EXIT_SUCSESS is a better way to terminate the program
    It's not better. 0 and EXIT_SUCCESS are functionally identical. However, EXIT_SUCCESS is more obvious than 0 and when you need a portable failure return, EXIT_SUCCESS is symmetric with EXIT_FAILURE (thus improving readability a bit).
    My best code is written with the delete key.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Ok I see now, thanks prelude
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 03:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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