C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-14-2002, 11:33 AM   #1
Registered User
 
Nutshell's Avatar
 
Join Date: Jan 2002
Posts: 1,020
For some reason, it prints funny characters ???

hi,

This program prints funny characters which it shouldn't be doin it. And whenever i change something in the program, the output would change to different set of funny characters. Pls tell me whats wrong with the app. Thnx in advance!

Code:
/* Race between the tortoise and the hare */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 70

void printPositions( const char positions[], int arraySize );
int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare );

int main()
{
   char positions[ SIZE ];
   int whoWins = 0;
   int currentPos_tortoise = 1, currentPos_hare = 1;

   printf( "***---Race---***\n\n" );

   printf( "BANG!!!\nAND THEY'RE OFF!!!\n\n" );

   srand( time( NULL ) );

   while ( whoWins == 0 ) {
      whoWins = moveAnimals( positions, &currentPos_tortoise, &currentPos_hare );
      printPositions( positions, SIZE );
   }

   switch ( whoWins ) {
      case 1:
         printf( "\n\nTurtle wins!\n" );
         break;
      case 2:
         printf( "\n\nHare wins!\n" );
         break;
      case 3:
         printf( "\n\nIt's a tie.\n" );
         break;
   }

   getch();
   return 0;
}

int moveAnimals( char positions[], int *currentPos_Tortoise, int *currentPos_Hare )
{
   static int moveTortoise[ 3 ] = { 3, -6, 1 };
   static int moveHare[ 5 ] = { 0, 9, -12, 1, -2 };
   int i;

   i = 1 + rand() % 10;

   if ( i >= 1 && i <= 5 )
      *currentPos_Tortoise += moveTortoise[ 0 ];
   else if ( i >= 6 && i <= 7 )
      if ( *currentPos_Tortoise != 1 )
         *currentPos_Tortoise += moveTortoise[ 1 ];
   else if ( i >= 8 && i <= 10)
      *currentPos_Tortoise += moveTortoise[ 2 ];

   if ( i >= 1 && i <= 2 )
      *currentPos_Hare += moveHare[ 0 ];
   else if ( i >= 3 && i <= 4 )
      *currentPos_Hare += moveHare[ 1 ];
   else if ( i == 5 )
      if ( *currentPos_Hare != 1 )
         *currentPos_Hare += moveHare[ 2 ];
   else if ( i >= 6 && i <= 8 )
      *currentPos_Hare += moveHare[ 3 ];
   else if ( i >= 9 && i <= 10 )
      if ( *currentPos_Hare != 1 )
         *currentPos_Hare += moveHare[ 4 ];

         printf( "\nCurrent Pos of tortoise = %d ", *currentPos_Tortoise );
         printf( "Current Pos of hare = %d", *currentPos_Hare );

   for ( i = 0; i < 70; i++ )
      positions[ i ] = "-";   /* Clearing up the array */

   if ( *currentPos_Tortoise == *currentPos_Hare )
      positions[ *currentPos_Tortoise ] = "!";
   else {
      positions[ *currentPos_Tortoise ] = "T";
      positions[ *currentPos_Hare ] = "H";
   }

   if ( *currentPos_Tortoise >= 70 && *currentPos_Hare >= 70 )
      return 3;   /* it's a tie */
   else if ( *currentPos_Tortoise >= 70 )
      return 1;   /* turtle wins */
   else if ( *currentPos_Hare >= 70 )
      return 2;   /* hare wins */
   else
      return 0;   /* nobody wins yet */
}

void printPositions( const char positions[], int arraySize )
{
   int i;

   printf( "\n\n" );

   for ( i = 0; i < arraySize; i++ )
      printf( "%c ", positions[ i ] );
}
Nutshell is offline   Reply With Quote
Old 01-14-2002, 11:44 AM   #2
Registered User
 
