C++ Functions Question

言語: JP EN DE FR
2010-06-21
New Items
users online
フォーラム » Everything Else » Tech Support » C++ functions question
C++ functions question
 Asura.Muneonna
Offline
サーバ: Asura
Game: FFXI
user: Muneonna
Posts: 9
By Asura.Muneonna 2010-02-28 21:58:28  
Is there some reason why i can only do very simple math in a C++ function? and no math whatsoever involving new variables within that function?

#include <iostream>
using namespace std;
int test(int evasion, int acc);
int main()
{
int evasion = 200;
int acc = 185;

int combo = test(evasion,acc);
cout << combo << "\n\n";
system("PAUSE");
return 0;
}

int test(int evasion, int acc)
{
int total = evasion + acc;
int average = 100;
int acc2 = acc;
// next few lines are my test code
if ( evasion == 200 && acc == 185 && average == 100) {
total = acc2 / total;
return total;
} else {
cout << "FAIL";
system("PAUSE");
return 0;
}
// int total2 = evasion / total;
// return total2;
}

Above code returned 0.
 Diabolos.Chibixzero
Offline
サーバ: Diabolos
Game: FFXI
Posts: 204
By Diabolos.Chibixzero 2010-02-28 22:03:02  
<?php echo'What is this c++' ;?> :)
 Fenrir.Scragg
Administrator
Offline
サーバ: Fenrir
Game: FFXI
user: Scragg
Posts: 2579
By Fenrir.Scragg 2010-02-28 22:05:26  
pretty sure when you divide two integers, you will get an integer. (not a float) 185/385 == 0

try: float z = (float) x / (float) y
[+]
 Asura.Muneonna
Offline
サーバ: Asura
Game: FFXI
user: Muneonna
Posts: 9
By Asura.Muneonna 2010-02-28 22:10:07  
:D lol. im designing a game. this is all going into the engine, and it would be much faster for me to send Accuracy and Evasion to a function and have that do the math, than doing it individually within a variable (because im planning on a turn based RPG wherein you can be attacked by multiple people at once, and you can defend yourself simultaneously from said attackers.

as such, sending all the info to a function to do the math for me would be godly, but its not working right >.>

What i need in lames terms is:

Have 2 values

Have those two values to be sent to a function to have work done.

while work is being done on those variables, i need to be running the pseudo random numbers to be making a number between 1 and 100.

compare those two variables by finding the percentage of each. (add them together like so:)

[ (acc[100] / (acc[100] + eva[87]))*100 ]
AND
[ (eva[87] / (acc[100] + eva[87]))*100 ]

BUT for some reason i cant use the same variable for math twice in a function >.>
 Asura.Muneonna
Offline
サーバ: Asura
Game: FFXI
user: Muneonna
Posts: 9
By Asura.Muneonna 2010-02-28 22:21:23  
Omg scragg ty sooo much >.<

Im Nativity a PHP programmer, and the call function is a little different. My PHP is self taught and i never had a use for functions because i simply used PHP to take the place of Javascript to allow for increased comparability between browsers.

Float >.< have to scrounge around in my book to see why that actually helped so much.
 Ifrit.Kunihiro
Offline
サーバ: Ifrit
Game: FFXI
user: Kunihiro
Posts: 1
By Ifrit.Kunihiro 2010-02-28 22:25:29  
I'm not the best at C++, but I do believe it won't let you use the same variable twice because your arrays are not initialized.
 Asura.Muneonna
Offline
サーバ: Asura
Game: FFXI
user: Muneonna
Posts: 9
By Asura.Muneonna 2010-02-28 22:28:51  
Posting what worked. Actualy.. i just changed everything that had to do with the function to a float lol.

#include <iostream>
using namespace std;
float test(float evasion, float acc);
int main()
{
float evasion = 200;
float acc = 185;

float combo = test(evasion,acc);
cout << "Your percentage to dodge is " << combo << "%.\n\n";
system("PAUSE");
return 0;
}

float test(float evasion, float acc)
{
float total = evasion + acc;

float average = (evasion / total) * 100;

return average;
}
 Fairy.Xerxes
Offline
サーバ: Fairy
Game: FFXI
user: Xerxes
Posts: 11
By Fairy.Xerxes 2010-02-28 23:19:17  
When I compiled and ran each example, the result was the same. Scragg is correct, if you're expecting non-whole numbers for your output then you can't use int variables, unless you want to do the calc twice and use the modulo op in the 2nd calc. Float works, but I suggest using double in your code.


#include <iostream>
double test(double eva, double acc);
int main(){
double evasion = 200.0;
double accuracy = 185.0;
double combo = test(evasion, accuracy);
std::cout << combo << "\n\n";
system("PAUSE");
return 0;
}

double test(double eva, double acc) {
double total = eva + acc;
double average = 100;
double acc2 = acc;
// next few lines are my test code
if (eva == 200 && acc == 185 && average == 100) {
total = acc2 / total;
return total;
}
else {
std::cout << "FAIL";
system("PAUSE");
return 0.0;
}
}
 Asura.Muneonna
Offline
サーバ: Asura
Game: FFXI
user: Muneonna
Posts: 9
By Asura.Muneonna 2010-03-01 04:17:03  
[spoiler]This is the final code for the alpha for this program.
Quote:
#include <iostream>
#include <cstdlib>
#include <ctime>
// Prototype for percentage calc



double evaVsAcc(double eva, double acc);


int main(){
double evasion = 200.0;
double accuracy = 185.0;
double outputCTH = evaVsAcc(evasion, accuracy);

srand(time(0)); // Seed random number generator based on current time
int randomNumber = rand();
int hitOrMiss = (randomNumber % 100) +1;

bool endProgram = 0;
while (endProgram == 0){
std::cout << "Evasion Success Calculator\n\n";
std::cout << outputCTH << "% is your chance to evade this attacker.\n\n" << hitOrMiss << " is the random number generated.\n\n";
if (outputCTH > hitOrMiss) {
std::cout << "Attack Sucessfully Evaded!\n\n";
} else {
std::cout << "You've taken a hit!\n\n";
}
std::cout << "End Program?";
std::cin >> endProgram;
}


// std::cout << combo << "\n\n";
system("PAUSE");
return 0;
}

// Function to get Percentages

double evaVsAcc(double eva, double acc) {
double total = eva + acc;
double average = 100;
// next few lines are my test code
total = (acc / total) * 100;
total = round(total);
return total;
}

// Function to round variables

double round(double x)
{
if(x>=0.5){return ceil(x);}else{return floor(x);}
}

Last problems in my way are:

Making a simple, yet more random number generator. If i refresh the application i can watch the numbers go up... 1, 13, 24, 35, 56, 89, 99, 1, 15... ect. thats terribly timeable.

AND allowing the game to not come to an end when the code is run, and refreshing the random counter when i call the function. As it is it remains static if i press 0 to start the program again.[/spoiler]

test
Log in to post.