Hi! anyone who could help me fix this code for it to work? i need your help very badly. :)

i'm having a hard time debugging my program for it to work... i dont know how to use the function rand() properly.. :rolleyes: tried everything i can but it just won't work.. please help me out. thanks. :)

the problem is in prn_random_fcookie (void)

#include <stdio.h>
#include <stdlib.h>

char ans;
void prn_random_fcookie(char y);
char fc_array[5][100] =
 {  
    {"Everything will now come your way."},
    {"Now is the time to try something new."},
    {"A handful of patience is worth more than a bushel of brains."},
    {"You have an active mind and a keen imagination."},
    {"Don't let doubt and suspicion bar your progress."}
 };
 
main()
{
  gotoxy (19,8); textcolor(10);
  cprintf ("Fortune cookie of the day:");

  gotoxy (24,10); textcolor (11);
  cprintf ("Everything will now come your way!");
  
  decision();

getch();
clrscr();
}

decision(void)
{
  gotoxy(31,21); textcolor(13);
  cprintf ("want more [Y/N]? "); scanf ("%s", &ans);
  if (ans == 'y' && 'Y')
  {
    prn_random_fcookie();
  }
      
  else
     clrscr();
     gotoxy (24,10); printf ("Goodbye! Have a nice day!");
}
 
prn_random_fcookie ()
{

    srand (time(NULL));
    char fc_array = rand();   
    for (char fc_array=0; fc_array<5; fc_array++)
    {
      gotoxy (19,8); textcolor(10);           
      cprintf ("Fortune cookie of the day:");
      gotoxy (24,10); textcolor (11);
      cprintf ("%s", fc_array);
      char fc_array = rand();   
    }
  decision();
}

You have declared prn_random_fcookie( ) as prn_random_fcookie( char ) ; while you have defined it as

prn_random_fcookie( )
{
   // you code here 
}

As you can see the declaration and definaton dont match.

Also what you need to do is to pass the function nothing, calculate a random integer between 0 to 4 (since your string array has 5 strings ) in your function itself and use that integer to print out a random string.

Also it would be better if you declare the array of strings as

char* fc_array[] = { "Everything will now come your way.",
                                  "Now is the time to try something new.",
                                  "A handful of patience is worth more than a bushel of brains.",
                                  "You have an active mind and a keen imagination.",
                                  "Don't let doubt and suspicion bar your progress."
                                } ;

since this method helps save space and letst the compiler take care of things for you.

You need to write in your function somethign like:

int random = rand( ) % 5 ; // generate random between 0 and 4
printf( "%s", fc_array[random] ) ; // use that random to index your string array

Hope it helped, bye.

thanks a lot! it's working already! you're the best! :D

My pleasure....also one more thing to point out.

The entry point of the program i.e. main( ) returns int so the correct way to write programs would be

int main( ) 
{
   // write your code here
  return 0 ;   // dont forget to return program status
}

Also dont use getch( ) its non standard, use getchar( ) to achieve the same purpose ie stopping the screen.

ei sos... got question... you left the [] unknown... does that also mean that the user can input unlimited phrases or tags? it was only now that i have encountered that. :D glad to learn something new from the pro! :D thanks for helping me again!

It does not mean the user can input as many strings it means that at the time of declaration of your array of strings you can initialize it with as many strings there as possible and the compiler will figure that out for you.

It also tends to save space as compared to the normal method of creating a two dimensional character array.

Hope it helped, bye.

oh i see.. got it! thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.