Join Date: Jan 2002
Location: Cardiff
Posts: 2,219
I'm not sure.
Maybe you should comment out some bits of code and try it from the simplest form up, that's the way I debug.

BTW, don't double post
Brian is offline   Reply With Quote
Old 01-14-2002, 11:45 AM   #3
Registered User
 
Nutshell's Avatar
 
Join Date: Jan 2002
Posts: 1,020
yeah but they are two different questions.
Nutshell is offline   Reply With Quote
Old 01-14-2002, 11:48 AM   #4
Registered User
 
Join Date: Jan 2002
Location: Cardiff
Posts: 2,219
Two questions can fit into the same post (especially if it's about one program :P)

For instance:
Why doesn't this work?
How do I do a comment?

Code:
#include <stdio.h>

int main(void
{
 printf("Hello, world");
 return 0;
}
Oh, and a cookie to the first one to spot the reason it doesn't work
Brian is offline   Reply With Quote
Old 01-14-2002, 11:52 AM   #5
Registered User
 
Nutshell's Avatar
 
Join Date: Jan 2002
Posts: 1,020
yes but i want a title which really describes my problem. Anyway my mistake ok ?
Nutshell is offline   Reply With Quote
Old 01-14-2002, 11:54 AM   #6
Registered User
 
Join Date: Jan 2002
Location: Cardiff
Posts: 2,219
Okay.
Brian is offline   Reply With Quote
Old 01-14-2002, 11:54 AM   #7
Registered User
 
Nutshell's Avatar
 
Join Date: Jan 2002
Posts: 1,020
can u help me out with the thread i've just posted, about the compile warning pls ?
Nutshell is offline   Reply With Quote
Old 01-14-2002, 02:46 PM   #8
Mayor of Awesometown
 
Govtcheez's Avatar
 
Join Date: Aug 2001
Location: MI
Posts: 8,826
What do you mean by funny characters? Chars that aren't letters, or just the wrong letters?

> Oh, and a cookie to the first one to spot the reason it doesn't work

You didn't close the parens in your main statement.
__________________
-Govtcheez
govtcheez03@hotmail.com
Govtcheez is offline   Reply With Quote
Old 01-14-2002, 04:27 PM   #9
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
Quote:
int currentPos_tortoise = 1, currentPos_hare = 1;


if ( i >= 1 && i <= 5 )
*currentPos_Tortoise += moveTortoise[ 0 ];
else if ( i >= 6 && i <= 7 )
if ( *currentPos_Tortoise != 1 )
*currentPos_Tortoise += moveTortoise[ 1 ];
else if ( i >= 8 && i <= 10)
*currentPos_Tortoise += moveTortoise[ 2 ];

if ( i >= 1 && i <= 2 )
*currentPos_Hare += moveHare[ 0 ];
else if ( i >= 3 && i <= 4 )
*currentPos_Hare += moveHare[ 1 ];
else if ( i == 5 )
if ( *currentPos_Hare != 1 )
*currentPos_Hare += moveHare[ 2 ];
else if ( i >= 6 && i <= 8 )
*currentPos_Hare += moveHare[ 3 ];
else if ( i >= 9 && i <= 10 )
if ( *currentPos_Hare != 1 )
*currentPos_Hare += moveHare[ 4 ];
You are dereferencing non pointer values.

'currentPos_Hare' is not a pointer.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?

Last edited by quzah; 01-14-2002 at 04:31 PM.
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Funny characters being printed learninC C Programming 8 03-17-2005 09:02 AM
Maps with VC++6 - funny looking warnings nickname_changed C++ Programming 7 07-09-2003 07:34 PM
Funny looking characters Wiz_Nil C++ Programming 12 03-20-2002 01:23 AM
Characters. Funny looking ones Wiz_Nil C++ Programming 8 02-25-2002 12:33 PM
pigLatin, code prints funny characters Nutshell C Programming 16 01-23-2002 08:33 AM


All times are GMT -6. The time now is 11:42 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22