I know the basics of vanilla c, but converting this compounded interest calculator over to the ti84 in ce proves to be difficult.

here's the code :

Code:
#include <stdio.h>
#include <math.h>

int main ()
{
  //define stuff
  float principal;
  float rate;
  int times;
  int years;
  float roi;
  float power;

  //get starting values
  printf("Principal? ");
  scanf("%f",&principal);
  printf("Rate? ");
  scanf("%f",&rate);
  printf("Years? ");
  scanf("%d",&years);
  printf("Times (per year)? ");
  scanf("%d",&times);
 
  //do duh math sheiße
  power = pow((1+(rate/times)),(years*times));
  roi = principal*power;

  //return values
  printf("ROI: %f",roi);
  return 0;
};


I've found that scanf and printf don't really work but found os_GetStringInput(); works well for scanf(); and the code that is default on TI-planet's "project builder" (printText()Wink, works fine for printf(); :

Code:
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}


could someone help me port this over to the ti84+ce?

this is what i've done so far :

Code:

////////////////////////////////////////
// { PROGRAM NAME } { VERSION }
// Author:
// License:
// Description:
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
#define INPUT_SIZE  10

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put function prototypes here */
void printText(const char *text, uint8_t x, uint8_t y);

/* Put all your code here */
void main(void)
{
    /*
    float principal;
    float rate;
    int times;
    int years;
    float roi;
    float power;
    */
    char inputBuffer[INPUT_SIZE];
    char principal[RESP_SIZE];
    /*
    const char rate[RESP_SIZE];
    const char times[RESP_SIZE];
    const char years[RESP_SIZE];
    const char roi[RESP_SIZE];
    const char power[RESP_SIZE];
    */
    const char* one = "Principal? ";
    /*
    const char* two = "Rate? ";
    const char* three = "Years? ";
    const char* four = "Times (per year)? ";
    const real_t fPrincipal;
    */


    /* Clear the homescreen */
    os_ClrHome();

    /* Print a few strings */
    printText(one, 0, 0);
    os_GetStringInput("", inputBuffer, INPUT_SIZE);
    rPrincipal = os_StrToReal(inputBuffer, rPrincipal);
    printText(principal, 0, 2);
    /*printText(two, 0, 3);
    os_GetStringInput("", inputBuffer, INPUT_SIZE);
    sprintf(principal, "Rate is %s.", inputBuffer);
    printText(principal, 0, 4);*/
    /* Wait for a key press */
    while (!os_GetCSC());
}

/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}
Double posting cus it's been a few days

This is a debug version of the code:
So with the help of MateoC && jacobly [bash joke], I got this kinda working.

Code:
////////////////////////////////////////
// { prgmFINANCE } { b1.0 }
// Author: izder456
// License: n/a
// Description: compound periodic interest calculator
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
#define INPUT_SIZE  10

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put function prototypes here */
void printText(const char *text, uint8_t x, uint8_t y);
void float2str(float value, char *str);

