Part of my C++ code

This is a discussion on Part of my C++ code within the C++ Programming forums, part of the General Programming Boards category; I just started c++ the other day and I can't get the program to load, I didn't bother to do ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    4

    Part of my C++ code

    I just started c++ the other day and I can't get the program to load, I didn't bother to do the whole code because if this part doesn't work, then what's to stop me to make the same mistake through the whole program?
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    float Health();
    float Attack();
    float Defense();
    float Spider();
    int main()
    {
    cout<<"Welcome to the Battle Arena!\n"<<endl;
    }
    float Health()
    {
    float x;
    x = 1 + rand() % 5;
    if (x >= 20)
    return 15;
    else
    return x;
    cout<<"Your starting Health is: \n"<<Health;
    }
    float Defense()
    {
    float y;
    y = 5 + rand();
    if (y > 20)
    return y;
    else
    return 20;
    {
    cout<<"Your starting Defense is: \n"<<Defense;
    }
    cout<<"Your first Enemy is the poison Spider!\n"<<endl;
    float Spider();
    {
    float a;
    a = 5 + rand();
    if (a > 10)
    return 7;
    else
    return 5;
    }
    {
    cout<<"Spider's Health is: \n"<<Spider;
    cout<<"Attack?\n"<<endl;
    }
    }

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    it works, your not calling anything in main + you should ALWAYS use return 0; in main!

    Theres alot wrong with this code. For one you should code a bit neater. Use spaces, follow the flow of what you are trying to do, and keep track of your curly braces ( {} ). For two your converting int to floats, why not just use both floats or both int's? For three i would reccomend you either learn/use classes or parameters so you aren't returning so many things, just imo of course.

    Start by giving main a return 0;

    Than make some function calls in the main. Only things in main are executed, having 400 functions means nothing without any invoking of those functions.
    Last edited by RoD; 02-13-2003 at 07:21 PM.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    threw together a quick example of your code with classes. I still reccomend you revise the logic and how you do things in it, but see how much neater and (for the most part) clearer this is? If there is anything you don't understand were more than glad to help/improve on it. The most important thing in C++ is to never give up and never stop learning.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    class Actions
    {
    public:
    	float health();
    	float attack();
    	float defense();
    	float spider();
    private:
    	float x,
    		  y,
    		  a,
    		  Health,
    		  Defense,
    		  Spider;
    };
    
    int main()
    {
    	Actions	Object;
    
    	cout << "Welcome to the Battle Arena!" << endl;
    	
    	Object.health();
    	Object.attack();
    	Object.defense();
    	Object.spider();
    	
    	return 0;
    }
    
    float Actions::health()
    {
    	
    	x = 1 + rand() % 5;
    
    	if (x >= 20)
    	{
    		return 15;
    	}
    	else
    	{
    		return x;
    	}
    
    cout << "Your starting Health is: " << Health << endl;
    
    }
    
    float Actions::defense()
    {
    	
    	y = 5 + rand();
    
    	if (y > 20)
    	{
    		return y;
    	}
    	else
    	{
    		return 20;
    	}
    
    cout << "Your starting Defense is: " << Defense << endl;
    cout << "Your first Enemy is the poison Spider!" << endl;
    
    }
    
    float Actions::spider()
    {
    
    	a = 5 + rand();
    
    	if (a > 10)
    	{
    		return 7;
    	}
    	else
    	{
    		return 5;
    	}
    
    cout << "Spider's Health is: " << Spider << endl;
    cout << "Attack? " << endl;
    }
    Last edited by RoD; 02-13-2003 at 08:08 PM.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Why are you returning 1 in main(), RoD?
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    typo, i fixed it.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Indent your code, its the ONLY way. Its absolutly 100% more proffesional.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I'm more than willing to help you with this, btw, feel free to PM/Email me if u run into a wall, i'm logging off soon.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    4
    Thanks, I was thinking that I would just rush into making a decent game on my first day and it wasn't working, then I got mad, and I started looking for message boards to ask questions.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    4
    Would anyone happen to know how to use graphics in a program, so that I could like make a Galaga look alike or something of the sort?

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    theres a few ways, do you mean console or like actual game graphics. Check out the game forum, theres alot of links and information there.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    4
    How would I change what letters are. Like if I were to make up a new alphabet, how would I make a=(some other letter). If anyone if familiar with the game Final Fantasy 10, I am trying to make my own new alphabet like Al Bhed, just changing where the letter are in the alphabet.

  12. #12
    Unreg1
    Guest
    Originally posted by Maverick
    How would I change what letters are. Like if I were to make up a new alphabet, how would I make a=(some other letter). If anyone if familiar with the game Final Fantasy 10, I am trying to make my own new alphabet like Al Bhed, just changing where the letter are in the alphabet.
    How about a lookup table, something like that used in ASCII to EBCDIC conversions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Skipping part of the code.
    By And3rs in forum C Programming
    Replies: 38
    Last Post: 10-01-2008, 02:52 PM
  2. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 09:28 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

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