You can add, subtract, multiply, and divide, here are some basic examples:


Code:

Adding:
int a = 1;
int b = 2;

int c = a + b;

int d = b - a;

int e = b * a; //I'm no expert, but perhaps in calculator C there is a different function for this, as pointers may act weirdly?

int f = b / a;


Here are some other shortcuts you can take:

Code:

int i = 0;
i++; //Increments i by 1
i--; //decrements i by 1

i += 2; // adds 2 to i
i -= 2; // subtracts 2 from i

i = 10; //sets i to 10

i *= 10; //multiplies i by 10
i /= 10; //divides i by 10


in conditional statements, there are some other operators:

Code:

int foo = 1;
if (i == 10 || foo == 2) //if i is equivalent to 10 or foo is equal to two
{
   foo = 1; //set foo to 1
}

if (i == 5 && foo == 1) //if i is equal to 10 and foo is equal to one
{
   i = 0; // set i to 0
}

if (i == 5 %% foo != 1) //if i is equal to 10 and foo is NOT equal to one
{
   foo = 10; // set foo to 10
}


I think I covered all of the basics.
Also familiarize yourself with data types - you won't want to use int for everything, if you look at some C programs there will be uint8_t and such.

https://www.tutorialspoint.com/cprogramming/c_data_types.htm

Also don't get into bad habits and have all your variables as globals.
Unicorn wrote:
Also familiarize yourself with data types - you won't want to use int for everything, if you look at some C programs there will be uint8_t and such.

https://www.tutorialspoint.com/cprogramming/c_data_types.htm

Also don't get into bad habits and have all your variables as globals.



Thanks! Also, I didn't plan on doing all of my variables as global but in this case, it makes it easier.

Next question:

How do I make something that accepts input? Because my end goal is to have a person put in a number ranging from 2-8(for number of players), but I have yet to find something that allows me to input anything in C. Is there a equivalent to the "Prompt" or "Input" command in BASIC?


Or is there a way I can read String0 in C and store it to a global variable? Because all I need this for is a number, which will be used for how many players are playing.




Code:
 while (!(key = os_GetCSC)) switch(key){sk_2:break;sk_3:break;sk_4:break;sk_5:break;sk_6:break;sk_7:break;sk_8:break;}



Would this work?

Also, how do I read from a buffer?
All right then. Now that I have my previous question answered, I wan to write a loop.

Here's the BASIC equvalent:

Code:

8000->X:8->A
While A!=0
Text(10,10*A,"Player",X)
A-1->A
End


This will create a column of players, all with there respective life totals. At least, that's what I want.

X in this case is their life total, and A is the player number.

How would I do this in C? Or is it as simple as I put it up there? And would a do-while loop work for this?


EDIT: Never mind. PT_ helped me out. Now for the hard part... Making the life totals manipulable. Which I can do. And when that is done, I'll need beta testers...... Any volunteers?


REQ. FOR TESTING:
Must have a CE
Must have the latest C libs installed

Optional:
Have Cesium or something that can backup programs.


Volunteers so far:
_iPhoenix_
I'll beta test. If it crashes my calc it will give me an excuse to sort through my memory.
_iPhoenix_ wrote:
I'll beta test. If it crashes my calc it will give me an excuse to sort through my memory.


Thanks, iPhoenix!

Anyone else?
Caleb_J wrote:
_iPhoenix_ wrote:
I'll beta test. If it crashes my calc it will give me an excuse to sort through my memory.


Thanks, iPhoenix!

Anyone else?


