Seite 38 von 340 ErsteErste ... 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 88 138 ... LetzteLetzte
Zeige Ergebnis 1.111 bis 1.140 von 10174

C/C++ Programming Help Thread

This is a discussion on C/C++ Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Code: #include <pspdisplay.h> #include <pspgu.h> #include <pspkernel.h> #include <pspctrl.h> #include <pspdebug.h> #include <stdlib.h> #include <stdio.h> #include "graphics.h" #define printf pspDebugScreenPrintf ...

  
  1. #1111
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Code:
    #include <pspdisplay.h>
    #include <pspgu.h>
    #include <pspkernel.h>
    #include <pspctrl.h>
    #include <pspdebug.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "graphics.h"
    #define printf pspDebugScreenPrintf
    #define dprint pspDebugScreenPrintf
    #define true 1
    #define false !true
    #define bool int
    
    PSP_MODULE_INFO("???", 0, 1, 1);
    
    int exit_callback(int arg1, int arg2, void *common) {
              sceKernelExitGame();
              return 0;
    }
    
    int CallbackThread(SceSize args, void *argp) {
              int cbid;
    
              cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
              sceKernelRegisterExitCallback(cbid);
    
              sceKernelSleepThreadCB();
    
              return 0;
    }
    
    int SetupCallbacks(void) {
              int thid = 0;
    
              thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
              if(thid >= 0) {
                        sceKernelStartThread(thid, 0, 0);
              }
    
              return thid;
    }
    
    int i;
    bool rn0 = 0;
        
    int rand() {
         sceCtrlPeekBufferPositive(&pad, 1);
         
         if (pad.Buttons != 0){
              if(pad.Buttons & PSP_CTRL_CROSS) {
               rn0=rand()%10000; //generates random number
              }
         }
         if (rn0==0) {
            pspDebugScreenSetXY(0,2);
            pspDebugScreenPrintf("Press 'X' to start"); //prints text to the screen
         } else {
              pspDebugScreenSetXY(0, 2); //setting up our X and Y coordinates
              pspDebugScreenClear(); //clears screen
              pspDebugScreenSetBackColor(0x00FF0000);
              
              for(i=0;i<65;i++) {
                                for(z=0;z<36;z++) {
                                                  printf(" ");
                                }
              }
              
              pspDebugScreenSetTextColor(0x00000000); //sets text color with HEX
              pspDebugScreenPrintf("Generated Integer:%d",rn0); //prints text to the screen
         }
    }
    int main(void) { //main functions
              pspDebugScreenInit();
              SetupCallbacks();
              SceCtrlData pad;
              
              while (1) {
                        //while loop
                        rand();
              }
    return 0;
    }
    Try that...



    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  2. #1112
    Developer
    Points: 5.359, Level: 47
    Level completed: 5%, Points required for next Level: 191
    Overall activity: 0%

    Registriert seit
    Feb 2006
    Ort
    Norway
    Beiträge
    384
    Points
    5.359
    Level
    47
    Downloads
    0
    Uploads
    0

    Standard

    what are you trying to do with the
    int rand() {
    line in the middle of your main function?
    you should probally delete that.

    on a side note. sceCtrlPeekBufferPositive (&pad, 1);
    should be inside your while loop.
    and the if (pad.Buttons != 0){ check is completly unecesarry.


  3. #1113
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Question

    Code:
    #include <pspdisplay.h>
    #include <pspgu.h>
    #include <pspkernel.h>
    #include <pspctrl.h>
    #include <pspdebug.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include "graphics.h"
    #define printf pspDebugScreenPrintf
    #define dprint pspDebugScreenPrintf
    #define true 1
    #define false !true
    #define bool int
    
    PSP_MODULE_INFO("???", 0, 1, 1);
    
    //Standard callbacks
    int exit_callback(int arg1, int arg2, void *common) {
              sceKernelExitGame();
              return 0;
    }
    
    int CallbackThread(SceSize args, void *argp) {
              int cbid;
    
              cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
              sceKernelRegisterExitCallback(cbid);
    
              sceKernelSleepThreadCB();
    
              return 0;
    }
    
    int SetupCallbacks(void) {
              int thid = 0;
    
              thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
              if(thid >= 0) {
                        sceKernelStartThread(thid, 0, 0);
              }
    
              return thid;
    }
    
    int i;
    bool rn0 = 0;
    
    int rand() {
         sceCtrlPeekBufferPositive(&pad, 1);
         srand(sceKernelLibcTime(NULL));
         if (pad.Buttons != 0){
              if(pad.Buttons & PSP_CTRL_CROSS) {
               rn0=rand()%10000; //generates random number
              }
         }
         if (rn0 != 0) {
            pspDebugScreenSetXY(0,2);
            pspDebugScreenPrintf("Press 'X' to start"); //prints text to the screen
         } else {
              pspDebugScreenSetXY(0, 2); //sets up our X and Y coordinates
              pspDebugScreenClear(); //clears screen
              pspDebugScreenSetBackColor(0x00FF0000); //sets screen color with HEX
              pspDebugScreenSetTextColor(0x00000000); //sets text color with HEX
              pspDebugScreenPrintf("Generated Integer:%d",rn0); //prints text to the screen
         }
    }
    int main(void) { 
              //main functions
              pspDebugScreenInit();
              SetupCallbacks();
              SceCtrlData pad;
              while (1) {
                        //while loop
                        rand();
              }
    return 0;
    }
    hm now i get this error
    Code:
    [email protected] ~
    $ cd /pspdev
    
    [email protected] /pspdev
    $ make
    psp-gcc -I. -I/usr/psp/sdk/include -O2 -G0 -Wall   -c -o main.o main.c
    main.c : In function 'rand':
    main.c(48) : error: 'pad' undeclared (first use in this function)
    main.c(48) : error: (Each undeclared identifier is reported only once
    main.c(48) : error: for each function it appears in.)
    main.c : In function 'main':
    main.c(76) : warning: unused variable 'pad'
    make: *** [main.o] Error 1
    
    [email protected] /pspdev
    $
    It keeps sayind 'pad' is undeclared. hmm weird.
    Geändert von psphacker12. (10-06-2006 um 09:12 PM Uhr)

  4. #1114
    Heroes never die
    Points: 8.645, Level: 62
    Level completed: 65%, Points required for next Level: 105
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    ...........
    Beiträge
    1.323
    Points
    8.645
    Level
    62
    Downloads
    0
    Uploads
    0

    Standard

    you forgotten to ad this:

    SceCtrlData pad;
    -= Double Post =-
    Zitat Zitat von homer
    Pretty sure this would work:
    Code:
     inline void vermenigvuldigen()
    {
    int upPressed = FALSE; // This initializes "upPressed" //
    int downPressed = FALSE;
    printf("Druk op pijltje naar boven/beneden om de waarde van het eerste getal te veranderen :%i\n",x);
    printf("Druk op pijltje naar links/rechts om de waarde van het tweede getal te veranderen :%i\n",y);
    while(1) {
    sceCtrlReadBufferPositive (&pad, 1);//start controls
    if(pad.Buttons & PSP_CTRL_UP) {
    if (!upPressed) {
    x++;
    pspDebugScreenClear();
    vermenigvuldigen();
    upPressed = TRUE;
    }
    }
    else {
    upPressed = FALSE;
    }
    
    
    if(pad.Buttons & PSP_CTRL_DOWN) {
    if (!downPressed) {
    x--;
    pspDebugScreenClear();
    vermenigvuldigen();
    downPressed = TRUE;
    }
    }
    else {
    downPressed = FALSE;
    }
    
    
    
    
    if(pad.Buttons & PSP_CTRL_RIGHT) {
    y++;
    
    pspDebugScreenClear();
    vermenigvuldigen();
    
    }
    if(pad.Buttons & PSP_CTRL_LEFT) {
    y--;
    
    pspDebugScreenClear();
    vermenigvuldigen();
    
    }
    if(pad.Buttons & PSP_CTRL_CROSS) {
    pspDebugScreenClear();
    printf("het product is: %i\n",x*y);
    menu();
    }
    if(pad.Buttons & PSP_CTRL_TRIANGLE) {
    printf(" deze functie is nog niet af");
    vermenigvuldigen();
    }
    
    }
    }
    EDIT: Just tested it, worked fine.

    not working it does again , x=1,2,3,4,
    it only have to do x=1 and x=2if you press again:Cry:
    Geändert von hallo007 (10-07-2006 um 05:05 AM Uhr) Grund: Automerged Doublepost

  5. #1115
    Developer
    Points: 5.359, Level: 47
    Level completed: 5%, Points required for next Level: 191
    Overall activity: 0%

    Registriert seit
    Feb 2006
    Ort
    Norway
    Beiträge
    384
    Points
    5.359
    Level
    47
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von hallo007
    you forgotten to ad this:

    SceCtrlData pad;
    -= Double Post =-



    not working it does again , x=1,2,3,4,
    it only have to do x=1 and x=2if you press again:Cry:
    try this
    Code:
    inline void vermenigvuldigen()
    {
    int upPressed = FALSE; // This initializes "upPressed" //
    int downPressed = FALSE;
    printf("Druk op pijltje naar boven/beneden om de waarde van het eerste getal te veranderen :%i\n",x);
    printf("Druk op pijltje naar links/rechts om de waarde van het tweede getal te veranderen :%i\n",y);
    while(1) {
    sceCtrlReadBufferPositive (&pad, 1);//start controls
    if(pad.Buttons & PSP_CTRL_UP) {
    if (!upPressed) {
    x++;
    pspDebugScreenClear();
    upPressed = TRUE;
    }
    }
    else {
    upPressed = FALSE;
    }
    
    
    if(pad.Buttons & PSP_CTRL_DOWN) {
    if (!downPressed) {
    x--;
    pspDebugScreenClear();
    downPressed = TRUE;
    }
    }
    else {
    downPressed = FALSE;
    }
    
    
    
    
    if(pad.Buttons & PSP_CTRL_RIGHT) {
    y++;
    
    pspDebugScreenClear();
    
    }
    if(pad.Buttons & PSP_CTRL_LEFT) {
    y--;
    
    pspDebugScreenClear();
    
    }
    if(pad.Buttons & PSP_CTRL_CROSS) {
    pspDebugScreenClear();
    printf("het product is: %i\n",x*y);
    menu();
    }
    if(pad.Buttons & PSP_CTRL_TRIANGLE) {
    printf(" deze functie is nog niet af");
    vermenigvuldigen();
    }
    
    }
    }


  6. #1116
    Heroes never die
    Points: 8.645, Level: 62
    Level completed: 65%, Points required for next Level: 105
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    ...........
    Beiträge
    1.323
    Points
    8.645
    Level
    62
    Downloads
    0
    Uploads
    0

    Standard

    so the prob is repating something

    thnx!!

  7. #1117
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    does anyone have a link to download the JPEG viewer lib?

  8. #1118
    AKA Homer
    Points: 12.596, Level: 73
    Level completed: 37%, Points required for next Level: 254
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    Sweden
    Beiträge
    1.779
    Points
    12.596
    Level
    73
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von psphacker12.
    does anyone have a link to download the JPEG viewer lib?
    Jpeg's on a PSP is really slow, I would suggest using PNG's. However, GeMP 3.3 has a jpeg viewer, you can download the source code here Source (it's in image.c)


    Click Here if you want a Winamp Currently Playing Userbar like the one above.

  9. #1119
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    So does FileAssistant's Source =-\

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  10. #1120
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    would this restart my game??
    Code:
    if(pad.Buttons & PSP_CTRL_CROSS) {
      break;
    }

  11. #1121
    Developer
    Points: 5.359, Level: 47
    Level completed: 5%, Points required for next Level: 191
    Overall activity: 0%

    Registriert seit
    Feb 2006
    Ort
    Norway
    Beiträge
    384
    Points
    5.359
    Level
    47
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von psphacker12.
    would this restart my game??
    Code:
    if(pad.Buttons & PSP_CTRL_CROSS) {
      break;
    }
    ehm. It would break you out of the current loop.

    It would only restart the game if you had a loop from the start of the game that ended after the current loop or if you called a function after the loop you break out of.


  12. #1122
    AKA Homer
    Points: 12.596, Level: 73
    Level completed: 37%, Points required for next Level: 254
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    Sweden
    Beiträge
    1.779
    Points
    12.596
    Level
    73
    Downloads
    0
    Uploads
    0

    Standard

    A sceKernelExitGame(); call would exit the game.


    Click Here if you want a Winamp Currently Playing Userbar like the one above.

  13. #1123
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von waterbottle
    ehm. It would break you out of the current loop.

    It would only restart the game if you had a loop from the start of the game that ended after the current loop or if you called a function after the loop you break out of.
    ok thx.
    -= Double Post =-
    Code:
    #include <pspdisplay.h>
    #include <pspgu.h>
    #include <pspkernel.h>
    #include <pspctrl.h>
    #include <pspdebug.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <png.h>
    #include "graphics.h"
    #define printf pspDebugScreenPrintf
    #define dprint pspDebugScreenPrintf
    #define true 1
    #define false !true
    #define bool int
    
    PSP_MODULE_INFO("???", 0, 1, 1);
    
    //Standard callbacks
    int exit_callback(int arg1, int arg2, void *common) {
              sceKernelExitGame();
              return 0;
    }
    
    int CallbackThread(SceSize args, void *argp) {
              int cbid;
    
              cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
              sceKernelRegisterExitCallback(cbid);
    
              sceKernelSleepThreadCB();
    
              return 0;
    }
    
    int SetupCallbacks(void) {
              int thid = 0;
    
              thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
              if(thid >= 0) {
                        sceKernelStartThread(thid, 0, 0);
              }
    
              return thid;
    }
    
    int i;
    int rn0 = (NULL);
    int rn1 = (NULL);
    int rn2 = (NULL);
    bool status = (NULL);
    int nums() {
         SceCtrlData pad;
         sceCtrlPeekBufferPositive(&pad, 1);
         srand(sceKernelLibcTime(NULL));
         if (rn0 != (NULL) & rn1 != (NULL) & rn2 != (NULL)) {
           rn0=0
           rn1=0
           rn2=0
         }
         if (pad.Buttons != 0){
              if(pad.Buttons & PSP_CTRL_CROSS) {
               rn0=rand()%3;
               rn0=rand()%3;
               rn0=rand()%3;
              }
         }
         if (rn0 != 3 & rn1 != 3 & rn2 !=3) {
              pspDebugScreenSetXY(0,2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("You won./n");
              pspDebugScreenPrintf("Press 'X' to restart./n");
              if(pad.Buttons & PSP_CTRL_CROSS) {
                nums();
              }
         }
         if(rn0 != 0 & rn1 != 0 & rn2 != 0) {
              pspDebugScreenSetXY(0,2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("Press 'X' to start");
         } else {
           if (pad.Buttons & PSP_CTRL_CROSS) {
              pspDebugScreenSetXY(0, 2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn0);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn1);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn2);
              sceDisplayWaitVblankStart();
              sceKernelSleepThread();
           }
         }
    }
    int main(void) {
              //main functions
              pspDebugScreenInit();
              SetupCallbacks();
              while (1) {
                        //while loop
                        nums();
              }
    return 0;
    }
    it says rn1(undeclared???
    how can i fix this?
    Geändert von psphacker12. (10-07-2006 um 09:24 AM Uhr) Grund: Automerged Doublepost

  14. #1124
    AKA Homer
    Points: 12.596, Level: 73
    Level completed: 37%, Points required for next Level: 254
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    Sweden
    Beiträge
    1.779
    Points
    12.596
    Level
    73
    Downloads
    0
    Uploads
    0

    Standard

    By declaring m1 maybe??


    Click Here if you want a Winamp Currently Playing Userbar like the one above.

  15. #1125
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    It would help if you told us the line number.

  16. #1126
    Ponies and Unicorns
    Points: 5.778, Level: 49
    Level completed: 14%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    Pelennor Fields
    Beiträge
    547
    Points
    5.778
    Level
    49
    Downloads
    0
    Uploads
    0

    Standard

    Hi guys i just went to the book store today and saw a book called "C++ For Dummies" I looked through it and it seemed to have a lot of C++ information and it even came with a cd with a compiler and some other useful stuff. So I bought it. Havent started reading it yet but has anyone else gotten this book and was it helpful?
    If you play WoW come find me on DOOMHAMMER (US) I am Human mage lvl 64 Atrana is the name (dont ask for runs!)
    Gold donations are highly appreciated!

  17. #1127
    sceKernelExitGame();
    Points: 19.955, Level: 89
    Level completed: 21%, Points required for next Level: 395
    Overall activity: 0%

    Registriert seit
    Jan 2006
    Ort
    New York
    Beiträge
    3.126
    Points
    19.955
    Level
    89
    Downloads
    0
    Uploads
    0

    Standard

    Usually the "___ for dummies" series have good informataion, but they are very generic. I wouldn't og bought that book myself, but it can't hurt to read it ;)

  18. #1128
    Ponies and Unicorns
    Points: 5.778, Level: 49
    Level completed: 14%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    Pelennor Fields
    Beiträge
    547
    Points
    5.778
    Level
    49
    Downloads
    0
    Uploads
    0

    Standard

    okay. but skimming through it it looks pretty good so i will read it anyways and i guess i read some internet tutorials too. thanks
    If you play WoW come find me on DOOMHAMMER (US) I am Human mage lvl 64 Atrana is the name (dont ask for runs!)
    Gold donations are highly appreciated!

  19. #1129
    QJ Gamer Blue
    Points: 4.511, Level: 42
    Level completed: 81%, Points required for next Level: 39
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Germany
    Beiträge
    216
    Points
    4.511
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard Handling data recieved via TCP

    Handling data recieved via TCP

    Hi,
    On a simple server I'm recieving (and displaying) a string via

    Code:
        for (;;) {
            len = sizeof(client);
            fd = accept(sock, (struct sockaddr*)&client, &len);
            if (fd < 0)
                error_exit("Fehler bei accept");
            printf("Bearbeite Client mit der Adresse: %s\n", inet_ntoa(client.sin_addr));
            /* Daten vom Client auf dem Bildschirm ausgeben */
            echo ( fd );
    
            closesocket(fd);
        }
    but he outputs the string and the time it was recieved. What I want is to fetch the string (isolated) and save it in a variable. How do I do that?

  20. #1130
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von homer
    By declaring m1 maybe??
    i have it set up like this..
    Code:
    int rn1 = (NULL);

  21. #1131
    Developer
    Points: 4.447, Level: 42
    Level completed: 49%, Points required for next Level: 103
    Overall activity: 0%

    Registriert seit
    May 2006
    Beiträge
    114
    Points
    4.447
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von psphacker12.
    i have it set up like this..
    Code:
    int rn1 = (NULL);
    In the case that you are using it, && is supposed to be used for 'and' statements, not &. You're also missing some semicolons.

    For game downloads and development updates, check out my website.
    http://xfacter.wordpress.com/

  22. #1132
    QJ Gamer Blue
    Points: 4.511, Level: 42
    Level completed: 81%, Points required for next Level: 39
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Germany
    Beiträge
    216
    Points
    4.511
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von Lukeson
    Handling data recieved via TCP

    Hi,
    On a simple server I'm recieving (and displaying) a string via

    Code:
        for (;;) {
            len = sizeof(client);
            fd = accept(sock, (struct sockaddr*)&client, &len);
            if (fd < 0)
                error_exit("Fehler bei accept");
            printf("Bearbeite Client mit der Adresse: %s\n", inet_ntoa(client.sin_addr));
            /* Daten vom Client auf dem Bildschirm ausgeben */
            echo ( fd );
    
            closesocket(fd);
        }
    but he outputs the string and the time it was recieved. What I want is to fetch the string (isolated) and save it in a variable. How do I do that?
    Solution:
    Code:
        for (;;) {
            len = sizeof(client);
            fd = accept(sock, (struct sockaddr*)&client, &len);
            if (fd < 0)
                error_exit("Error while accepting");
    		
    		printf("Accepting connection, IP: %s\n", inet_ntoa(client.sin_addr));
    		while(rc!=SOCKET_ERROR)
    		{
    			rc=recv(fd,buf,256,0);
    			if(rc==0)
    			{
    				printf("Server has closed the connection...\n");
    				break;
    			}
    			if(rc==SOCKET_ERROR)
    			{
    				printf("Fehler: recv, fehler code: %d\n",WSAGetLastError());
    				break;
    			}
    			buf[rc]='\0';
    			printf("Client sendet: %s\n",buf);
    			//sprintf(buf2,"Du mich auch %s",buf);
    			//rc=send(connectedSocket,buf2,strlen(buf2),0);
    		}
            closesocket(fd);
    What's the right returntype for char[256]?


    Here's another question:

    I need to write a function that returns a string of variable length; in fact it's not a string, it's an array of "char"s; What return-type do I have t choose?
    Geändert von Lukeson (10-07-2006 um 01:30 PM Uhr) Grund: Automerged Doublepost

  23. #1133
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Lukeson - What? A 'char' datatype is a character array, or 'buffer'. If you're writing a function returning a char, then have the data type of that function, a char.

    As for getting hte length of a given string, either include string.h (or stdlib..hmmm... cant rememmber) and use 'strlen(string)' or:
    Code:
    int
    strlen(const char *s)
    {
    	const char *sc;
    
    	for (sc = s; *sc != '\0'; ++sc)
    		;// nothing 
    
    	return sc - s;
    }
    But i prefer you use the strnlen version, to prevent an overflow. Manual strnlen:
    Code:
    int
    strnlen(const char *s, int count)
    {
    	const char *sc;
    
    	for (sc = s; count-- && *sc != '\0'; ++sc)
    		/* nothing */ ;
    
    	return sc - s;
    }
    So say if you wanted to do a directory listing type thing, the psp screen only hold around 60 characters from left to right, so you may want to limit it.. By using strnlen, you can say only print the filename up to 60... Itd be much more complicated than that, but you really should use strnlen as it prevents an overflow.

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  24. #1134
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von GuitarGod1134
    Hi guys i just went to the book store today and saw a book called "C++ For Dummies" I looked through it and it seemed to have a lot of C++ information and it even came with a cd with a compiler and some other useful stuff. So I bought it. Havent started reading it yet but has anyone else gotten this book and was it helpful?
    Hmm I would reccomend to start out with that book, then later on you can learn the PSP specific syscalls.:)

  25. #1135
    words are stones in my <3
    Points: 35.274, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%

    Registriert seit
    Jul 2005
    Ort
    Spokane
    Beiträge
    5.008
    Points
    35.274
    Level
    100
    My Mood
    Lonely
    Downloads
    1
    Uploads
    0

    Standard

    Yes cause everyone needs to know them in order to create for hte PSP. =-\ [/sarcasm]

    ...at what speed must I live.. to be able to see you again?...

    Projects

    You can support my Open World 3D RPG for PSP by voting for it here


  26. #1136
    QJ Gamer Blue
    Points: 4.511, Level: 42
    Level completed: 81%, Points required for next Level: 39
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Germany
    Beiträge
    216
    Points
    4.511
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard

    I'm sorry, I'm stupid and don't get it;
    So a char isn't - as I thought - a single character but a buffer for a string? so can I just do this:?

    Code:
    char foo()
    {
        char buf[256];
    
        buf = get_string_from_server();
    
        return buf;
    }
    
    int main()
    {
        ...
        printf("%s", foo());
        ...
    }
    Tested, freezes the PSP

    I probably need to clarify my question; I have a function that recieves a string via recv() from the server. I want this string to be the return-value of the function, I just don't know what return-type i need therefor. Ex.:

    Code:
    [???] get_artists(int socket)
    {
    	long rc;
    	char buf[256];
    
    	...
    
    	rc=recv(socket,buf,256,0);
    	buf[rc]='\0';
    	return buf;
    }

  27. #1137
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von SG57
    Yes cause everyone needs to know them in order to create for hte PSP. =-\ [/sarcasm]
    BTW who was that guy who released the tiff game "BRICKS" without your permission? and how did he get his hands onto it?
    -= Double Post =-
    not working
    Code:
    #include <pspdisplay.h>
    #include <pspgu.h>
    #include <pspkernel.h>
    #include <pspctrl.h>
    #include <pspdebug.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <png.h>
    #include "graphics.h"
    #define printf pspDebugScreenPrintf
    #define dprint pspDebugScreenPrintf
    #define true 1
    #define false !true
    #define bool int
    
    PSP_MODULE_INFO("???", 0, 1, 1);
    
    //Standard callbacks
    int exit_callback(int arg1, int arg2, void *common) {
              sceKernelExitGame();
              return 0;
    }
    
    int CallbackThread(SceSize args, void *argp) {
              int cbid;
    
              cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
              sceKernelRegisterExitCallback(cbid);
    
              sceKernelSleepThreadCB();
    
              return 0;
    }
    
    int SetupCallbacks(void) {
              int thid = 0;
    
              thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
              if(thid >= 0) {
                        sceKernelStartThread(thid, 0, 0);
              }
    
              return thid;
    }
    
    int funcs() {
      int i;
      int rn0 = (NULL);
      int rn1 = (NULL);
      int rn2 = (NULL);
      bool status = (NULL);
      SceCtrlData pad;
      sceCtrlPeekBufferPositive(&pad, 1);
      srand(sceKernelLibcTime(NULL));
    }
    int nums() {
         if (rn0 != (NULL) && rn1 != (NULL) && rn2 != (NULL)) {
           rn0=0
           rn1=0
           rn2=0
         }
         if (pad.Buttons != 0){
              if(pad.Buttons && PSP_CTRL_CROSS) {
               rn0=rand()%3;
               rn1=rand()%3;
               rn2=rand()%3;
              }
         }
         if (rn0 != 2 && rn1 != 2 && rn2 != 2) {
              pspDebugScreenSetXY(0,2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("You won./n");
              pspDebugScreenPrintf("Press 'X' to restart./n");
         }
         if (rn0 != 3 && rn1 != 3 && rn2 !=3) {
              pspDebugScreenSetXY(0,2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("You won./n");
              pspDebugScreenPrintf("Press 'X' to restart./n");
              if(pad.Buttons & PSP_CTRL_CROSS) {
                nums();
              }
         }
         if(rn0 != 0 && rn1 != 0 && rn2 != 0) {
              pspDebugScreenSetXY(0,2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("Press 'X' to start/n");
         } else {
           if (pad.Buttons && PSP_CTRL_CROSS) {
              pspDebugScreenSetXY(0, 2);
              pspDebugScreenClear();
              pspDebugScreenSetBackColor(0x00FF0000);
              pspDebugScreenSetTextColor(0x00000000);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn0);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn1);
              pspDebugScreenPrintf("Generated Integer:%d/n",rn2);
              sceDisplayWaitVblankStart();
              sceKernelSleepThread();
           }
         }
    }
    int main(void) {
              //main functions
              pspDebugScreenInit();
              SetupCallbacks();
              while(1) {
                        //while loop
                        funcs();
                        nums();
              }
    return 0;
    }
    Errors
    Code:
    [email protected] ~
    $ cd /pspdev
    
    [email protected] /pspdev
    $ make
    psp-gcc -I. -I/usr/psp/sdk/include -O2 -G0 -Wall   -c -o main.o main.c
    main.c : In function 'funcs':
    main.c(50) : warning: initialization makes integer from pointer without a cast
    main.c(51) : warning: initialization makes integer from pointer without a cast
    main.c(52) : warning: initialization makes integer from pointer without a cast
    main.c(53) : warning: initialization makes integer from pointer without a cast
    main.c(53) : warning: unused variable 'status'
    main.c(52) : warning: unused variable 'rn2'
    main.c(51) : warning: unused variable 'rn1'
    main.c(50) : warning: unused variable 'rn0'
    main.c(49) : warning: unused variable 'i'
    main.c : In function 'nums':
    main.c(59) : error: 'rn0' undeclared (first use in this function)
    main.c(59) : error: (Each undeclared identifier is reported only once
    main.c(59) : error: for each function it appears in.)
    main.c(59) : error: 'rn1' undeclared (first use in this function)
    main.c(59) : error: 'rn2' undeclared (first use in this function)
    main.c(61) : error: syntax error before 'rn1'
    main.c(64) : error: 'pad' undeclared (first use in this function)
    make: *** [main.o] Error 1
    
    [email protected] /pspdev
    $
    Geändert von psphacker12. (10-07-2006 um 02:23 PM Uhr) Grund: Automerged Doublepost

  28. #1138
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    GuitarGod1134:
    Don't get that book, they are usually pretty poor in my opinion.

    Take a read through this online book:
    http://www.ibiblio.org/obp/thinkCS/cpp/english/

    And consider buying this one:
    http://www.amazon.co.uk/gp/product/1...649123-1896422

    Lukeson:
    Code:
    [???] get_artists(int socket)
    {
    	long rc;
    	char buf[256];
    
    	...
    
    	rc=recv(socket,buf,256,0);
    	buf[rc]='\0';
    	return buf;
    }
    The correct return type would be a char pointer:
    Code:
    char * get_artists(int socket)
    {
    	long rc;
    	char buf[256];
    
    	...
    
    	rc=recv(socket,buf,256,0);
    	buf[rc]='\0';
    	return buf;
    }
    However this will throw a runtime error:
    Code:
    char buffer[256];
    buffer = get_artists(6666);
    printf("%s", buffer);
    This is because the buf that was in the function was created locally on the stack and the function returns a pointer to it. However, after the pointer assignment to buffer, it is removed from the stack since it is no longer in scope and now pointer is pointing to garbarge. A better method would be to do:

    Code:
    void get_artists(int socket, char * recivedMessage)
    {
    	long rc;
    	...
    
    	rc=recv(socket,recivedMessage,256,0);
    	recivedMessage[rc]='\0';
    	return recivedMessage;
    }
    
    int main()
    {
        char buffer[256] = "";
        get_artists(6666, buffer);
        printf("%s", buffer);
        ...
    }
    -= Double Post =-
    psp12.:
    Code:
    main.c(50) : warning: initialization makes integer from pointer without a cast
    main.c(51) : warning: initialization makes integer from pointer without a cast
    main.c(52) : warning: initialization makes integer from pointer without a cast
    main.c(53) : warning: initialization makes integer from pointer without a cast
    You are assigning non pointer variables NULL. NULL is usually #defined as 0 and should be reserved for pointers only hence a warning rather then an error. Just give them the value 0.
    Code:
      int rn0 = 0; 
      int rn1 = 0;
      int rn2 = 0;
    Code:
    main.c(53) : warning: unused variable 'status'
    main.c(52) : warning: unused variable 'rn2'
    main.c(51) : warning: unused variable 'rn1'
    main.c(50) : warning: unused variable 'rn0'
    main.c(49) : warning: unused variable 'i'
    You didn't use any of the variables in the function funcs().

    Code:
    main.c : In function 'nums':
    main.c(59) : error: 'rn0' undeclared (first use in this function)
    main.c(59) : error: (Each undeclared identifier is reported only once
    main.c(59) : error: for each function it appears in.)
    main.c(59) : error: 'rn1' undeclared (first use in this function)
    main.c(59) : error: 'rn2' undeclared (first use in this function)
    main.c(64) : error: 'pad' undeclared (first use in this function)
    None of these variables have been decleared in scope of the function nums().

    Code:
    main.c(61) : error: syntax error before 'rn1'
    You forgot ';' on the line before.
    Geändert von yaustar (10-07-2006 um 02:48 PM Uhr) Grund: Automerged Doublepost

  29. #1139
    likes kittens....awww....
    Points: 6.975, Level: 55
    Level completed: 13%, Points required for next Level: 175
    Overall activity: 0%

    Registriert seit
    Sep 2006
    Ort
    Detroit
    Beiträge
    628
    Points
    6.975
    Level
    55
    Downloads
    0
    Uploads
    0

    Question

    None of these variables have been decleared in scope of the function nums().

    Code:
    main.c(61) : error: syntax error before 'rn1'
    You forgot ';' on the line before.
    How can I fix that?
    Geändert von psphacker12. (10-07-2006 um 03:09 PM Uhr)

  30. #1140
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Great idea, quote the entire post for one small part of it (-_-;)

    Code:
    int nums() {
         if (rn0 != (NULL) && rn1 != (NULL) && rn2 != (NULL)) {
           rn0=0
           rn1=0
           rn2=0
         }
    Code:
    int nums() {
       int rn0=0;
       int rn1=0;
       int rn2=0;
       SceCtrlData pad;
    Here is another tip, buy this book:
    The C programming Language or this one Beginning C++ Game Programming or at LEAST read this online one: Thinking like a computer scientist [C++]


 

Tags for this Thread

Forumregeln

  • Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
  • Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
  • Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
  • Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
  •  





Alle Zeitangaben in WEZ -8. Es ist jetzt 09:15 PM Uhr.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © , Caputo Media, LLC. All Rights Reserved. Cluster .