/* Put all your code here */
int main(void)
{
    char *ptr;
    char inputBuffer[INPUT_SIZE];
    char prompt[] = "";

    int iPrincipal;
    char sPrincipal[15];
    float fRate;
    char sRate[17];
    int iYears;
    char sYears[15];
    int iTimes;
    char sTimes[15];
    float fPower;
    float fROI;
    char sROI[19];

    const char* one = "Principal? ";
    const char* two = "Rate? ";
    const char* three = "Years? ";
    const char* four = "Times (per year)? ";


    /* Clear the homescreen */
    os_ClrHomeFull();

    /* Print a few strings */
    printText(one, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iPrincipal = strtol(inputBuffer, &ptr, INPUT_SIZE);
    sprintf(sPrincipal, "sPrincipal : %i", iPrincipal);
    printText(sPrincipal, 0, 1);
    /* Wait for a key press */
    while (!os_GetCSC());

    os_ClrHomeFull();
    printText(two, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    fRate = strtod(inputBuffer, &ptr);
    float2str(fRate, sRate);
    printText(sRate, 0, 1);
    /* Wait for a key press */
    while (!os_GetCSC());

    os_ClrHomeFull();
    printText(three, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iYears = strtol(inputBuffer, &ptr, INPUT_SIZE);
    sprintf(sYears, "sYears : %i", iYears);
    printText(sYears, 0, 1);
    /* Wait for a key press */
    while (!os_GetCSC());

    os_ClrHomeFull();
    printText(four, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iTimes = strtol(inputBuffer, &ptr, INPUT_SIZE);
    sprintf(sTimes, "sTimes : %i", iTimes);
    printText(sTimes, 0, 1);
    /* Wait for a key press */
    while (!os_GetCSC());
    os_ClrHomeFull();

    /* do the math */
    fPower = pow((1+(fRate/iTimes)),(iYears*iTimes));
    fROI = iPrincipal*fPower;

    /* printing */
    float2str(fROI, sROI);
    printText("sROI is : ", 0, 0);
    os_PutStrFull(sROI);

    /* Wait for a key press */
    while (!os_GetCSC());
    os_ClrHomeFull();

    return 0;
}

/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}
/* convert float to string */
void float2str(float value, char *str) {
    real_t tmp_real = os_FloatToReal(value);
    os_RealToStr(str, &tmp_real, 8, 1, 2);
}


The only problem aside from the spaghetti code I've created is that every time after the first time the os_GetStringInput(); is called, the previous input is still there. It cannot be deleted. I've tried clearing the inputBuffer from memory with memset(); but to no avail.

as MateoConLechuga mentioned, sprintf doesn't support float/%f so I had to use the code snippet float2str();:



here's the problem in video form :
https://drive.google.com/file/d/1hg6iEnZQWrv9y4zAmKa3MCuN8R9gdXo5/view?usp=sharing

also, here's a link to the TI-Planet project builder :
https://tiplanet.org/pb/?id=249849_1584452415_80cbafdaed

EDIT :
silly mistake, I forgot to get rid of the sprintf in the printing section

new problem :

Code:

strcat("sROI is : ", sROI);
↑ warning : passing 'const char [11]' to parameter of type 'char *' discards qualifiers


EDIT2 :

I got the new problem cleared up; The problem that it wasn't doing the math section of the code, is now fixed. The problem was the memset(); functions, I think they screwed up memory and that's why.

I also made main(); return int 0 [I think this is standard].

The problem still persists where the input buffer doesn't get cleared, so after the first input, while inputting the new variable the old one is already there, [see video].
Double posting because it's now working

Thank ya, MateoConLechuga, jacobly && UXDS [hope I spelt it right].

With you's help, I got the code working.

The problem that every time after the first time the os_GetStringInput(); is called, the previous input was still there. It couldn't be deleted. Well, that's been fixed, I was clearing the wrong string. I needed to clear prompt with :


Code:
prompt[0] = 0;


Conclusion, I didn't know as much C as I thought.

Here's the working, non-debug version of the code :


Code:
////////////////////////////////////////
// { prgmFINANCE } { v1.0 }
// Author: izder456
// License: n/a
// Description: compound periodic interest calculator
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
#define INPUT_SIZE  10

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put function prototypes here */
void printText(const char *text, uint8_t x, uint8_t y);
void float2str(float value, char *str);

/* Put all your code here */
int main(void)
{
    char *ptr;
    char inputBuffer[INPUT_SIZE];
    char prompt[] = "";

    int iPrincipal;
    float fRate;
    int iYears;
    int iTimes;
    float fPower;
    float fROI;
    char sROI[19];

    const char* one = "Principal? ";
    const char* two = "Rate? ";
    const char* three = "Years? ";
    const char* four = "Times (per year)? ";


    /* Clear the homescreen */
    os_ClrHomeFull();

    /* Print a few strings && get input */
    prompt[0] = 0;
    printText(one, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iPrincipal = strtol(inputBuffer, &ptr, INPUT_SIZE);

    prompt[0] = 0;
    printText(two, 0, 1);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    fRate = strtod(inputBuffer, &ptr);

    prompt[0] = 0;
    printText(three, 0, 2);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iYears = strtol(inputBuffer, &ptr, INPUT_SIZE);

    prompt[0] = 0;
    printText(four, 0, 3);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    iTimes = strtol(inputBuffer, &ptr, INPUT_SIZE);

    /* do the math */
    fPower = pow((1+(fRate/iTimes)),(iYears*iTimes));
    fROI = iPrincipal*fPower;

    /* printing */
    float2str(fROI, sROI);
    printText("sROI is : $", 0, 4);
    os_PutStrFull(sROI);

    /* Wait for a key press */
    while (!os_GetCSC());
    os_ClrHomeFull();

    return 0;
}

/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}
/* convert float to string */
void float2str(float value, char *str) {
    real_t tmp_real = os_FloatToReal(value);
    os_RealToStr(str, &tmp_real, 8, 1, 2);
}
  
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 1 of 1
» All times are GMT - 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