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();
}

Dani AI

Generated

Quick expert note: the problem is structural rather than magical — several independent bugs combine to make the routine misbehave. The main issues are a mismatched function prototype/definition, reading a single-character choice with the wrong scanf format, an incorrect boolean test for the Y/N check, reseeding the random generator inside the printer, shadowing the global string array with a local variable, and using char for an index. correctly highlighted the prototype and array approach, and reports the fix worked after that guidance.

Seed the RNG once at program start (use the current time) instead of reseeding inside the print function; reseeding on every call often produces the same value if calls happen within the same second. Produce an integer index in the range 0..(count-1) from rand and use that index to pick a message. For five items the simple modulus approach is fine; prefer an int index rather than char.

Read the user choice as a single character (or use getchar) and remove any leftover newline before the next prompt. Compare with logical OR (ans == 'y' || ans == 'Y') or normalize the character to lowercase and compare once. The original if (ans == 'y' && 'Y') is a logic error: it does not test the second comparison and always behaves incorrectly.

Keep function signatures consistent (for example void prn_random_fcookie(void) if no parameter is needed), and do not redeclare fc_array inside the function — either use the global array or pass a pointer plus its length. Prefer an array of string pointers for clarity and memory efficiency. Note also that gotoxy, textcolor and cprintf are Turbo C / conio.h specifics; remove or replace them when porting to modern compilers. Quick checklist:

  • Match prototype and definition.
  • Seed RNG once in main.
  • Read a single char correctly and compare with ||.
  • Use an int index and avoid shadowing the string array.

Recommended Answers

All 6 Replies

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.