6 Replies - 312 Views - Last Post: 10 June 2012 - 01:34 PM Rate Topic: -----

#1 Baron.Slayer  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 10-June 12

Randomize strings problem

Posted 10 June 2012 - 11:59 AM

I am the beginner in programming. I am trying to create story with random characters. The output of this code contains just odd symbols instead of my characters. What am I doing wrong?


#include <stdio.h>
#include <string.h>
#include <conio.h>

main()
{
      int iAge = 0;
      int iResponse = 0;
      
      char sName [80];
      
      printf ("\n\tPlease, enter your name: \n");
      gets (sName);
      printf ("\n\tPlease, enter your age: \n");
      scanf ("%d", &iAge);
      
      printf ("\n\t1\tWin\n");
      printf ("\n\t2\tLoose\n");
      printf ("\n\t3\tDraw\n");
      printf ("\n\tPlease, choose the finale: \n");
      scanf ("%d", &iResponse);
      
      char *strfry (char *sColor);
      char *sColor [5] = {0};
           sColor [0] = "White";
           sColor [1] = "Black";
           sColor [2] = "Blue";
           sColor [3] = "Red";
           sColor [4] = "Green";
          
      char *strfry (char *sCar);
      char *sCar [5] = {0};
           sCar [0] = "Toyota Camry";
           sCar [1] = "Cadillac CTS";
           sCar [2] = "BMW X6";
           sCar [3] = "Mercedes-Benz CLK";
           sCar [4] = "Jaguar XF";
      
      char *strfry (char *sState);    
      char *sState [5] = {0};
           sState [0] = "Kentucky";
           sState [1] = "Utah";
           sState [2] = "North Carolina";
           sState [3] = "Georgia";
           sState [4] = "Indiana";
      
      char *strfry (char *sCity);    
      char *sCity [5] = {0};
           sCity [0] = "Taylorsville";
           sCity [1] = "Shelbyville";
           sCity [2] = "Fort Jackson";
           sCity [3] = "Birmingham";
           sCity [4] = "Nashville";
      
      char *strfry (char *sStay);    
      char *sStay [5] = {0};
           sStay [0] = "Marriott";
           sStay [1] = "Crowne Plaza";
           sStay [2] = "Residence Inn";
           sStay [3] = "Hilton Garden Inn";
           sStay [4] = "Springfield";
      
      char *strfry (char *sEatery);    
      char *sEatery [5] = {0};
           sEatery [0] = "Red Lobster";
           sEatery [1] = "Longhorn Steakhouse";
           sEatery [2] = "Stoney River";
           sEatery [3] = "Backyard Burgers";
           sEatery [4] = "Red Robin";
      
      char *strfry (char *sSport);    
      char *sSport [5] = {0};
           sSport [0] = "Football";
           sSport [1] = "Soccer";
           sSport [2] = "Basketball";
           sSport [3] = "Hockey";
           sSport [4] = "Baseball";
           
      char *strfry (char *sTeam);    
      char *sTeam [5] = {0};
           sTeam [0] = "Devils";
           sTeam [1] = "Hawks";
           sTeam [2] = "Lakers";
           sTeam [3] = "Cardinals";
           sTeam [4] = "Kings";
     
      printf ("\n%c is %d years old and is a big fan of %s.  One day %c decided to go to %s, %s to the %s game.\n", sName, iAge, sSport, sCity, sState, sSport);
      printf ("\n%c's favorite team %s had the game with the host team.  %c drove his %s %s all the way to %s, %s.", sName, sTeam, sColor, sCar, sCity, sState);
      printf ("\n%c stayed at the beatiful %s.  After early dinner at %s %c went to the game.", sName, sStay, sEatery);
      
      switch (iResponse) {
             case 1:
                  printf ("\nAfter celebrating the victory of %s at the same %s, %c went home.", sName, sTeam, sEatery);
                  break;
                  
             case 2:
                  printf ("\nThe game ended in a draw and %c with the fans of the host team went to %s to celebrate the meeting.", sName, sEatery);
                  break;
                  
             case 3:
                  printf ("\n%s lost and sad %c went home.", sTeam, sName);
                  break;
             
             } // end switch
             
      getch ();
               
} // end main


edit by ishkabible: please use code tags when posting code. like so, :code: thank you :)

This post has been edited by ishkabible: 10 June 2012 - 12:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Randomize strings problem

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Randomize strings problem

Posted 10 June 2012 - 12:38 PM

You're getting funny symbols because for the strings you're passing a pointer to a pointer, rather than a pointer to the first character in a string, to printf(). You're also using %c (char type) in printf when you want a string (%s).

You're going to need random numbers. rand() in <stdlib.h> is good for this. You could have a set of variables to hold the index, which will be a random number between 0 and 4 inclusive.

An example of fixing up the first printf, you could have the variables
int iChosenSport, iChosenState, iChosenCity;

