Help with my code

This is a discussion on Help with my code within the C++ Programming forums, part of the General Programming Boards category; I have a small bug, every time I start this program it runs, but it ignores the condition and every ...

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

    Help with my code

    I have a small bug, every time I start this program it runs, but it ignores the condition and every number I type in it runs the Kill() function. I need it to run the Kill() function only if 1 is pressed, and the Die() function if 2 is pressed. Here is the code for the Header and the Main.cpp.

    Code:
    //main.cpp
    #include <iostream>
    //include Killclass.h
    #include "Killclass.h"
    
    //initialize the main function
    int main()
    {	//declare and initialize integers kill and die
    	int kill, die;
    	kill = 1;
    	die = 2;
    	//declares Player1 an object
    	Player Player1;
    	//declares an NPC EvilTaco, an object
    	Player EvilTaco;
    	//displayes text in red
    	std::cout << "Enter 1 to kill the taco, enter 2 to die from the taco: " << std::endl;
    	//if statements for dying and killing also, telling to enter a number
    	std::cin.ignore();
    
    	if(kill == 1)
    	{
    		EvilTaco.Kill();
    	}
    	else
    	{
    		Player1.Die();
    	}
    
    	system( "PAUSE" );
    }
    
    
    //Killclass.h
    
    #include <iostream>
    
    class Player
    {
    public:
    	void Kill() { std::cout << "You have killed the Evil Taco!" << std::endl; }
    	void Die() { std::cout << "You have been killed by the Evil Taco!" << std::endl; }
    };
    If someone can help that would be appreciated,

    Thanks.

    PS, I'm pretty sure it's the IF Command doing this, I don't think I have the values right.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want input from the user, maybe you should read it in or something.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by tabstop View Post
    If you want input from the user, maybe you should read it in or something.
    What does READ IT IN mean?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nicodemus95 View Post
    What does READ IT IN mean?
    Use, say, cin + >> to obtain input from the user and store said input in a variable of some kind.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Use, say, cin + >> to obtain input from the user and store said input in a variable of some kind.
    Is it at all possible you can give me an example? Unless it is like,


    Code:
    if(cin + >> 1)
    {
        EvilTaco.Kill();
    }
    PS I am a beginner programmer. =)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int what_did_the_user_type_in_anyway;
    cin >> what_did_the_user_type_in_anyway;

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4
    Ok, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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