My neighbor is in the same advanced math class (there's a program at my school that lets people skip several grades in a subject if they have shown that they can handle it) as me (which means that we both have graphing calculators, although he hides his, publicly denies that he has it, and downright tries to seem normal.), so he could actually test it with me.

He might be in Europe right now on vacation, though, I can check.
Code so far:

Code:

#include <tice.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graphx.h>
#include <keypadc.h>
#include <debug.h>
#include <fileioc.h>
int playerOne=8000;
int playerTwo=8000;
int playerThree=8000;
int playerFour=8000;
int playerFive=8000;
int playerSix=8000;
int playerSeven=8000;
int playerEight=8000;
int quitVar=0;
int a
typedef uint8_t sk_key_t
void main() {
gfx_Begin(gfx_8bpp);
gfx_FillScreen(gfx_blue);
gfx_SetTextXY(150,150
gfx_PrintScreenXY("Input how many people are playing:",5,15)
}
while (!key = os_GetCSC()); //selects how many people are playing
switch (key) {
    case sk_2:
        playerVar=2;
        break;
    case sk_3:
        playerVar=3;
        break;
    case sk_4:
      playerVar=4
      break
   case sk_5
      playerVar=5
      break
   case sk_6
      playerVar=6
      break
   case sk_7
      playerVar=7
      break
    case sk_8:
        playerVar=8;
        break;
    default:
        return ERROR;
}
char buf[20]; // displays the people playing
    uint24_t x = 8000;
do {
sprintf(buf, "Player %u", x)
gfx_PrintStringXY(buf, 10, playerVar*10);
while (playerVar>=1);}
while (quitVar=0) {
sk_key_t=(os_GetCSC); // actual program, where I will be manipulating life totals
if (os_GetCSC()==sk_Clear) {
quitVar=1;} //exits the program
If (sk_key_t=sk_1)}// beginning of life total manipulation for player 1




os_ClrHomeFUll()
gfx_End();
prgm_CleanUp();
exit(0);



So far, so good.

But how do I manipulate life totals?

Currently what I am thinking is for a player to press keys 2-8 to change that player's life total. Then the + or - button to add or subtract life totals. then I want to have something that has (for now) something like a Goto/Label so I can make my program simpler. Is there a command that does the equivalent of that?

EDIT: Beta testers, you should be getting the first copy of the program soon(ish). Probably within a week or so.
What about this?

Code:
if (sk_key_t == sk_1) {
KeyNotFound:
    while (!key = os_GetcSC());
    if (key == sk_Min) {
        playerOne -= 100;
    else if (key == sk_Plus) {
        playerOne += 100;
    } else {
        goto KeyNotFound;
    }
}


Also, pleassseeeeeee, be sure to compile your program everytime you change something. I'm 100% this program doesn't work, so please fix all the current bugs before adding new stuff. How do you even test your program?
Thanks, PT_!

Also, I am using TI-planet's Project Builder to compile.

Although it has been a while since I've compiled.... I'll do that now.

/me goes and compiles code


Uh oh.....

I got a LOT of errors. I went and fixed as many as I could, but there's still a bunch of them.

Here they are:


Code:
gfx_PrintScreenXY("Input how many people are playing:",5,15)}
↑ No function prototype "gfx_PrintScreenXY" in scope

What does this mean? Is it just because the compiler doesn't recognize it?

Code:
while (!key = os_GetCSC()); //selects how many people are playing
↑ Expression must be an lvalue

What's an lvalue?

Code:
 default:
        return ERROR;
↑ Identifier "ERROR" not defined within current scope

Ummm.....

Code:
}
↑ Cannot return a value from a function returning "void"

What does this mean? And why does it need to return a value?

Code:
char buf[20]; // displays the people playing
↑Syntax error

Am I not supposed to have a ";" there?

Code:
uint24_t x = 8000;
↑ Syntax error

???

Code:
do {
sprintf(buf, "Player %u", x)
↑ Identifier "x" not defined within current scope

Probably related to the error above this piece of code.

Code:
gfx_PrintStringXY(buf, 10, playerVar*10);
↑ Syntax error

Why am I getting so many syntax errors?

Code:
while (quitVar=0) {
↑ Missing "56" detected

Am I supposed to have something else here? Like, maybe a colon?

Code:
sk_key_t=(os_GetCSC);
↑ Syntax error

Another one? Pretty sure it doesn't have to do with the semicolon this time, either.

Code:
if (sk_key_t == sk_1) {
↑ Identifier "sk_key_t" is not a variable or enumeration constant name

Does this mean I forgot to declare it? (probably)

Code:
while (!key = os_GetcSC());
↑ Expression must be an lvalue

Already asked.

Code:
if (key == sk_Min) {
↑ Identifier "sk_Min" not defined within current scope


Code:
else if (key == sk_Plus) {
↑ Syntax error


Code:
gfx_End();
↑ Missing "56" detected


Code:
exit(0);
↑ Syntax error



Can someone please help me fix these errors and possibly explain why they happened so I don't get these errors again?

EDIT:
Current state of code is here
For the record: all compile errors have been worked out over IRC.
Yay! I did it!


NOTE: I forgot to add something in my readme, so here it is:


Use the + and - keys to edit life total. To select the player to edit, press keys 1-8.
So:


A few problems:

1) When I put in how many people are playing, say 5, only that player will show. in this case, only player 5 will show. However, I can fix that with a for() loop.

And I'm not sure, but the adding/subtracting life does not work.


For the display:

Code:
for( playerVar=playerVar; playerVar>=1;playerVar=playerVar-1){       
gfx_PrintStringXY(buf, 10, playerVar * 10);
    }


Would that work? I'm pretty sure it would.

Fixed (I think) code:

Code:
#include <tice.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graphx.h>
#include <keypadc.h>
#include <debug.h>
#include <fileioc.h>
int playerOne   = 8000;
int playerTwo   = 8000;
int playerThree = 8000;
int playerFour  = 8000;
int playerFive  = 8000;
int playerSix   = 8000;
int playerSeven = 8000;
int playerEight = 8000;
int playerVar;
int quitVar;
int key;
int i;
char buf[20];
uint24_t x = 8000;

void main()
{
    gfx_Begin(gfx_8bpp);

    gfx_FillScreen(gfx_blue);
    gfx_SetTextXY(150, 150);
    gfx_PrintStringXY("Input how many people are playing:", 5, 15);


   while (!(key = os_GetCSC()))
        ; // selects how many people are playing
    while (playerVar == 0)
    {
        switch (key)
        {
            case sk_2:
                playerVar = 2;
                break;
            case sk_3:
                playerVar = 3;
                break;
            case sk_4:
                playerVar = 4;
                break;
            case sk_5:
                playerVar = 5;
                break;
            case sk_6:
                playerVar = 6;
                break;
            case sk_7:
                playerVar = 7;
                break;
            case sk_8:
                playerVar = 8;
                break;
        }
    }

    do
    {
        sprintf(buf, "Player %u", x);
for(i = playerVar; i >=1; i--){
    gfx_PrintStringXY(buf, 10, i * 10);
}
Begin:
while (!(key = os_GetCSC()));

    if (key == sk_Clear)
    {
quitVar=2;
gfx_End();
        exit(0);
    }

    if (key == sk_1)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerOne -= 100;
            }
            else if (!(key == sk_Add))
            {
                playerOne += 100;
            }
        }
    }

    if (key == sk_2)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerTwo -= 100;
            }
            else if (!(key == sk_Add))
            {
                playerTwo += 100;
            }
        }
    }
if (key == sk_3)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerThree-=100;
        }
            else if (!(key == sk_Add))
            {
                playerThree+= 100;
            }
        }
    }
