• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

hi help with this if/else statement

The-R

The One & Only
im pretty new to psp coding on a whole so im having trouble with this bit of code. trying to get the program to only save input that is 25 characters or less and if over 25 print a message to the screen tht tells the user the input shud be 25 characters or less widout the program saving the aforementioned over 25 character message.

if (oslOskIsActive()){
oslDrawOsk(); // starts the osk (Onscreen Keyboard)
if (oslGetOskStatus() == PSP_UTILITY_DIALOG_NONE){
if (oslOskGetResult() == OSL_OSK_CANCEL)
sprintf(message, "Make A Selection....");
}else if
char userText[100] = "";{
oslOskGetText(userText); // enters a record here
}else if (strlen(userText)>25) {
sprintf(message, "note must be 25 characters or less");
oslPrintf(message);


}else if sprintf(message, "P-Note Saved ^_^ %s \n"); { // displays record on the screen
strcat(userText, "\n");
log = writelog(userText); // writes the record to file
}
oslEndOsk();
}
 

Hellcat

Contributor
Hm, OK, first of all this line:
Code:
}else if
[B][I]char userText[100] = "";{[/I][/B]
oslOskGetText(userText); // enters a record here
might throw a compiler error.
It has no brackets for the actual conditional statement AND you're using a variable declaration/definition as a conditional statement.... can't work.

Then, you're having too many "else if" where you maybe want to start off with a new "if" block.

Let's consider for a second the above "else if" would work (which it doesn't) then it would ask the user for the input - but would NOT execute the "else if" for the length, as in an "if .... else if .... else if .... .... .... else" block only ONE of all of the possible conditions will be executed, once it found one it skips to the very end of the whole "if" thing and continues from there.

So, you might want to nest the "if"s a bit different, maybe like this:
Code:
if (oslOskIsActive())
{
  oslDrawOsk(); // starts the osk (Onscreen Keyboard)
  if (oslGetOskStatus() == PSP_UTILITY_DIALOG_NONE)
  {
    if (oslOskGetResult() == OSL_OSK_CANCEL)
    sprintf(message, "Make A Selection....");
  } else if
    char userText[100] = "";
    oslOskGetText(userText); // enters a record here
    if (strlen(userText)>25)
    {
      sprintf(message, "note must be 25 characters or less");
      oslPrintf(message);
    } else {
      sprintf(message, "P-Note Saved ^_^ %s \n");  // displays record on the screen
      oslPrintf(message);
      strcat(userText, "\n");
      log = writelog(userText); // writes the record to file
  }
  oslEndOsk();
}


Maybe you should also grab a C book and read up again on how IF works.
You keep writing "else if" when you might only want to have "else" and you keep using function calls and such as conditional statements (which can work, but only if you phrase them as a conditional statement, for one you must put brackets around it and the function must return something the IF can evaluate):
 

The-R

The One & Only
i appreciate the correction of the code i was having problems with. seemingly however it also gives me errors. i was wondering if u cud take a look at my complete code and see if im doing sumthin wrong tht you see to fix so as to get this program to compile.
 

Hellcat

Contributor
Can't promise anything as I'm kinda short on time :-/ But can try to give it a shot :)

Post the code AND the compiler errors on some pastebin (for example http://pb.malloc.us/ or wichever you like most) and link them here, I will do a quick scan over the errornous lines and see if I can spot something :)
 

elite

Oldie moldie
What compiler do you use? I use Visual Studio and the good thing is that it locates the mistake. :)
 

MenaceInc

Staff Member
code is here at http://pb.malloc.us/m1831e59c and screen shot of errors is here at http://i52.tinypic.com/2vunc6x.png. helping fixing this program and finally getting it to work is greatly appreciated.

You've missed a bracket, I know that much just by looking at it. I'll read through it now and figure out where.

EDIT: I've cleaned up your main loop but to be honest, with that many if/else statements, you should be using switches.
http://pb.malloc.us/m48bc680a

Haven't tried to compile that but it should be fine now. You were missing a bracket. If you keep your code clean then it becomes easier to spot these. Especially when you use a good editor like Notepad++.


DOUBLE EDIT: just installed the toolchain and tried compiling it. Threw up loads of errors about osLib xD here I go hunting for headers
Code:
main.c:2:25: error: oslib/oslib.h: No such file or directory
main.c:3:29: error: oslib/intraFont.h: No such file or directory

ANOTHER EDIT: yeah...tried searching for the right headers and whatnot for OSLib and IntraFont....couldn't find the right one that you used. So...yeah. xD
 

The-R

The One & Only
thanks for the help and wisdom. imma try compiling it and see what turns up.

edit so yea tried ti and got an error. tried to fix it and got it to compile but program crashes so reverted back to code u had pasted initially and showing u the error see if u know wats up.

http://i53.tinypic.com/4si8td.png
 

skyend

