I have two problems

This is a discussion on I have two problems within the C++ Programming forums, part of the General Programming Boards category; First off, heres my code. Code: #include <iostream> #include <cstdlib> #include <cstring> #include <time.h> using namespace std; string Command; struct ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    I have two problems

    First off, heres my code.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <time.h>
     
    using namespace std;
     
    string Command;
     
    struct User
    {
           int UHP; // Users Current Hit Points
           int max_UHP; // Users Maximum Hit Points
    };
    struct Challenger
    {
           int CHP; // Challengers Current Hit Points
           int max_CHP; // Challengers Maximum Hit Points
    };
    int main()
    {
        srand( time(NULL) );
        User Player;
        Challenger Enemy;
        
        Player.max_UHP = 100; // Users Maximum Health Points
        Player.UHP = 100; // Users Current Health Points
        int UATK = rand()%20; // Users Attack Points
        int UDF = rand()%20; // Users Defence Points
        int UACC = rand()%10; // Users Accuracy Points
        int UAGT = rand()%10; // Users Agility Points
        
        Enemy.max_CHP = 100; // Challengers Maximum Health Points
        Enemy.CHP = 100; // Challengers Current Health Points
        int CATK = rand()%15; // Challengers Attack Points
        int CDF = rand()%15; // Challengers Defence Points
        int CACC = rand()%10; // Challengers Accuracy Points
        int CAGT = rand()%10; // Challengers Agility Points
     
        cout<<"As your walking down a path a cloaked swordsman ambushes you.\n";
        cout<<"He then challenges you to a fight and you humbly accept.\n";
        
        do{
        cout<<"Your turn:\n";
        cin>>Command;
        cin.ignore();
        
        if(Command=="Attack"||Command=="attack"){
             
             if(UACC >=6 && CAGT >=6){Enemy.CHP-=(UATK-=CDF);
             cout<<UATK<<" Damage!\n";
             cout<<"He blocked "<<CDF<<" damage.\n";
             cout<<"Him: "<<Enemy.CHP<<"/"<<Enemy.max_CHP<<"\n";}
             
             else if(UACC >=6){Enemy.CHP-=UATK;
             cout<<UATK<<" Damage!\n";
             cout<<"Him: "<<Enemy.CHP<<"/"<<Enemy.max_CHP<<"\n";}
             
             else(UACC <=5);{
             cout<<"Miss\n";}
             }
        cout<<"He attacks.\n";
        Player.UHP-=CATK;
        cout<<"You lost "<<CATK<<" hp.\n";
        cout<<"You: "<<Player.UHP<<"/"<<Player.max_UHP<<"\n";
        }while(Player.UHP >=0 || Enemy.CHP >=0);
        cin.get();
    }
    This is a battle program Im working on. In the code it subtracts the defenders defence from the attackers attack then subtracts the rest from the defenders health points

    ex : Enemy.CHP-=(UATK-=CDF)

    now my problem is if the defence is higher than the attack it adds the remaining points to the defenders health. How can I stop it from doing that.

    Second problem, I heard that the way Im using rand isnt exactly the right and best way to use it. I agree because my numbers arent very random at all. Can someone tell me how I would fix this and work into my code?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> How can I stop it from doing that.
    Perhaps you could separate those two operations and actually check for a negative value.

    >> I heard that the way Im using rand isnt exactly the right and best way to use it.
    Looks fine to me. Why do you think the numbers aren't very random? I doubt you'll need anything better than what you're using.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35
    Quote Originally Posted by Daved
    >> How can I stop it from doing that.
    Perhaps you could separate those two operations and actually check for a negative value.

    >> I heard that the way Im using rand isnt exactly the right and best way to use it.
    Looks fine to me. Why do you think the numbers aren't very random? I doubt you'll need anything better than what you're using.
    How would I check for a negative value?

    My number are barely randum at all, if you run the program you pretty much get the same responce every time.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> How would I check for a negative value?
    Something like if (UATK < 0)

    >> My number are barely randum at all, if you run the program you pretty much get the same responce every time.
    Can you give an example? If the randomness wasn't working, the initial value for UATK would be the same every time. If it was working, it would get a pretty even distribution between 0 and 19 every time. Try outputting that value as soon as you set it with rand to find out whether rand is working. The same would be true for each of the other values you set with rand() calls.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    35
    ok when you start it up, the first responce you get is what you contnue to get. its not always the same thing when you start the program but about 90% of the time you get the same responce

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Can you give an example? Like, do you get "Damage!" 90% of the time? Do you get "Miss" 90% of the time?

    Run the program 10 times, and keep track of the exact first output for each run, then post them here. I haven't run that code myself, so real details from your runs would make it a lot easier to identify what's going on.

    BTW, it should be #include <string>, not #include <cstring>.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> else(UACC <=5);{
    That's probably a problem right there. The else should look like this:

    else{

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    35
    about 6 out of 10 it misses and then the next time around it repeats the same responces

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> about 6 out of 10 it misses
    It should miss 6 out of ten times on the first attack. A miss happens when UACC is less than or equal to 5. Since you are using rand() % 10 for UACC, you will get numbers from 0-9. So 6 out of ten times, you will get a number less than or equal to 5. Four out of ten times you will get a number greater than or equal to 6, which will be a hit or block.

    >> then the next time around it repeats the same responces.
    I'm not sure what you mean by that. Obviously, a lot more detail would help, like exact input and output for a run that you think is doing something it shouldn't. If you mean that 6 out of 10 times it misses, and then the next attack misses in the same run, that makes sense, since the UACC is lower than 6 and it never changes in the loop.

    The random numbers are correct. Keep working on your code to see if you can find any bugs that are causing any other problems that you are having.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    35
    see thats what i was thinking since its in the loop its going to repeat anyways but i have no idea how to fix something like that

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    One idea would be to call rand() to get the accuracy once for each attack instead once for the player.

    You also always use 6 as the threshhold for hit or miss. If you want the player's ability to be random as well, maybe you could make the threshhold be random. So you could get a random number in the beginning of the game using rand() % 10. That will be the player's accuracy. Then, each attack get another random number using rand() % 10, and that will be the accuracy of the particular attack. If the attack's accuracy is greater than the player's accuracy, it is a hit. If it is less or equal, it is a miss.

    Just play around with those types of changes, and think of them from an algorithm point of view instead of a code point of view first. Once it makes sense, then see if you can code it.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    35
    mmm sounds good let me check this out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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