if (key == sk_4)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerFour-=100;
        }
        else if (!(key == sk_Add))
        {
            playerFour+=100;
        }
    }
}
if (key == sk_5)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerFive-=100;
        }
        else if (!(key == sk_Add))
        {
            playerFive+=100;
        }
    }
}
if (key == sk_6)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerSix-=100;
        }
        else if (!(key == sk_Add))
        {
            playerSix+=100;
        }
    }
}
if (key == sk_7)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerSeven-=100;
        }
        else if (!(key == sk_Add))
        {
            playerSeven+=100;
        }
    }
}
if (key == sk_8)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerEight-=100;
        }
        else if (!(key == sk_Add))
        {
            playerEight+=100;
        }
    }
}
    }
}
If (quitVar!=2){
Goto Begin
}


What do you people think? Should I keep the quitVar? Or is it completely unnecessary? And would it even work and return you to the key selection for life manipulation?
Are you intentionally shadowing a variable? The more canonical way to represent that particular piece of code is, by the way:
Code:
for(i = playerVar; i >=1; i--){       
    gfx_PrintStringXY(buf, 10, i * 10);
}
Also, wouldn't you need to pull strings out of different locations that just buf all the time?
No, not really, KermMartian. I'm only displaying one string, followed by a changing number, and in a different spot every time, so i think the buffer is all I really need.



