vendredi 30 mars 2018

Outputting A Sentence Configured Via Random Pieces Using Pointer Arrays

I am new to coding and we're just learning pointers, and I'm unsure of why this code is not storing the bits of the sentence to the sentence array throughout the program, or why it is not able to print the data,

This is for a school assignment, and I normally just spend hours attempting different methods, but I figure it will be better to ask for advice so i can learn to do this correctly.

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#define SIZE 4



void nounPick(const const char *nPTR, char *sPTR );
void verbPick(const const char *vPTR, char *sPTR );
void prepPick(const const char *pPTR, char *sPTR );
void artiPick(const const char *aPTR, char *sPTR );


int main(void){


   char *noun[SIZE];
   char *verb[SIZE];
   char *prep[SIZE];
   char *arti[SIZE];
   char *sent[SIZE];


   noun[0] = "cat ";
   noun[1] = "dog ";
   noun[2] = "truck ";
   noun[3] = "plane ";
   noun[4] = "skateboard ";

   verb[0] = "drove ";
   verb[1] = "jumped ";
   verb[2] = "ran ";
   verb[3] = "walked ";
   verb[4] = "flew ";

   prep[0] = "to ";
   prep[1] = "from ";
   prep[2] = "over ";
   prep[3] = "under ";
   prep[4] = "on ";

   arti[0] = "a ";
   arti[1] = "one ";
   arti[2] = "some ";
   arti[3] = "any ";




   const char *nPTR = noun[0];
   const char *vPTR = verb[0];
   const char *pPTR = prep[0];
   const char *aPTR = arti[0];
   char *sPTR = sent[0];

    srand( time(NULL));
    for(int c = 0; c< SIZE; c++)
        *(sent + c) = 0;

    nounPick(nPTR, sPTR);
    verbPick(vPTR, sPTR);
    prepPick(pPTR, sPTR);
    artiPick(aPTR, sPTR);

    printf("Made it through the picking\n");
    printf("Your Random Sentence Is As Follows:\n");


    for(int i = 0; i < SIZE; i++){
        if(*(sent + i) == 0)
            i = SIZE;
        else
            printf("%s", *(sent + i));
    }


return 0;

}

void nounPick(const const char *nPTR, char *sPTR ){

    int i = 0;
    int l = rand() % 5;
    printf("%d, is L\n", l);

    do{
            switch(*(sPTR + i)){
                case 0:
                *(sPTR + i) = *(nPTR + l);
                break;

                default:
                i++;
                break;
            }
    } while (i < SIZE);
}

void verbPick(const const char *vPTR, char *sPTR ){

    int i = 0;
    int l = rand() % 5;
    printf("%d, is L\n", l);

    do{
            switch(*(sPTR + i)){
                case 0:
                *(sPTR + i) = *(vPTR + l);
                break;

                default:
                i++;
                break;
            }
    } while (i < SIZE);
}

void prepPick(const const char *pPTR, char *sPTR ){

    int i = 0;
    int l = rand() % 5;
    printf("%d, is L\n", l);

    do{
            switch(*(sPTR + i)){
                case 0:
                *(sPTR + i) = *(pPTR + l);
                break;

                default:
                i++;
                break;
            }
    } while (i < SIZE);
    printf("\n\n%s\n\n", sPTR);
}

void artiPick(const const char *aPTR, char *sPTR ){

    int i = 0;
    int l = rand() % 5;
    printf("%d, is L\n", l);

    do{
            switch(*(sPTR + i)){
                case 0:
                *(sPTR + i) = *(aPTR + l);
                break;

                default:
                i++;
                break;
            }
    } while (i < SIZE);
}




Aucun commentaire:

Enregistrer un commentaire