Ch2 FruitLoopy program
by Matthew Wiggins · in Torque Game Engine · 01/03/2005 (2:15 pm) · 18 replies
I've been trying to get the FruitLoopy program to run but it keeps saying it is unable to find function main. I've tried everything from capitalizing function and main to tabbing it. Here is the code:
function Main()
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
%mangoIdx=3
%pearIdx=4
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "apples";
%names[%orangeIdx] = "oranges";
%names[%mangoIdx] = "mangos";
%names[%pearIdx] = "pears";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = 0.55;
%cost[%orangeIdx] = 0.55;
%cost[mangoIdx] = 1.90;
%cost[%pearIdx] = 0.68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
Here is the error report:
FruitLoopy.cs.bak
Compiling CH2/FruitLoopy.cs.bak...
CH2/FruitLoopy.cs.bak Line: 1 - Syntax error.
>>> Advanced script error report. Line 1.
>>> Some error context, with ## on sides of error halt:
Function Main(##)##
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
>>> Error report complete.
Executing CH2/FruitLoopy.cs.bak.
CH2/FruitLoopy.cs.bak Line: 1 - Syntax error.
>>> Advanced script error report. Line 1.
>>> Some error context, with ## on sides of error halt:
Function Main(##)##
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
>>> Error report complete.
CH2/run.cs (30): Unable to find function main
What is going on that it can't find function main when I clearly typed it in the first line?
function Main()
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
%mangoIdx=3
%pearIdx=4
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "apples";
%names[%orangeIdx] = "oranges";
%names[%mangoIdx] = "mangos";
%names[%pearIdx] = "pears";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = 0.55;
%cost[%orangeIdx] = 0.55;
%cost[mangoIdx] = 1.90;
%cost[%pearIdx] = 0.68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
Here is the error report:
FruitLoopy.cs.bak
Compiling CH2/FruitLoopy.cs.bak...
CH2/FruitLoopy.cs.bak Line: 1 - Syntax error.
>>> Advanced script error report. Line 1.
>>> Some error context, with ## on sides of error halt:
Function Main(##)##
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
>>> Error report complete.
Executing CH2/FruitLoopy.cs.bak.
CH2/FruitLoopy.cs.bak Line: 1 - Syntax error.
>>> Advanced script error report. Line 1.
>>> Some error context, with ## on sides of error halt:
Function Main(##)##
{
%numFruitTypes = 5
%bananaIdx=0
%appleIdx=1
%orangeIdx=2
>>> Error report complete.
CH2/run.cs (30): Unable to find function main
What is going on that it can't find function main when I clearly typed it in the first line?
#2
01/03/2005 (5:24 pm)
I typed it exactly what was in the book. After reading it over I really don't see where I missed any semicolons.
#3
FruitLoopy.cs.bak
Compiling CH2/FruitLoopy.cs.bak...
CH2/FruitLoopy.cs.bak Line: 31 - Syntax error.
>>> Advanced script error report. Line 61.
>>> Some error context, with ## on sides of error halt:
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{##
##
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index
>>> Error report complete.
Executing CH2/FruitLoopy.cs.bak.
CH2/FruitLoopy.cs.bak Line: 31 - Syntax error.
>>> Advanced script error report. Line 61.
>>> Some error context, with ## on sides of error halt:
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{##
##
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index
>>> Error report complete.
CH2/run.cs (30): Unable to find function main
It says theres an error on line 31, but line 31 has a bracket in it. So I have no idea whats going on. I recieved the same error message on Fruit.cs, but changed the F in function to lower case which fixed it. I do the same thing here and it still says it can't find the function main.
01/03/2005 (5:43 pm)
I fixed the error only to recieve a new error report:FruitLoopy.cs.bak
Compiling CH2/FruitLoopy.cs.bak...
CH2/FruitLoopy.cs.bak Line: 31 - Syntax error.
>>> Advanced script error report. Line 61.
>>> Some error context, with ## on sides of error halt:
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{##
##
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index
>>> Error report complete.
Executing CH2/FruitLoopy.cs.bak.
CH2/FruitLoopy.cs.bak Line: 31 - Syntax error.
>>> Advanced script error report. Line 61.
>>> Some error context, with ## on sides of error halt:
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
{##
##
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index
>>> Error report complete.
CH2/run.cs (30): Unable to find function main
It says theres an error on line 31, but line 31 has a bracket in it. So I have no idea whats going on. I recieved the same error message on Fruit.cs, but changed the F in function to lower case which fixed it. I do the same thing here and it still says it can't find the function main.
#4
function main()
{
%numFruitTypes = 5;
%bananaIdx=0;
%appleIdx=1;
%orangeIdx=2;
%mangoIdx=3;
%pearIdx=4;
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "apples";
%names[%orangeIdx] = "oranges";
%names[%mangoIdx] = "mangos";
%names[%pearIdx] = "pears";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = 0.55;
%cost[%orangeIdx] = 0.55;
%cost[%mangoIdx] = 1.90;
%cost[%pearIdx] = 0.68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
for (%index = 0; %index < %numFruitTypes; %index++)
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
01/04/2005 (12:50 am)
Compare the code you pasted above to this:-function main()
{
%numFruitTypes = 5;
%bananaIdx=0;
%appleIdx=1;
%orangeIdx=2;
%mangoIdx=3;
%pearIdx=4;
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "apples";
%names[%orangeIdx] = "oranges";
%names[%mangoIdx] = "mangos";
%names[%pearIdx] = "pears";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = 0.55;
%cost[%orangeIdx] = 0.55;
%cost[%mangoIdx] = 1.90;
%cost[%pearIdx] = 0.68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%pearIdx] = 2;
%numFruit=0;
%totalCost=0;
for (%index = 0; %index < %numFruitTypes; %index++)
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
#5
01/04/2005 (1:56 am)
Yeah, no semi-colon = sytax error.
#6
01/04/2005 (2:34 pm)
Where should the semicolons go in the code I typed? I thought they were supposed to be at the end.
#7
01/04/2005 (2:44 pm)
Wait I thought the For loop code was part of the comment section of the book. It's fixed now thanks.
#8
01/05/2005 (8:07 am)
I marked the missing parts in bold. You needed to add 6 semicolons, a percent sign and 3 for statements.
#9
//Fruit Loops .CS program, Variation of fruit.cs in the sense that ARRAYS are now used
function main()
{
%numFruitTypes = 5;
%bananaIdx=0;
%appleIdx=1;
%orangeIdx=2;
%mangoIdx=3;
%pearIdx=4;
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "Juicy Apples";
%names[%orangeIdx] = "Yummy Oranges";
%names[%mangoIdx] = "Mango?";
%names[%pearIdx] = "Pearly PEARS!";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = .55;
%cost[%orangeIdx] = .55;
%cost[%mango] = 1.90;
%cost[%pear] = .68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%mangoIdx] = 2;
%numFruit=0;
%totalCost=0;
//-------------------------Computation!-----------------------------------
// Display the known stats of fruit collection
for (%index = 0; %index <= %numFruitTypes; %index++)
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
//-------------Calculate Total Cost-----------------
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
01/05/2005 (1:25 pm)
I also have a problem with that code except for the fact that mine says cannot find function main. Heres the code://Fruit Loops .CS program, Variation of fruit.cs in the sense that ARRAYS are now used
function main()
{
%numFruitTypes = 5;
%bananaIdx=0;
%appleIdx=1;
%orangeIdx=2;
%mangoIdx=3;
%pearIdx=4;
%names[%bananaIdx] = "bananas";
%names[%appleIdx] = "Juicy Apples";
%names[%orangeIdx] = "Yummy Oranges";
%names[%mangoIdx] = "Mango?";
%names[%pearIdx] = "Pearly PEARS!";
%cost[%bananaIdx] = 1.15;
%cost[%appleIdx] = .55;
%cost[%orangeIdx] = .55;
%cost[%mango] = 1.90;
%cost[%pear] = .68;
%quantity[%bananaIdx] = 1;
%quantity[%appleIdx] = 3;
%quantity[%orangeIdx] = 4;
%quantity[%mangoIdx] = 1;
%quantity[%mangoIdx] = 2;
%numFruit=0;
%totalCost=0;
//-------------------------Computation!-----------------------------------
// Display the known stats of fruit collection
for (%index = 0; %index <= %numFruitTypes; %index++)
{
print("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
print("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
}
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%numFruit = %numFruit + %quantity[%index];
}
print("Total pieces of Fruit:" @ %numFruit);
//-------------Calculate Total Cost-----------------
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
}
#10
Also, the first for loop should contain "<" not "<=" in the comparison between the %index and %numFruitTypes variables. In other words, you have added an equal sign which should not be there.
01/05/2005 (5:57 pm)
You seem to have omitted some leading zeroes when defining the values for the prices.Also, the first for loop should contain "<" not "<=" in the comparison between the %index and %numFruitTypes variables. In other words, you have added an equal sign which should not be there.
#11
01/05/2005 (8:10 pm)
Thanks! What do the zeros do in front of the values?
#12
01/08/2005 (3:15 pm)
Quick question, i noticed in some functions that the "{ and }" signs sometimes are used and sometimes arent, if they are used for separating different parts of a function, why do you need them? Essentially it will execute them the same then...?
#13
In long...
{} are used to mark beginning and end of functions, conditionals, and various loops.
In C++, if you declare a local variable inside of {}, then that variable will disappear after you get out of the {}. I'm not sure if something similar is true in torquescript, which is based on C++.
Ken Finney seems to do this a lot. I think he's trying to say that all the lines inside this set of {} have a purpose and all the ones in this set of {} have another purpose. Putting {} around one line of code looks very silly to me though.
01/08/2005 (5:23 pm)
Bryan, in short, yes, this program includes a lot of {} that are completely useless. They have no effect on how the code works.In long...
{} are used to mark beginning and end of functions, conditionals, and various loops.
In C++, if you declare a local variable inside of {}, then that variable will disappear after you get out of the {}. I'm not sure if something similar is true in torquescript, which is based on C++.
Ken Finney seems to do this a lot. I think he's trying to say that all the lines inside this set of {} have a purpose and all the ones in this set of {} have another purpose. Putting {} around one line of code looks very silly to me though.
#14
If you just add them automatically, then there is nothing to worry about.
01/08/2005 (5:55 pm)
Its just good programming style to put the brackets after every for and if statement, even if i t is one line. Its really hard to find when you add another line to the statement, forgetting there were no brackets and so the line gets called all the time.If you just add them automatically, then there is nothing to worry about.
#15
print("Total pieces of Fruit:" @ %numFruit);
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
This book does it a lot. I see 3 examples in the code block shown in this thread's first post. Totally useless brackets that aren't related to one-line conditionals or loops. Not that they hurt anything... it just looks weird.
01/08/2005 (7:07 pm)
When I mentioned silly looking brackets around just one line, I wasn't talking about for loops. Check out these lines of frootloopy:print("Total pieces of Fruit:" @ %numFruit);
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
This book does it a lot. I see 3 examples in the code block shown in this thread's first post. Totally useless brackets that aren't related to one-line conditionals or loops. Not that they hurt anything... it just looks weird.
#16
In other words, you're making the same mistake as many others in this thread by thinking that comments are spread over more than one line and are therefore omitting important lines of executable/parsable code.
The full code is:-
print("Total pieces of Fruit:" @ %numFruit);
// now calculate the total cost
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
Only "// now calculate the total cost" can safely be omitted without affecting the way the script runs.
As for the other brackets in the assignment statement, the rounded ones are used to ensure that the calculations are made in a particular order, and the square ones are used to indicate array indices.
01/09/2005 (6:44 am)
The code you have quoted is incomplete. Between the print statement and the assignment statement is a for statement.In other words, you're making the same mistake as many others in this thread by thinking that comments are spread over more than one line and are therefore omitting important lines of executable/parsable code.
The full code is:-
print("Total pieces of Fruit:" @ %numFruit);
// now calculate the total cost
for (%index = 0; %index <= %numFruitTypes; %index++)
{
%totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
}
print("Total Price of Fruit:$" @ %totalCost);
Only "// now calculate the total cost" can safely be omitted without affecting the way the script runs.
As for the other brackets in the assignment statement, the rounded ones are used to ensure that the calculations are made in a particular order, and the square ones are used to indicate array indices.
#17
I was looking only at post #1 to figure out what he meant by useless {}. Your version that appears further down makes much more sense.
01/09/2005 (8:25 am)
Okay, agreed :)I was looking only at post #1 to figure out what he meant by useless {}. Your version that appears further down makes much more sense.
#18
%index = 0; <--- Starting point in the index ?
%index < %numFruitTypes; <--- Ending point in the index ?
%index++ <--- Increment values through the index (Increment by 1) ?
Is that right ?
Also further to that. Could someone explain where the script knows this is an array?
I see declarations of values for variables. But nothing specific to define that the variable(s) are in an array ?
I really like to full understand it before I continue.
03/27/2005 (6:45 pm)
I need some help with sytax... With the syntax of the array. As I understand it:%index = 0; <--- Starting point in the index ?
%index < %numFruitTypes; <--- Ending point in the index ?
%index++ <--- Increment values through the index (Increment by 1) ?
Is that right ?
Also further to that. Could someone explain where the script knows this is an array?
I see declarations of values for variables. But nothing specific to define that the variable(s) are in an array ?
I really like to full understand it before I continue.
Torque Owner Wysardry