Then you'd generate some random numbers.
iChosenSport = rand() % 5;
iChosenCity = rand() % 5;
iChosenState = rand() % 5;

Then you would use these to determine which string to print.
printf ("\n%s is %d years old and is a big fan of %s. ", 
        sName, iAge, sSport[iChosenSport]); 
printf("One day %s decided to go to %s, %s to the %s game.\n", 
       sName, sCity[iChosenCity], sState[iChosenState], 
       sSport[iChosenSport]);

I split up a longer printf into 2. Hopefully you see how you could apply this to the rest.

Hope this helps. :)
Was This Post Helpful? 2
  • +
  • -

#3 Baron.Slayer  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 10-June 12

Re: Randomize strings problem

Posted 10 June 2012 - 01:00 PM

Thank a lot! I've already noticed my mistake with %c. I'll try to fix it the way you suggest
Was This Post Helpful? 0
  • +
  • -

#4 Baron.Slayer  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 10-June 12

Re: Randomize strings problem

Posted 10 June 2012 - 01:06 PM

That works! Thanks a lot!!!
Was This Post Helpful? 0
  • +
  • -

#5 Baron.Slayer  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 10-June 12

Re: Randomize strings problem

Posted 10 June 2012 - 01:21 PM

I've changed the code but it does not generate RANDOM answers. Here is the final version:

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

main()
{
      int iAge = 0;
      int iResponse = 0;
      int iSport, iState, iCity;
      int iTeam, iColor, iCar;
      int iEatery, iStay;
      
      iSport = rand () % 5;
      iState = rand () % 5;
      iCity = rand () % 5;
      iTeam = rand () % 5;
      iColor = rand () % 5;
      iCar = rand () % 5;
      iEatery = rand () % 5;
      iStay = rand () % 5;
      
      
      char sName [80];
      
      printf ("\n\tPlease, enter your name: \n");
      gets (sName);
      printf ("\n\tPlease, enter your age: \n");
      scanf ("%d", &iAge);
      
      printf ("\n\t1\tWin\n");
      printf ("\n\t2\tLoose\n");
      printf ("\n\t3\tDraw\n");
      printf ("\n\tPlease, choose the finale: \n");
      scanf ("%d", &iResponse);
      
      char *sColor [5] = {"White", "Black", "Blue", "Red", "Green"};
          
      char *sCar [5] = {"Toyota Camry", "Cadillac CTS", "BMW X6", "Mercedes-Benz CLK", "Jaguar XF"};

      char *sState [5] = {"Kentucky", "Utah", "North Carolina", "Georgia", "Indiana"};
      
      char *sCity [5] = {"Taylorsville", "Shelbyville", "Fort Jackson", "Birmingham", "Nashville"};
      
      char *sStay [5] = {"Marriott", "Crowne Plaza", "Residence Inn", "Hilton Garden Inn", "Springfield"};
      
      char *sEatery [5] = {"Red Lobster", "Longhorn Steakhouse", "Stoney River", "Backyard Burgers", "Red Robin"};
      
      char *sSport [5] = {"Football", "Soccer", "Basketball", "Hockey", "Baseball"};
           
      char *sTeam [5] = {"Devils", "Hawks", "Lakers", "Cardinals", "Kings"};
     
      printf ("\n%s is %d years old and is a big fan of %s.  One day %s decided to go to %s, %s to the %s game.\n", sName, iAge, sSport[iSport], sName, sCity[iCity], sState[iState], sSport[iSport]);
      printf ("\n%s's favorite team %s had the game with the host team.  %s drove his %s %s all the way to %s, %s.", sName, sTeam[iTeam], sName, sColor[iColor], sCar[iCar], sCity[iCity], sState[iState]);
      printf ("\n%s stayed at the beatiful %s.  After early dinner at %s %s went to the game.", sName, sStay[iStay], sEatery[iEatery], sName);
      
      switch (iResponse) {
             case 1:
                  printf ("\nAfter celebrating the victory of %s at the same %s, %s went home.", sTeam[iTeam], sEatery[iEatery], sName);
                  break;
                  
             case 2:
                  printf ("\nThe game ended in a draw and %s with the fans of the host team went to %s to celebrate the meeting.", sName, sEatery[iEatery]);
                  break;
                  
             case 3:
                  printf ("\n%s lost and sad %s went home.", sTeam[iTeam], sName);
                  break;
             
             } // end switch
             
      getch ();
               
} // end main



Any suggestions? Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Randomize strings problem

Posted 10 June 2012 - 01:24 PM

You need to seed the random number generator using the srand function before you call rand of the first time.

If you want the results to be different each time your application is run, you need to make sure that the seed value that you pass to srand is also different each time. To achieve this people usually pass the current system time as the argument to srand.
Was This Post Helpful? 2
  • +
  • -

#7 Baron.Slayer  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 10-June 12

Re: Randomize strings problem

Posted 10 June 2012 - 01:34 PM

Thanks! I'll do that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1