EDIT: 600th post. Yay.



Current state of code:
https://tiplanet.org/pb/?id=143220_1500556935_91055892a7

Unfortunately, I can't save it, because the pB is being really mean, and deciding to not show a scroll bar. So this might not work after I close it out. Which is why I'm going to do this:

Code:

#include <tice.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graphx.h>
#include <keypadc.h>
#include <debug.h>
#include <fileioc.h>

int playerOne   = 8000;
int playerTwo   = 8000;
int playerThree = 8000;
int playerFour  = 8000;
int playerFive  = 8000;
int playerSix   = 8000;
int playerSeven = 8000;
int playerEight = 8000;
int playerVar;
int i;

int quitVar;
int a;
int key;

typedef uint8_t sk_key_t;
char buf[20];
uint24_t x = 8000;

void main()
{
    gfx_Begin(gfx_8bpp);

    gfx_FillScreen(gfx_blue);
    gfx_SetTextXY(150, 150);
    gfx_PrintStringXY("Input how many people are playing:", 5, 15);

    while (!(key = os_GetCSC()))
        ; // selects how many people are playing
    while (playerVar == 0)
    {
        switch (key)
        {
            case sk_2:
                playerVar = 2;
                break;
            case sk_3:
                playerVar = 3;
                break;
            case sk_4:
                playerVar = 4;
                break;
            case sk_5:
                playerVar = 5;
                break;
            case sk_6:
                playerVar = 6;
                break;
            case sk_7:
                playerVar = 7;
                break;
            case sk_8:
                playerVar = 8;
                break;
        }
    }
        sprintf(buf, "Player %u", x);
        for(i = playerVar; i >=1; i--){
    gfx_PrintStringXY(buf, 10, i * 10);
}
    while (key != sk_Clear)) {
    while (!(key = os_GetCSC())){

    if (key == sk_Clear)
    {
        gfx_End();
        exit(0);
    }

    if (key == sk_1)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerOne -= 100;
            }
            else if (!(key == sk_Add))
            {
                playerOne += 100;
            }
        }
        gfx_SetTextXY(10,10);
        gfx_PrintInt(playerOne,4);
    }

    if (key == sk_2)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerTwo -= 100;
            }
            else if (!(key == sk_Add))
            {
                playerTwo += 100;
            }
        }
        gfx_SetTextXY(10,20);
        gfx_PrintInt(playerTwo,4);
    }
    if (key == sk_3)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerThree-=100;
            }
            else if (!(key == sk_Add))
            {
                playerThree+= 100;
            }
        }
        gfx_SetTextXY(10,30);
        gfx_PrintInt(playerThree,4);
    }
    if (key == sk_4)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerFour-=100;
            }
            else if (!(key == sk_Add))
            {
                playerFour+=100;
            }
        }
        gfx_SetTextXY(10,40);
        gfx_PrintInt(playerFour,4);
    }
    if (key == sk_5)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerFive-=100;
            }
            else if (!(key == sk_Add))
            {
                playerFive+=100;
            }
        }
        gfx_SetTextXY(10,50);
        gfx_PrintInt(playerFive,4);
    }
    if (key == sk_6)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerSix-=100;
            }
            else if (!(key == sk_Add))
            {
                playerSix+=100;
            }
        }
        gfx_SetTextXY(10,60);
        gfx_PrintInt(playerSix,4);
    }
    if (key == sk_7)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerSeven-=100;
            }
            else if (!(key == sk_Add))
            {
                playerSeven+=100;
            }
        }
        gfx_SetTextXY(10,70);
        gfx_PrintInt(playerSeven,4);
    }
    if (key == sk_8)
    {
        while (!(key = os_GetCSC()))
        {
            if (key == sk_Sub)
            {
                playerEight-=100;
            }
            else if (!(key == sk_Add))
            {
                playerEight+=100;
            }
        }
        gfx_SetTextXY(10,80);
        gfx_PrintInt(playerEight,4);
    }
        key=os_GetCSC();
    }
    }
}


There. Now everybody can see it.

Finally! I (think) I'm done.

