cint

cint university / 101 / ced, the cint editor

101-03: names for values

This is lesson 101-03 of CU-101.2, checked against cint 0.2.0.

You will give a number a name, change it as the program runs and count money exactly.

the program

// A jar of coins, counted in cents.
I64 jar = 0;
jar += 125;
"put in 125, jar holds {jar}\n";
jar += 240;
"put in 240, jar holds {jar}\n";
jar -= 15;
"took out 15, jar holds {jar}\n";
I64 dollars = jar / 100;
I64 cents = jar % 100;
"that is {dollars} dollars and {cents} cents\n";

Run this program in ced

Run it:

put in 125, jar holds 125
put in 240, jar holds 365
took out 15, jar holds 350
that is 3 dollars and 50 cents

what happened

I64 jar = 0; makes a variable. A variable is a named place that holds a number. This line does three things: I64 says what kind of number the place holds, jar is its name and = 0 puts 0 in it to start. Until lesson 101-04 explains it, read I64 as "a whole number, which may be negative".

A variable's number can change. jar += 125; means "add 125 to what jar holds". After it, jar holds 125. jar -= 15; takes 15 away. You can also write the long form, jar = jar + 125;: work out the right side, then put the answer in the variable on the left. This is called an assignment, and the = sign here means "put", not "is equal to".

A name in a hole prints its number. {jar} prints whatever jar holds at that moment, so the same hole prints 125, then 365, then 350 as the program runs.

Money is counted in cents. The jar holds 350 cents, not 3.50 dollars, because cint counts in whole numbers. Lesson 101-02's / and % turn cents into dollars and cents: 350 divided by 100 is 3, with 50 left over. Many programs that handle money count this way, in the smallest unit, because a whole number of cents is always exact. Many other languages store an amount such as 0.10 as the nearest fraction a computer can hold, which is close to 0.10 but not equal to it.

Names are yours to choose. A name starts with a letter and may hold letters, digits and the underscore _. Pick names that say what the number is: jar, dollars, cents.

change it

  1. Start the jar at 1000 cents instead of 0. What does the last line print?
  2. Add a line that puts in 99 more cents, and a line after it that prints the jar.
  3. Add a variable quarters that holds how many whole quarters (25 cents each) the jar could be changed into.
what the last line prints after the first change
that is 13 dollars and 50 cents

when it goes wrong

Names must match exactly, capital letters included. Here the last line says Jar instead of jar:

I64 jar = 0;
jar = jar + 10;
"{jar}\n";
"{Jar}\n";

Run this program in ced

error[C3005]: undefined or unusable name
  --> jar.ci:4:3
   |
 4 | "{Jar}\n";
   |   ^
   |

To cint, Jar and jar are two different names, and no variable is called Jar. You get the same message if you use a name before the line that makes it, or leave out the I64 in that line.

new words

variable, name, assignment

Next: lesson 101-04, numbers have sizes.