Member
Code:
      else if [COLOR="#ff0000"]no condition >.>[/COLOR]{
                                        char userText[100] = "";
                                        oslOskGetText(userText); // enters a record here

and

Code:
  sprintf(message, "P-Note Saved ^_^ [COLOR="#ff0000"]%s[/COLOR] \n");  // displays record on the screen
should be 
  sprintf(message, "P-Note Saved ^_^  \n");  // displays record on the screen
 

The-R

The One & Only
its like no matter what i try to get this thing to work it just refuses to work. finally got it to compile and not crash but now al of a sudden it doesnt wanna save the input from the oskeyboard to teh file. is it really tht hard to set a character limit on a message inputted...? pls if anyone can come up with a working solution to this program so it can run properly...?


#include <pspkernel.h>
#include <oslib/oslib.h>
#include <oslib/intraFont.h>

PSP_MODULE_INFO("P-Notes v 1.0", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);


int runningFlag = 1; //Emergency Callback value for home button exit command
int skip = 0; //User Defined Variable
char message[100] = ""; //Limit for error messages
char userText[100] = "";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Callbacks:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
runningFlag = 0;
return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;

cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
if(thid >= 0)
sceKernelStartThread(thid, 0, 0);
return thid;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init OSLib:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int initOSLib(){
oslInit(0);
oslInitGfx(OSL_PF_8888, 1);
// oslInitAudio();
oslSetQuitOnLoadFailure(1);
oslSetKeyAutorepeatInit(10);
oslSetKeyAutorepeatInterval(1);
return 0;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
int skip = 0;
int log; //keeps track of return values
char inbuf[128*1024]; //sets up size parameters to for file that stores notes
SetupCallbacks(); //calls the callback functions for home button
initOSLib(); // inits oslib (Onscreen Library)
oslIntraFontInit(INTRAFONT_CACHE_MED); // inits intraFont

//Loads image:
OSL_IMAGE *bkg = oslLoadImageFilePNG("bkg.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);

//Load font:
OSL_FONT *pgfFont = oslLoadFontFile("flash0:/font/ltn0.pgf");
oslIntraFontSetStyle(pgfFont, 1.0, RGBA(255,255,255,255), RGBA(0,0,0,0), INTRAFONT_ALIGN_LEFT);
oslSetFont(pgfFont);

while(!osl_quit){
if (!skip){
oslStartDrawing();
oslDrawImageXY(bkg, 0, 0);
oslDrawString(150, 10, "P-Notes version 1.0");
oslDrawString(50, 40, "Press X to Write Data");
oslDrawString(50, 60, "Press O to Read Data");
oslDrawString(50, 80, "Press [] to Delete All");

oslDrawString(50, 250, message);
oslDrawString(50, 120, inbuf);

if (oslOskIsActive()){
oslDrawOsk(); // starts the osk (Onscreen Keyboard)
if (oslGetOskStatus() == PSP_UTILITY_DIALOG_NONE){
if (oslOskGetResult() == OSL_OSK_CANCEL){
sprintf(message, "Make A Selection....");
}
else if (oslOskGetResult() != OSL_OSK_CANCEL){
char userText[100] = "";
oslOskGetText(userText); // enters a record here
}
if ( strlen(userText) >= 26 ){
sprintf(message, "Note must be 25 characters or less\n");
oslPrintf(message);
}
else if ( strlen(userText) < 26 ){
sprintf(message, "P-Note Saved ^_^ \n"); // displays record on the screen
oslPrintf(message);
strcat(userText, "\n");
log = writelog(userText); // writes the record to file
}
oslEndOsk();
}
}
//oslEndDrawing();
if (!oslOskIsActive()){
oslReadKeys();
}
if (osl_keys->pressed.triangle){
// runningFlag = 0;
oslEndGfx();
sceKernelExitGame(); // exit if triangle is pressed
}
else if (osl_keys->pressed.cross){
// enter data if cross is pressed
oslInitOsk("Please insert some text", "Input Note", 128, 1,"ENGLISH");
memset(message, 0, sizeof(message));
}
else if (osl_keys->pressed.square){
// delete the file
sceIoRemove("ms0:/P-Notes.txt");
}
else if (osl_keys->pressed.circle){
// read the file
readlog(inbuf);
oslDrawString(350, 250, "Loading...");
oslDrawString(50, 120, inbuf); // print the records
}
}
oslEndFrame();
skip = oslSyncFrame();
}
//Quit OSL:
oslEndGfx();
sceKernelExitGame();
return 0;
}

// writes the data entered from osk to a file
int writelog(char* str)
{
SceUID fd = sceIoOpen("ms0:/P-Notes.txt", PSP_O_CREAT | PSP_O_APPEND | PSP_O_WRONLY, 0777);
if (fd > 0) sceIoWrite(fd, str, strlen(str));
else return -1;
sceIoClose(fd);
return 0;
}

// reads the file
void readlog(char* buff)
{

int fd;
int flen;



if(!(fd = sceIoOpen("ms0:/P-Notes.txt", PSP_O_RDONLY, 0777)))
{
// Error if we cant open it
}
else {
flen = sceIoRead(fd, buff, 131072);
if(flen <= 0)
{
// if flen is 0 or lower, this means that the file
// is empty, it didnt read any bytes.
}
}

// Must make sure to close the file I/O before continuing
// with the rest of the program
sceIoClose(fd);
}
 
Top