views:

83

answers:

3

Here is my code:

var num, totalNum, averageNum, numItems, msg;
msg = "";
totalNum = 0;
numItems = 0;
while(true)
{
   num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
   totalNum += num;
   numItems += 1;
   if (num == -99)
   {
   totalNum += 99;
   msg += "Total is: " +string(totalNum) +"#";
   averageNum = real(totalNum / numItems);
   msg += "Average is: " +string(averageNum);
   show_message(msg);
   break;
   }
}

Right now, if I enter the number 1 and 2 and then -99 to break the loop, I get an average of 1. I want to get the real number, not the integer. Any help?

A: 

I don't know GML but at the moment, you seem to be converting the result of the division to a real, and by this time it is too late.

I have revised your program, making some other improvements. Forgive me if it's not valid GML.

var num, totalNum, averageNum, numItems, msg;
totalNum = 0;
numItems = 0;
num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
while(num != -99)
{
    totalNum += num;
    numItems += 1;
    num = get_integer("Please enter a number. Enter -99 to quit.", "type here");
}
msg = "Total is: " + string(totalNum) + "#";
averageNum = real(totalNum) / numItems;
msg += "Average is: " + string(averageNum);
show_message(msg);
Artelius
A: 

I don't know how Game Maker Language works.

However, try replacing averageNum = real(totalNum / numItems); with averageNum = real(totalNum) / numItems;

Arriu
+2  A: 

2 possible corrections:

...
if (num == -99)
{
totalNum += 99;
numItems -= 1; // <----
msg += "Total is: " +string(totalNum) +"#";
averageNum = real(totalNum) / numItems; // <----
...
Nick D