Prompt for variable?

Pixilang programming language
Post Reply
User avatar
The Handle
Posts: 186
Joined: Wed Sep 07, 2011 5:11 am

Prompt for variable?

Post by The Handle »

Is there a way to prompt a user for a value (they enter the value using their keyboard)? I was thinking of making a program that calculates torque, moments of inertia, etc., but I didn't see anything in the list of commands. I've done it with excel, but I only have so much space on my hard drive and I'd like to save space where possible (its over 100KB).

Also, can it perform equations with exponents?

(To provide some background info, I've messed around with batch files in Notepad, have some experience with excel and very basic knowledge of Visual Basic)
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Prompt for variable?

Post by NightRadio »

Of course. Please check this example: examples/console/text_input.pixi
But it works in the console mode only.
Also use the str2num() function to convert the resulted string to number.

List of all Pixilang math functions is here: http://code.google.com/p/pixilang/wiki/ ... thematical
It is not finished yet, so you can read detailed description of any math function here: http://en.wikipedia.org/wiki/C_mathematical_functions
User avatar
The Handle
Posts: 186
Joined: Wed Sep 07, 2011 5:11 am

Re: Prompt for variable?

Post by The Handle »

I was on 1.6, which would explain why I didn't have the console version. I looked at some of the examples and read through the commands (couldn't be thorough because I have class soon, I'll read more tonight), and here are my questions:

-Could you go into more detail on what you're doing in the text_input example, as well as how to use the str2num command? I was able to get the prompts to show, but not the result. I understand there's a difference between strings and numbers, but I don't know what it is.

-I also need to use decimals, so how do I tell Pixilang to use 32bit float? Would you use the "new" command for that, or is that for creating modules?

I am a visual learner, so seeing the commands in use (along with comments regarding what you're telling Pixilang to do) helps a lot.
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Prompt for variable?

Post by NightRadio »

Let me show you the example with comments

Code: Select all

STR = new( 256, 1, INT8 ) //Create the new container for the text string
printf( "Please enter some numeric value and press Enter:\n" ) //Show the question
fgets( STR, 256, STDIN ) //Read the string entered by user and put it to the STR container
NUM = str2num( STR ) //Convert the entered text string to the numeric value and put this value to the NUM variable
//Ok. Now the number you asked is in the NUM variable.
//Lets print this number:
printf( "The value you entered is %f\n", NUM )
User avatar
The Handle
Posts: 186
Joined: Wed Sep 07, 2011 5:11 am

Re: Prompt for variable?

Post by The Handle »

Alright thanks man! Starting to understand how this works.

Here is what I have now (my interpretations of the commands are in the comments).
What I want it to do is prompt the user for variables A and B, display the value they entered, Add them together, then store and display the result as C.

Code: Select all

//creates new container with int8 format, size of 256 wide by 1 tall (not sure what the size means)
STR = new( 256, 1, int8 )
//prompts user with print, not sure what \n does
printf( "Enter a number for A:\n" )
//stores number in the str container, not sure what the 256 in the middle means (manual shows parameter format as (s,n,stream))
fgets( STR, 256, STDIN )
//converts str to number and store as variable A
A = str2num( STR )
//tells user what they entered for A, not sure what %f\n does
printf( "The value you entered is %f\n", A )
//prompts user with print, not sure what \n does
printf( "Enter a number for B:\n" )
//stores number in the str container
fgets( STR, 256, STDIN )
//converts str to number and store as variable B
B = str2num( STR )
//tells user what they entered for B, not sure what %f\n does
printf( "The value you entered is %f\n", B )
//THIS IS WHERE I HAVE PROBLEMS WITH MATH FUNCTIONS IN PIXILANG
//Adds A to B and stores as str
str=a+b
//converts str to number and stores as C
c=str2num(str)
//tells user the result C, not sure what %f\n does, put it in there because it seemed to work for the above print commands
printf("Here is your result %f\n",c)
So in summary, I don't understand....
-what %f\n and \n do
-how to keep the exact number the user entered (36.1 would be displayed as 36.099998, I want it to keep the decimal places given)
-format of the fgets/fputs commands
-what the size of a container means
-terminology in pixilang/programming-in-general (what is a stream? string? etc., and how are they used?)
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Prompt for variable?

Post by NightRadio »

-what %f\n and \n do
-how to keep the exact number the user entered (36.1 would be displayed as 36.099998, I want it to keep the decimal places given)
It is all here:
http://www.cplusplus.com/reference/clib ... io/printf/
or here
http://pubs.opengroup.org/onlinepubs/00 ... rintf.html

Functions printf, fputs, fgets are full analogs of the same C/C++ functions.
-format of the fgets/fputs commands
http://www.cplusplus.com/reference/clib ... dio/fputs/
http://www.cplusplus.com/reference/clib ... dio/fgets/
-what the size of a container means
Number of elements of the container.
a = new( 32, 32, INT8 ) //New container - 2D array of size 32x32 elements. Each element - 8bit integer (INT8)
terminology in pixilang/programming-in-general (what is a stream? string? etc., and how are they used?)
String - container of any size with INT8 elements.
Stream - stream of bytes. You can write to the stream, or read from the stream. There are standard streams: STDIN - text input (linked to your keyboard); STDOUT - text output, send data to this stream to see it on the screen; STDERR - send any error messages to this stream. Also you can associate the stream with some file through the fopen() function.
Also you can read something here: http://code.google.com/p/pixilang/wiki/ ... ual#Basics
Post Reply