What do you people think?
Just a copy/paste from IRC:
Quote:
20:21:22 <Adriweb> Caleb_J: "Updated: 20/07/2017 16:41:49"
20:21:29 <Adriweb> ie, the same time as the last time I checked
20:21:35 <Adriweb> so, the server didn't lose anything at all
20:21:44 <Adriweb> and, indeed, the content is still here for me
20:22:08 <Adriweb> so it might actually just be a display issue on your side.
20:22:28 <Adriweb> so I'm not sure I can do something about that
20:22:50 <Adriweb> if it's stuck in the loading phase, maybe I could superimpose some "Loading" overlay
20:22:57 <Adriweb> at least we'd know.

Also, what's the "scroll bar" issue?

Anyway, your while above the initial switch construct will inifinite-loop if the use hasn't pressed a specific key in the set you check. You probably want to call os_GetCSC() again.
Also, there is some indenting weirdness, but that'll be fixable "automatically" when I add AStyle to the PB, which should happen in a few hours max.
The scroll bar issue is that for some programs will not scroll up and down. Like, the scroll bar isn't there. but for other programs, it will scroll.

Also, where should I put another GetCSC? I already added one at the end of one loop; do I need to add it anywhere else?

Also, the last code posted is the most up-to-date code thus far. What is there is what I'm downloading to my calc.



EDIT: Test results

So... Good news and bad news.

Bad news: The program exits right after the For loop. I think i know how to fix it though.
Good news: The for loop works. it actually displays the players like it should.

For fixing the While loop.... I think it's the first loop it hits, the one that checks for the Clear button for when to quit. Not sure though. Please post what you think, as I'm not really sure.


EDIT: The above problem has been fixed. However, the life manipulation doesn't work. I'll see what I can do to fix that.
So:

After looking at the code extensively, I think I've found why life manipulation doesn't work.


Code:

if (key == sk_6)
{
    while (!(key = os_GetCSC()))
    {
        if (key == sk_Sub)
        {
            playerSix-=100;
        }
        else if (!(key == sk_Add))
        {
            playerSix+=100;
        }
    }
}


As soon as a key is hit, I think it exits out of the loop, instead of adding/subtracting life. Is this what it does? Because if not, I'm out of ideas for this.
Rather than just giving you the fish (of which there are two here), I'll try my best to try to convey the knowledge of how to catch the fish.

It is important to be able to examine a conditional control statement and fully understand what it does. There are two aspects to understand: the condition itself, and how execution will continue in all of the possible conditions.

Consider the while loop, for instance. The condition states something like: "read a key, save it, and check if it is zero". This is a while loop, so execution is as follows:
  • If this condition is true (the key is zero), the body (the life manipulation code) is executed and the condition is reevaluated.
  • If the condition is false (the key is nonzero), the body (the life manipulation code) is skipped.
For each condition possible, you need to consider if the combination of condition and subsequent execution is appropriate. If not, alter the condition statement, the control statement/body structure (hint: this is the culprit in the while loop case), or both.
So, I figured out the problem. It's both the loop and the executable. I need to change it so that when (in this case) key 6 is pressed, it waits for input, and then executes the commands if it's a plus or minus.

However, I have no idea on how to make it wait for a plus or minus. Although I'll take a look in the toolchain, it may be contained there.

I'll also add in a condition that if the key is neither plus or minus, it exits. Not the entire program, but rather the loop it's currently in, so that if a user pressed the wrong key, they can get out of what would other wise be a infinite loop waiting for a + or - .

EDIT:

Code:
if (key == sk_6)
{
    while (!(key = os_GetCSC()))
    {
        while (!(key == os_GetCSC()))
{
os_GetCSC=key;
}
if (key == sk_Sub)
        {
            playerSix-=100;
        }
        else if (!(key == sk_Add))
        {
            playerSix+=100;
        }
    }
}


This would create a loop that waits for a keypress, but once a key is pressed, stores it to key, and if it's neither a plus or minus, it automatically exits. At least, that's what I think it does.


EDIT: I compiled the code. Guess what? An error.

Code:

gfx_Begin(gfx_8bpp);
↑ Argument type is not compatible with formal parameter
             ↑ error : too many arguments to function call, expected 0, have 1

How can I fix this?


EDIT:

Above problem is fixed.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement