jeudi 1 octobre 2015

Interactive, randomized program in C

My goal with this program is to incorporate the users inputs into a sort of interactive/randomized story but I'm not sure how I'm supposed to get the inputs from the users to fit between *ptrDescription, *ptrBeginning, *ptrMiddle, and *ptrEnd. Any help would be much, much appreciated!

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include <string.h>
#include <ctype.h>

int main(void){

int i;
char name[20];
char color[20];
int age;
char sentence[1];
//array of pointers to char arrays
char *ptrDescription[]={"the painfully handsome","the one and only","who seemed much older than"};
char *ptrBeginning[]={"was blissfully ignoring","could clearly see","had no idea"};
char *ptrMiddle[]={"the huge truck","the falling meteor","the bucket of milk","the mailman","the most powerful wizard"};
char *ptrEnd[]={"that was barreling toward them.","on the horizon."};

srand(time(NULL));

printf("Enter your first name: ");
scanf("%s", &name);
printf("\nEnter your age: ");
scanf("%d", &age);
printf("\nEnter your favorite color: ");
scanf("%s", &color);


for (i = 0; i < 20; i++)
{
    strcpy(sentence,ptrDescription[rand()%3]);
    strcat(sentence," ");
    strcat(sentence,ptrBeginning[rand()%3]);
    strcat(sentence," ");
    strcat(sentence,ptrMiddle[rand()%5]);
    strcat(sentence," ");
    strcat(sentence,ptrEnd[rand()%2]);
    strcat(sentence,".");
    sentence[0]=toupper(sentence[0]);

    puts(sentence);
}

getch();
return 0;
}




Aucun commentaire:

Enregistrer un commentaire