Forums

cobra code for adding numbers please help

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

cobra code for adding numbers please help

Postby basabz » Wed Jan 02, 2013 10:58 am

Hello im new to this language and i have searched many sites regarding on this, is there a way to add numbers using the cobra codes? if yes can someone give me an example thank you^^
basabz
 
Posts: 3

Re: cobra code for adding numbers please help

Postby Charles » Wed Jan 02, 2013 3:28 pm

class X

def main
trace 3 + 5
print 3 + 5
trace .add(3, 5)

def add(a as int, b as int) as int
return a + b

Sounds like you're new to programming. If so, you are probably better off starting with the Python language and a book like "Python Programming for the Absolute Beginner".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: cobra code for adding numbers please help

Postby basabz » Thu Jan 03, 2013 9:06 am

thanks for the reply im just a freshmen into programming and i just want to learn more in other languages and thanks for advice :)
basabz
 
Posts: 3

Re: cobra code for adding numbers please help

Postby basabz » Mon Jan 07, 2013 10:27 pm

but is it possible to add in interactive way like the user is asked for the first number then for the second number then the result appears after entering the 2 numbers :?:
basabz
 
Posts: 3

Re: cobra code for adding numbers please help

Postby Charles » Mon Jan 07, 2013 11:25 pm

You can get input with Console.readLine and you can change it from a string to an int with either int.parse or int.tryParse. You can search for these on google to see examples in C# or VB, or to read their reference documentation.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: cobra code for adding numbers please help

Postby jessica » Tue Jan 08, 2013 6:48 am

i have read the last posts and i have a problem about the Console.readline, i've also checked the reference of C# and VB
and it just gave me confusion because they have different syntax and it gave me errors. can you share me a cobra code about adding numbers with the use of Console.readline, int.Parse etc. so that it will ask the user on what numbers he/she wants to add? thank you so much
jessica
 
Posts: 3

Re: cobra code for adding numbers please help

Postby Charles » Tue Jan 08, 2013 1:30 pm

class Add

def main
print 'Enter two numbers:'
x = Console.readLine
y = Console.readLine
print 'sum is', int.parse(x) + int.parse(y)
print 'done.'

Code: Select all
Enter two numbers:
3
4
sum is 7
done.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: cobra code for adding numbers please help

Postby jessica » Tue Jan 08, 2013 7:10 pm

thank you so much i really needed this for my assigned report on programming.
jessica
 
Posts: 3

Re: cobra code for adding numbers please help

Postby hopscc » Tue Jan 08, 2013 9:02 pm

Other than giving you the code to do what you want
what would you have needed/wanted to see (in compiler, doc, website,..)
to make it easy/easier for you to work out what you needed to do yourself ??
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 101 guests

cron