3 Replies - 89 Views - Last Post: 04 December 2014 - 01:42 PM Rate Topic: -----

#1 Nanyo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-July 14

variables from different functions don't match

Posted 04 December 2014 - 11:46 AM

In the code below there is some error, I think it is related with functions. It compiles properly but then it outputs only : "humanslife8.90145e+032"
#include <iostream>
#include <windows.h>
#include <stdlib.h>

using namespace std;

void userInput(float HumanNumber, float SkeletonNumber, float AllHumanslife, float AllSkeltonslife, float SkeletonHealth, float HumanHealth);
void HumansTurn(int turn, float HumanAttack, float AllSkeletonslife, float HumanNumber, float HumanDamage, float SkeletonHealth, float SkeletonNumber);
void SkeletonsTurn(int turn, float SkeletonAttack, float AllHumanslife, float SkeletonNumber, float SkeletonDamage, float HumanNumber, float HumanHealth);

int main(){

    // defining human properties
    float HumanAttack;
    float HumanHealth = 100;
    float HumanDamage = 70;
    float HumanNumber;
    float AllHumanslife;

    // defining skeleton properties
    float SkeletonAttack;
    float SkeletonHealth = 80;
    float SkeletonDamage = 65;
    float SkeletonNumber;
    float AllSkeletonslife;

    // defining whose turn is on
    int turn = 0;

    // user inputs the number of humans
    userInput(HumanNumber, SkeletonNumber, AllHumanslife, AllSkeletonslife, SkeletonHealth, HumanHealth);

    // the battle cycle
    while(true){

        while(AllHumanslife > 0 and AllSkeletonslife > 0){
            // Humans turn
            if(turn == 0){
                cout<<"humanslife" << AllHumanslife <<endl;
                HumansTurn(turn, HumanAttack, AllSkeletonslife, HumanNumber, HumanDamage, SkeletonHealth, SkeletonNumber);
            }
            // Skeletons turn
            if(turn == 1){
                cout<<"skeletonslife" << AllSkeletonslife <<endl;
                SkeletonsTurn(turn, SkeletonAttack, AllHumanslife, SkeletonNumber, SkeletonDamage, HumanNumber, HumanHealth);
            }
            //Battle results
            if(AllSkeletonslife <= 0){
                cout<<"Humans Lost";
                break;
            }
            if(AllHumanslife <= 0){
                cout<<"Skeletons Lost";
                break;
            }
        }
    }

    return 0;
}



// the user enters the number of skeletons and humans
void userInput(float HumanNumber, float SkeletonNumber, float AllHumanslife, float AllSkeletonslife, float SkeletonHealth, float HumanHealth){
    cout << "\t ***  Skeletons vs Humans  ***\n\n\n";
    cout<<"Please enter the number of human soldiers:\n";
    cin>>HumanNumber;
    cout<<"Please enter the number of skeletons:\n";
    cin>>SkeletonNumber;
    //all life of humans and skeletons
        AllHumanslife = HumanNumber * HumanHealth;
        AllSkeletonslife = SkeletonNumber * SkeletonHealth;
}

// Humans turn
void HumansTurn(int turn, float HumanAttack, float AllSkeletonslife, float HumanNumber, float HumanDamage, float SkeletonHealth, float SkeletonNumber){
    // Humans's chance to hit
    HumanAttack = (rand()%100 + rand()%100)/1.8;
        if(HumanAttack <= 60){
            AllSkeletonslife -= HumanNumber * HumanDamage;
            if(SkeletonHealth <= 0){
                SkeletonNumber -= 1;
            }
        }else{
            // switch to the other turn
            turn = 1;
        }
        //when attack finished switch to the other turn
        turn = 1;
}

// Skeletons turn
void SkeletonsTurn(int turn, float SkeletonAttack, float AllHumanslife, float SkeletonNumber, float SkeletonDamage, float HumanNumber, float HumanHealth){
    // Skeleton's chance to hit
    SkeletonAttack = (rand()%100 + rand()%100)/1.8;
        if(SkeletonAttack <= 40){
            AllHumanslife -= SkeletonNumber * SkeletonDamage;
            if(HumanHealth <= 0){
                HumanNumber -= 1;
            }
        }else{
            // switch to the other turn
            turn = 0;
        }
        //when attack finished switch to the other turn
        turn = 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: variables from different functions don't match

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: variables from different functions don't match

Posted 04 December 2014 - 11:54 AM

What do you think it should be outputting?

You are passing all of your variables to the functions by value so any changes made to those variables in those functions are lost when the functions return.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Nanyo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-July 14

Re: variables from different functions don't match

Posted 04 December 2014 - 01:30 PM

Thank you Jim, can tell me how to do it then. Do the variable have to have "&" in front
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: variables from different functions don't match

Posted 04 December 2014 - 01:42 PM

I suggest you start with this tutorial, then continue with the tutorials contained in my signature.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1